Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where org_admins couldn't promote users #4729

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def demote_to_user
RemoveRoleService.call(user_id: params[:user_id],
resource_type: Role::ORG_ADMIN,
resource_id: current_organization.id)
redirect_to user_update_redirect_path, notice: notice
redirect_to user_update_redirect_path, notice: "User has been demoted!"
rescue => e
redirect_back(fallback_location: organization_path, alert: e.message)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def kind
"normal"
end

def is_admin?(org)
has_role?(Role::ORG_ADMIN, org) || has_role?(Role::SUPER_ADMIN)
end

def switchable_roles
all_roles = roles.to_a.group_by(&:resource_id)
all_roles.values.each do |role_list|
Expand Down
13 changes: 7 additions & 6 deletions app/views/users/_organization_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
<ul class="dropdown-menu">
<li>
<%=
edit_button_to(
cielf marked this conversation as resolved.
Show resolved Hide resolved
edit_admin_user_path(user),
{text: 'Edit User'}
)
edit_button_to(
promote_to_org_admin_organization_path(user_id: user.id, organization_name: current_organization.short_name),
{text: 'Promote to Admin'},
{method: :post, rel: "nofollow", data: {confirm: 'This will promote the user to admin status. Are you sure that you want to submit this?', size: 'xs'}}
)
%>
</li>
<li>
Expand All @@ -30,8 +31,8 @@
</ul>
</div>
<% end %>
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) && user.has_role?(Role::ORG_ADMIN, current_organization) %>
<%= edit_button_to demote_to_user_organization_path(user_id: user.id),
<% if current_user.is_admin?(current_organization) && user.has_role?(Role::ORG_ADMIN, current_organization) %>
dorner marked this conversation as resolved.
Show resolved Hide resolved
<%= edit_button_to demote_to_user_organization_path(user_id: user.id, organization_name: current_organization.short_name),
{text: 'Demote to User'},
{method: :post, rel: "nofollow", data: {confirm: 'This will demote the admin to user status. Are you sure that you want to submit this?', size: 'xs'}} unless user.id == current_user.id %>
<% end %>
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/organization_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,23 @@
expect(response.body).to include(organization.display_last_distribution_date)
end
end

describe "POST #promote_to_org_admin" do
before { post promote_to_org_admin_organization_path(user_id: user.id, organization_name: organization.short_name) }

it "promotes the user to org_admin" do
expect(user.has_role?(Role::ORG_ADMIN, organization)).to eq(true)
expect(response).to redirect_to(admin_organization_path({ id: organization.id }))
end
end

describe "POST #demote_to_user" do
before { post demote_to_user_organization_path(user_id: admin_user.id, organization_name: organization.short_name) }

it "demotes the org_admin to user" do
expect(admin_user.reload.has_role?(Role::ORG_ADMIN, admin_user.organization)).to be_falsey
expect(response).to redirect_to(admin_organization_path({ id: organization.id }))
end
end
end
end
25 changes: 25 additions & 0 deletions spec/system/admin/organizations_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,30 @@
expect(page).to have_content("Default email text")
expect(page).to have_content("Users")
end

it "can promote a user to org_admin in the organization" do
cielf marked this conversation as resolved.
Show resolved Hide resolved
user = create(:user, name: "User to be promoted", organization: foo_org)

visit admin_organization_path(foo_org.id)
accept_confirm do
click_button "Actions"
click_link "Promote to Admin"
end

expect(page).to have_content("User has been promoted!")
expect(user.has_role?(Role::ORG_ADMIN, foo_org)).to be true
end

it "can demote an org_admin to user in the organization" do
user = create(:organization_admin, name: "User to be promoted", organization: foo_org)

visit admin_organization_path(foo_org.id)
accept_confirm do
click_link "Demote to User"
end

expect(page).to have_content("User has been demoted!")
expect(user.has_role?(Role::ORG_ADMIN, foo_org)).to be false
end
end
end
23 changes: 23 additions & 0 deletions spec/system/organization_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,28 @@
expect(page).to have_content("User has been removed!")
expect(user.has_role?(Role::ORG_USER)).to be false
end

it "can promote a user to org_admin in the organization" do
user = create(:user, name: "User to be promoted", organization: organization)
visit organization_path
accept_confirm do
click_button dom_id(user, "dropdownMenu")
click_link "Promote to Admin"
end

expect(page).to have_content("User has been promoted!")
expect(user.has_role?(Role::ORG_ADMIN, organization)).to be true
end

it "can demote an org_admin to user in the organization" do
user = create(:organization_admin, name: "User to be demoted", organization: organization)
visit organization_path
accept_confirm do
click_link "Demote to User"
end

expect(page).to have_content("User has been demoted!")
expect(user.has_role?(Role::ORG_ADMIN, organization)).to be false
end
end
end
Loading