diff --git a/app/controllers/accounts/users_controller.rb b/app/controllers/accounts/users_controller.rb index c387c62a9..2cda84475 100644 --- a/app/controllers/accounts/users_controller.rb +++ b/app/controllers/accounts/users_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 71544ca4a..a9c5ce057 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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}@") diff --git a/app/views/accounts/users/_user.html.erb b/app/views/accounts/users/_user.html.erb index 8acc9ffa6..4603631b1 100644 --- a/app/views/accounts/users/_user.html.erb +++ b/app/views/accounts/users/_user.html.erb @@ -5,8 +5,9 @@ - <%= 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 %> Remove <%= user.name %> from the account <% end %>