Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/models/principals/scopes/visible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@

# Only return Principals that are visible to the current user.
#
# Either the user has the `manage_members` permission in any project,
# or all principals in visible projects are returned.
# - Users with the global permission `view_all_principals` can see all Principals.
# - Admins can see all Principals.
# - Other users can see Principals if:
# - they are a member of the same project as the Principal, or
# - they are the same user, or
# - they share a group with the Principal.
module Principals::Scopes
module Visible
extend ActiveSupport::Concern

class_methods do
def visible(user = ::User.current)
if user.allowed_globally?(:view_all_principals)
if user.allowed_globally?(:view_all_principals) || user.admin?
all
else
in_visible_project_or_me_or_same_groups(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@
end
end

describe "#delete_all" do
context "with no devices" do
it "redirects to the user 2FA tab" do
post :delete_all, params: { id: user.id }
expect(response).to redirect_to edit_user_path(user, tab: :two_factor_authentication)
end
end

context "with existing devices" do
let!(:device1) { create(:two_factor_authentication_device_totp, user:, default: true) }
let!(:device2) { create(:two_factor_authentication_device_sms, user:, default: false) }

it "deletes all devices and redirects" do
post :delete_all, params: { id: user.id }
expect(response).to redirect_to edit_user_path(user, tab: :two_factor_authentication)
expect(user.otp_devices.reload).to be_empty
end
end

context "when admin shares no project or group with the user" do
let!(:device) { create(:two_factor_authentication_device_totp, user:, default: true) }

it "still deletes all devices (regression: User.visible excluded the user)" do
post :delete_all, params: { id: user.id }
expect(response).to redirect_to edit_user_path(user, tab: :two_factor_authentication)
expect(user.otp_devices.reload).to be_empty
end
end
end

describe "#destroy" do
it "croaks on missing id" do
delete :destroy, params: { id: user.id, device_id: "1234" }
Expand Down
6 changes: 6 additions & 0 deletions spec/models/principals/scopes/visible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
include_examples "sees all principals"
end

context "when user is an admin" do
current_user { create(:admin, firstname: "current user") }

include_examples "sees all principals"
end

context "when user has no permission" do
current_user { create(:user, firstname: "current user") }

Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v3/user/user_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
context "as locked admin" do
let(:current_user) { locked_admin }

it_behaves_like "not found"
it_behaves_like "unauthorized access"
end

context "as non-admin" do
Expand Down
Loading