Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sergii/bc54c7857e1056458119182e86824ea3 to your computer and use it in GitHub Desktop.
Save sergii/bc54c7857e1056458119182e86824ea3 to your computer and use it in GitHub Desktop.
Intersection use case - authorize a user who has an allowed role/ability
class PaymentsController < AuthenticatedController
before_action { authorize!(roles: [:superuser], abilities: [:charge_user, :manage_payments]) }
end
class AuthenticatedController < ApplicationController
def authorize!(roles: [], abilities: [])
# current
(roles & current_user.roles).any? || (abilities & current_user.abilities).any?
# desired
roles.intersect?(current_user.roles) || abilities.intersect?(current_user.abilities)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment