authorize_action
! Usage: “` class ThingsController < ApplicationController
before_action do
authorize_action! to: 'thing.owner'
authorize_action! :administrator, :manager, of: 'thing.other_thing'
authorize_action! :anyone, from: 'thing.company'
end, only: [:index, :show]
end “`
def authorize_action!(*given_roles)
given_roles, options = AuthorizationHelper.parse_given_roles(given_roles)
auth_request_env['match_roles_on'] = options[:match_roles_on] if options.key?(:match_roles_on)
return auth_request_env['authorized_roles'] = Role.all if super_admin_user?
auth_request_env['authorized_roles'] = current_user_roles.where(name: given_roles)
raise ActionErrors::Forbidden if authorized_roles.map(&:name).count == 1 && current_user_roles.first.name == 'manager' && current_user_roles.where(name: 'manager').where.not(target_id: nil).empty?
raise ActionErrors::Forbidden unless authorized_roles.any?
end