Skip to content

Commit

Permalink
AO3-6141 Disable user role checkboxes based on admin permissions (otw…
Browse files Browse the repository at this point in the history
…code#4355)

* AO3 6141 disable user role checkboxes

* please hound

* move logic to userpolicy class

* please hound

* use hash for checking that admin role can edit user role

* please hound
  • Loading branch information
Cesium-Ice authored and sarken committed Jun 24, 2023
1 parent 754e816 commit 2771fee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class UserPolicy < ApplicationPolicy
"tag_wrangling" => [roles: []]
}.freeze

# Define which admin roles can edit which user roles.
ALLOWED_USER_ROLES_BY_ADMIN_ROLES = {
"open_doors" => %w[archivist opendoors],
"policy_and_abuse" => %w[no_resets protected_user],
"superadmin" => %w[archivist no_resets official opendoors protected_user tag_wrangler],
"tag_wrangling" => %w[tag_wrangler]
}.freeze

def can_manage_users?
user_has_roles?(MANAGE_ROLES)
end
Expand All @@ -37,6 +45,10 @@ def permitted_attributes
ALLOWED_ATTRIBUTES_BY_ROLES.values_at(*user.roles).compact.flatten
end

def can_edit_user_role?(role)
ALLOWED_USER_ROLES_BY_ADMIN_ROLES.values_at(*user.roles).compact.flatten.include?(role.name)
end

alias index? can_manage_users?
alias bulk_search? can_manage_users?
alias show? can_manage_users?
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/admin_users/_user_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<td><%= text_field_tag "user[email]", user.email, title: ts("Email"), disabled: !admin_can_update_user_email? %></td>
<% for role in @roles %>
<td>
<%= check_box_tag "user[roles][]", role.id, user.roles.include?(role), title: role.name, id: "user_roles_#{role.id}", disabled: !admin_can_update_user_roles? %>
<%= check_box_tag "user[roles][]", role.id, user.roles.include?(role), title: role.name, id: "user_roles_#{role.id}", disabled: !policy(User).can_edit_user_role?(role) %>
</td>
<% end %>
<td>
Expand Down

0 comments on commit 2771fee

Please sign in to comment.