Module: ActionPolicy::Behaviour
- Includes:
- ActionPolicy::Behaviours::PolicyFor
- Included in:
- Channel, Controller
- Defined in:
- lib/action_policy/behaviour.rb
Overview
Provides ‘authorize!` and `allowed_to?` methods and `authorize` class method to define authorization context.
Could be included anywhere to perform authorization.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#allowed_to?(rule, record, **options) ⇒ Boolean
Checks that an activity is allowed for the current context (e.g. user).
- #authorization_context ⇒ Object
-
#authorization_rule_for(policy, rule) ⇒ Object
Check that rule is defined for policy, otherwise fallback to :manage? rule.
-
#authorize!(record, to:, **options) ⇒ Object
Authorize action against a policy.
Methods included from ActionPolicy::Behaviours::PolicyFor
#authorization_namespace, #policy_for
Class Method Details
.included(base) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/action_policy/behaviour.rb', line 16 def self.included(base) # Handle ActiveSupport::Concern differently if base.respond_to?(:class_methods) base.class_methods do include ClassMethods end else base.extend ClassMethods end end |
Instance Method Details
#allowed_to?(rule, record, **options) ⇒ Boolean
Checks that an activity is allowed for the current context (e.g. user).
Returns true of false.
42 43 44 45 |
# File 'lib/action_policy/behaviour.rb', line 42 def allowed_to?(rule, record, **) policy = policy_for(record: record, **) policy.apply((policy, rule)) end |
#authorization_context ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/action_policy/behaviour.rb', line 47 def return if instance_variable_defined?(:@__authorization_context) = self.class. .each_with_object({}) do |(key, meth), obj| obj[key] = public_send(meth) end end |
#authorization_rule_for(policy, rule) ⇒ Object
Check that rule is defined for policy, otherwise fallback to :manage? rule.
59 60 61 |
# File 'lib/action_policy/behaviour.rb', line 59 def (policy, rule) policy.resolve_rule(rule) end |
#authorize!(record, to:, **options) ⇒ Object
Authorize action against a policy.
Policy is inferred from record (unless explicitly specified through ‘with` option).
Raises ‘ActionPolicy::Unauthorized` if check failed.
33 34 35 36 37 |
# File 'lib/action_policy/behaviour.rb', line 33 def (record, to:, **) policy = policy_for(record: record, **) Authorizer.call(policy, (policy, to)) end |