Class: PermissionPolicy::Authorization
- Inherits:
-
Object
- Object
- PermissionPolicy::Authorization
- Defined in:
- lib/permission_policy/authorization.rb
Instance Attribute Summary collapse
-
#preconditions ⇒ Object
readonly
Returns the value of attribute preconditions.
-
#verified ⇒ Object
readonly
Returns the value of attribute verified.
Instance Method Summary collapse
-
#allowed?(action, options = {}) ⇒ Boolean
Decides if the action is allowed based on the matching strategy.
-
#authorize!(action, options = {}) ⇒ Object
Delegates to #allowed? but raises a NotAllowed exception when false.
-
#initialize(context) ⇒ Authorization
constructor
A new instance of Authorization.
Constructor Details
#initialize(context) ⇒ Authorization
Returns a new instance of Authorization.
5 6 7 8 9 10 11 12 13 |
# File 'lib/permission_policy/authorization.rb', line 5 def initialize(context) @preconditions = [] @context = context context..each do |precondition| set! precondition, context.public_send(precondition) @preconditions << precondition end end |
Instance Attribute Details
#preconditions ⇒ Object (readonly)
Returns the value of attribute preconditions.
3 4 5 |
# File 'lib/permission_policy/authorization.rb', line 3 def preconditions @preconditions end |
#verified ⇒ Object (readonly)
Returns the value of attribute verified.
3 4 5 |
# File 'lib/permission_policy/authorization.rb', line 3 def verified @verified end |
Instance Method Details
#allowed?(action, options = {}) ⇒ Boolean
Decides if the action is allowed based on the matching strategy. You may want to use this method for controlflow inside views.
Example:
do_something if allowed?(:manage, subject: my_subject)
22 23 24 |
# File 'lib/permission_policy/authorization.rb', line 22 def allowed?(action, = {}) strategy_for(action, ).allowed? end |
#authorize!(action, options = {}) ⇒ Object
Delegates to #allowed? but raises a NotAllowed exception when false. You may want to use this method for halting the execution of a controller method.
Example:
def edit
allow!(:manage, subject: my_subject)
end
35 36 37 38 |
# File 'lib/permission_policy/authorization.rb', line 35 def (action, = {}) @verified = true !!allowed?(action, ) or raise PermissionPolicy::NotAllowed end |