Module: AuthorizeIf
- Defined in:
- lib/authorize_if.rb,
lib/authorize_if/errors.rb,
lib/authorize_if/version.rb
Overview
Provides a set of methods to handle authorization scenarios. It is included to ActionController::Base on load.
Defined Under Namespace
Classes: MissingAuthorizationRuleError, NotAuthorizedError
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#authorize(*args, &block) ⇒ Object
Accepts any arguments and configuration block.
-
#authorize_if(rule, &block) {|exception| ... } ⇒ Boolean
Evaluates given object as boolean.
Instance Method Details
#authorize(*args, &block) ⇒ Object
Accepts any arguments and configuration block. Calls corresponding authorization rule method with given arguments, except block.
Then calls ‘#authorize_if` with the returning value of corresponding authorization rule as first argument, and with given configuration block.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/authorize_if.rb', line 137 def (*args, &block) rule_method_name = "authorize_#{action_name}?" unless self.respond_to?(rule_method_name, true) msg = [ "No authorization rule defined for action", "#{controller_name}##{action_name}.", "Please define method ##{rule_method_name} for", self.class.name ].join(' ') raise(MissingAuthorizationRuleError, msg) end (self.send(rule_method_name, *args), &block) end |
#authorize_if(rule, &block) {|exception| ... } ⇒ Boolean
Evaluates given object as boolean. Returns ‘true’ if object evaluates to ‘true’. Raises ‘AuthorizeIf::NotAuthorizedError` if object evaluates to ’false’.
Also accepts block and yields it with ‘AuthorizeIf::NotAuthorizedError` exception object. Behavior can be customized by calling methods on exception object.
65 66 67 68 69 70 71 |
# File 'lib/authorize_if.rb', line 65 def (rule, &block) return true if !!rule exception = NotAuthorizedError.exception yield(exception) if block raise(exception, exception.instance_variable_get(:@message)) end |