diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 9a07bdfafd..b10cbce8e1 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 52e16fcc63..5a47d45fdb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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| diff --git a/app/views/users/_organization_user.html.erb b/app/views/users/_organization_user.html.erb index 29a3d786b0..0e29481d87 100644 --- a/app/views/users/_organization_user.html.erb +++ b/app/views/users/_organization_user.html.erb @@ -16,12 +16,15 @@ <% 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) %> + <%= 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 %> diff --git a/spec/requests/organization_requests_spec.rb b/spec/requests/organization_requests_spec.rb index 3c93b169be..3bf3754666 100644 --- a/spec/requests/organization_requests_spec.rb +++ b/spec/requests/organization_requests_spec.rb @@ -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 @@ -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 @@ -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 @@ -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