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 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
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
19 changes: 11 additions & 8 deletions app/views/users/_organization_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
</button>
<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'}
)
%>
<% if current_user.has_role?(Role::SUPER_ADMIN) %>
<%= edit_button_to(edit_admin_user_path(user), { text: 'Edit User' }) %>
<% else %>
<%= edit_button_to(
promote_to_org_admin_organization_path(user_id: user.id),
{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'}}
) %>
<% end %>
</li>
<li>
<%= delete_button_to remove_user_organization_path(user_id: user.id),
Expand All @@ -30,8 +33,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
52 changes: 52 additions & 0 deletions spec/requests/organization_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@
expect(response.body).to include "Demote to User"
end

it "can see 'Promote to User' button for users" do
get organization_path

within(".content") do
expect(response.body).to have_link("Actions")
end

within "#dropdown-toggle" do
expect(response.body).to have_link("Promote User")
expect(response.body).to have_link("Remove User")
end
end

it "can re-invite a user to an organization after 7 days" do
create(:user, name: "Ye Olde Invited User", invitation_sent_at: Time.current - 7.days)
get organization_path
Expand Down Expand Up @@ -311,6 +324,7 @@
subject
expect(user.has_role?(Role::ORG_ADMIN, organization)).to eq(true)
expect(response).to redirect_to(organization_path)
expect(flash[:notice]).to eq("User has been promoted!")
end
end

Expand All @@ -321,6 +335,7 @@
subject
expect(admin_user.reload.has_role?(Role::ORG_ADMIN, admin_user.organization)).to be_falsey
expect(response).to redirect_to(organization_path)
expect(flash[:notice]).to eq("User has been demoted!")
end
end

Expand Down Expand Up @@ -402,6 +417,43 @@
expect(response.body).to include(organization.created_at.strftime("%Y-%m-%d"))
expect(response.body).to include(organization.display_last_distribution_date)
end

it "can see 'Edit User' button for users" do
within(".content") do
expect(response.body).to have_link("Actions")
end

within "#dropdown-toggle" do
expect(response.body).to have_link("Edit User")
expect(response.body).to have_link("Remove User")
end
end

it "can see 'Demote User' button for organizaiton admins" do
within(".content") do
expect(response.body).to have_link("Demote to User")
end
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 }))
expect(flash[:notice]).to eq("User has been promoted!")
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 }))
expect(flash[:notice]).to eq("User has been demoted!")
end
end
end
end