docbrown/app/services/users/remove_role.rb
Dhurba baral 3bc04bdcc2
Allows super-admin the ability to remove the role from themselves (#19621)
* Allows removing own role

* Removes dead test case
2023-06-26 09:11:33 -04:00

32 lines
785 B
Ruby

module Users
class RemoveRole
Response = Struct.new(:success, :error_message, keyword_init: true)
def self.call(...)
new(...).call
end
def initialize(user:, role:, resource_type:)
@user = user
@role = role
@resource_type = resource_type&.safe_constantize
@response = Response.new(success: false)
end
def call
if resource_type && user.remove_role(role, resource_type)
response.success = true
elsif user.remove_role(role)
response.success = true
end
response
rescue StandardError => e
response.error_message = I18n.t("services.users.remove_role.error", e_message: e.message)
response
end
private
attr_reader :user, :role, :resource_type, :response
end
end