Module: AuthorizeIf
- Defined in:
- lib/authorize_if.rb,
lib/authorize_if/version.rb
Overview
Provides a set of methods to handle authorization scenarios. It is included to ActionController::Base on load.
Constant Summary collapse
- NotAuthorizedError =
Class.new(StandardError)
- MissingAuthorizationRuleError =
Class.new(StandardError)
- Configuration =
Class.new do attr_accessor :error_message end
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#authorize(*args, &block) ⇒ Object
Accepts any arguments and configuration block.
-
#authorize_if(rule, &block) ⇒ 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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/authorize_if.rb', line 118 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) ⇒ 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 calls it with ‘AuthorizeIf::Configuration` object as parameter. Behavior can be customized by calling methods on configuraiton object.
57 58 59 60 61 62 |
# File 'lib/authorize_if.rb', line 57 def (rule, &block) config = Configuration.new block.call(config) if block !!rule || raise(NotAuthorizedError, config.) end |