Forbid account self-removal

This commit is contained in:
Jeffrey Hardy
2024-09-24 15:46:03 -04:00
parent 66a7641ecd
commit ee67d485c4
3 changed files with 12 additions and 2 deletions
@@ -1,5 +1,6 @@
class Accounts::UsersController < ApplicationController
before_action :set_user, only: %i[ destroy ]
before_action :ensure_permission_to_remove, only: :destroy
def index
@users = Current.account.users.active
@@ -14,4 +15,8 @@ class Accounts::UsersController < ApplicationController
def set_user
@user = Current.account.users.active.find(params[:id])
end
def ensure_permission_to_remove
head :forbidden unless Current.user.can_remove?(@user)
end
end
+4
View File
@@ -28,6 +28,10 @@ class User < ApplicationRecord
end
end
def can_remove?(other)
other != self
end
private
def deactived_email_address
email_address.sub(/@/, "-deactivated-#{SecureRandom.uuid}@")
+3 -2
View File
@@ -5,8 +5,9 @@
<hr class="flex-item-grow margin-none" aria-hidden="true">
<%= button_to account_user_path(user), method: :delete, class: "btn btn--small btn--negative flex-item-no-shrink", data: {
turbo_confirm: "Are you sure you want to permanently remove this person from the account?" } do %>
<%= button_to account_user_path(user), method: :delete, class: "btn btn--small btn--negative flex-item-no-shrink",
disabled: !Current.user.can_remove?(user),
data: { turbo_confirm: "Are you sure you want to permanently remove this person from the account?" } do %>
<%= image_tag "minus.svg", aria: { hidden: true }, size: 24 %>
<span class="for-screen-reader">Remove <%= user.name %> from the account</span>
<% end %>