From 6894b547b92cb9de04262e14a57cd97010fa9fc4 Mon Sep 17 00:00:00 2001 From: Kayla McEowen Date: Wed, 14 Dec 2022 06:27:17 -0600 Subject: [PATCH] Explicitly add organization for super admins (#3282) Co-authored-by: KaylaGallatin --- app/controllers/organizations_controller.rb | 16 +++-- app/views/users/_organization_user.html.erb | 8 +-- .../users/_organization_users_table.html.erb | 3 +- spec/requests/organization_requests_spec.rb | 59 +++++++++++++++++++ 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 4d5f853b68..4c403292ab 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -40,7 +40,7 @@ def promote_to_org_admin user = User.find(params[:user_id]) raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization) user.add_role(Role::ORG_ADMIN, current_organization) - redirect_to organization_path, notice: "User has been promoted!" + redirect_to user_update_redirect_path, notice: "User has been promoted!" end def demote_to_user @@ -53,21 +53,21 @@ def demote_to_user notice = "Admin has been changed to User!" end - redirect_to organization_path, notice: notice + redirect_to user_update_redirect_path, notice: notice end def deactivate_user user = User.with_discarded.find_by!(id: params[:user_id]) raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization) user.discard! - redirect_to organization_path, notice: "User has been deactivated." + redirect_to user_update_redirect_path, notice: "User has been deactivated." end def reactivate_user user = User.with_discarded.find_by!(id: params[:user_id]) raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization) user.undiscard! - redirect_to organization_path, notice: "User has been reactivated." + redirect_to user_update_redirect_path, notice: "User has been reactivated." end private @@ -89,4 +89,12 @@ def organization_params partner_form_fields: [] ) end + + def user_update_redirect_path + if current_user.has_role?(Role::SUPER_ADMIN) + admin_organization_path(current_organization.id) + else + organization_path + end + end end diff --git a/app/views/users/_organization_user.html.erb b/app/views/users/_organization_user.html.erb index 9f54c531fd..bb5bb83770 100644 --- a/app/views/users/_organization_user.html.erb +++ b/app/views/users/_organization_user.html.erb @@ -22,7 +22,7 @@
  • <%= edit_button_to( - promote_to_org_admin_organization_path(user_id: user.id), + promote_to_org_admin_organization_path(current_organization, user_id: user.id), {text: 'Make 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'}} ) @@ -30,12 +30,12 @@
  • - <%= deactivate_button_to deactivate_user_organization_path(user_id: user.id), + <%= deactivate_button_to deactivate_user_organization_path(current_organization, user_id: user.id), {id: dom_id(user), method: :post, class: 'deactivate', rel: "nofollow", data: {confirm: 'This will deactivate the user. Are you sure that you want to submit this?', size: 'xs'}} %>
  • <% else %> - <%= reactivate_button_to reactivate_user_organization_path(user_id: user.id), + <%= reactivate_button_to reactivate_user_organization_path(current_organization, user_id: user.id), {id: dom_id(user), method: :post, class: 'reactivate', rel: "nofollow", data: {confirm: 'This will reactivate the user. Are you sure that you want to submit this?', size: 'xs'}} %> <% end %> @@ -43,7 +43,7 @@ <% 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), + <%= edit_button_to demote_to_user_organization_path(current_organization, user_id: user.id), {text: 'Make 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/app/views/users/_organization_users_table.html.erb b/app/views/users/_organization_users_table.html.erb index 4f49784469..f02912ba84 100644 --- a/app/views/users/_organization_users_table.html.erb +++ b/app/views/users/_organization_users_table.html.erb @@ -25,7 +25,8 @@ <%= render partial: "/users/organization_user", collection: @organization.users.with_discarded.alphabetized, - as: :user %> + as: :user, + locals: { current_organization: current_organization || @organization } %> diff --git a/spec/requests/organization_requests_spec.rb b/spec/requests/organization_requests_spec.rb index e6241c985e..b24695f123 100644 --- a/spec/requests/organization_requests_spec.rb +++ b/spec/requests/organization_requests_spec.rb @@ -169,4 +169,63 @@ end end end + + context 'When signed in as a super admin' do + before do + sign_in(@super_admin) + end + + describe "POST #promote_to_org_admin" do + subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: @user.id)) } + + it "redirect after update" do + subject + expect(response).to redirect_to(admin_organization_path(@organization.id, default_params)) + end + it "change user" do + expect { subject }.to change { @user.reload.kind }.to "admin" + end + end + + describe "POST #demote_to_user" do + let(:admin_user) do + create(:user, organization: @organization, name: "ADMIN USER") + end + subject { post demote_to_user_organization_path(default_params.merge(user_id: admin_user.id)) } + + it "redirect after update" do + subject + expect(response).to redirect_to(admin_organization_path(@organization.id, default_params)) + end + it "demotes the user to user" do + subject + expect(admin_user.reload.has_role?(Role::ORG_ADMIN, admin_user.organization)).to be_falsey + end + end + + describe "PUT #deactivate_user" do + subject { put deactivate_user_organization_path(default_params.merge(user_id: @user.id)) } + + it "redirect after update" do + subject + expect(response).to redirect_to(admin_organization_path(@organization.id, default_params)) + end + it "deactivates the user" do + expect { subject }.to change { @user.reload.discarded_at }.to be_present + end + end + + describe "PUT #reactivate_user" do + subject { put reactivate_user_organization_path(default_params.merge(user_id: @user.id)) } + before { @user.discard! } + + it "redirect after update" do + subject + expect(response).to redirect_to(admin_organization_path(@organization.id, default_params)) + end + it "reactivates the user" do + expect { subject }.to change { @user.reload.discarded_at }.to be_nil + end + end + end end