diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a159f7b..51b7535 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,9 +11,9 @@ def admin_index authorize User if current_user.can_super_admin? - @users = User.includes(:roles).page(1) + @users = User.page(1) else - @users = current_user.org.users.includes(:roles).page(1) + @users = current_user.org.users.page(1) end end @@ -22,17 +22,22 @@ # Permissions which the user already has are pre-selected # Selecting new permissions and saving calls the admin_update_permissions action def admin_grant_permissions - @user = User.includes(:perms, :roles).find(params[:id]) - authorize @user - user_perms = current_user.perms - @perms = user_perms & [Perm.grant_permissions, Perm.modify_templates, Perm.modify_guidance, - Perm.use_api, Perm.change_org_details, Perm.add_orgs, - Perm.change_affiliation, Perm.grant_api] + user = User.find(params[:id]) + authorize user + + # Super admin can grant any Perm, org admins can only grant Perms they + # themselves have access to + if current_user.can_super_admin? + perms = Perm.all + else + perms = current_user.perms + end + render json: { "user" => { - "id" => @user.id, + "id" => user.id, "html" => render_to_string(partial: 'users/admin_grant_permissions', - locals: { user: @user, perms: @perms }, + locals: { user: user, perms: perms }, formats: [:html]) } }.to_json @@ -43,7 +48,7 @@ # redirects to the admin_index action # should add validation that the perms given are current perms of the current_user def admin_update_permissions - @user = User.includes(:perms).find(params[:id]) + @user = User.find(params[:id]) authorize @user perms_ids = params[:perm_ids].blank? ? [] : params[:perm_ids].map(&:to_i) perms = Perm.where( id: perms_ids) diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index cd85bb9..070e52f 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -16,11 +16,11 @@ end def admin_grant_permissions? - @user.can_grant_permissions? && (@users.org_id == @user.org_id) + @user.can_grant_permissions? && ((@users.org_id == @user.org_id) || @user.can_super_admin?) end def admin_update_permissions? - @user.can_grant_permissions? && (@users.org_id == @user.org_id) + @user.can_grant_permissions? && ((@users.org_id == @user.org_id) || @user.can_super_admin?) end # Allows the user to swap their org affiliation on the fly diff --git a/app/views/paginable/users/_index.html.erb b/app/views/paginable/users/_index.html.erb index 65da84d..377b95f 100644 --- a/app/views/paginable/users/_index.html.erb +++ b/app/views/paginable/users/_index.html.erb @@ -39,7 +39,9 @@ <%= render partial: 'users/current_privileges', locals: { user: user } %> - <% unless current_user == user %> + <%# Do not allow a user to change their own permissions or a super admin's permissions if they are not a super admin %> + <% unless current_user == user || + !current_user.can_super_admin? && user.can_super_admin? %> <% b_label = _('Edit') %> <%= link_to( b_label, admin_grant_permissions_user_path(user)) %> <% end %> diff --git a/app/views/users/_admin_grant_permissions.html.erb b/app/views/users/_admin_grant_permissions.html.erb index 82273a8..9d3c407 100644 --- a/app/views/users/_admin_grant_permissions.html.erb +++ b/app/views/users/_admin_grant_permissions.html.erb @@ -1,71 +1,70 @@ -<% namesHash = name_and_text %> -