Module: RailsAuthorize
- Defined in:
- lib/rails_authorize.rb,
lib/rails_authorize/version.rb
Defined Under Namespace
Classes: AuthorizationNotPerformedError, NotAuthorizedError, ScopingNotPerformedError
Constant Summary collapse
- VERSION =
"1.5.0"
Instance Method Summary collapse
-
#authorize(target, options = {}) ⇒ Object
Throwing an error if the user is not authorized to perform the given action.
-
#authorized_scope(target, options = {}) ⇒ Scope
Throwing an error if the user is not authorized to perform the given action.
-
#permitted_attributes(target, options = {}) ⇒ Hash{String => Object}
Retrieves a set of permitted attributes from the policy by instantiating the policy class for the given record and calling ‘permitted_attributes` on it, or `permitted_attributes_for_action` if `action` is defined.
-
#policy(target, options = {}) ⇒ Object
Finds policy class for given target and returns new instance.
-
#policy_scope(target, options = {}) ⇒ Scope
Retrieves the policy scope for the given target.
-
#skip_authorization ⇒ void
Allow this action not to perform authorization.
-
#skip_policy_scope ⇒ void
Allow this action not to perform policy scoping.
-
#verify_authorized ⇒ void
Raises an error if authorization has not been performed.
-
#verify_policy_scoped ⇒ void
Raises an error if policy scoping has not been performed.
Instance Method Details
#authorize(target, options = {}) ⇒ Object
Throwing an error if the user is not authorized to perform the given action
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails_authorize.rb', line 37 def (target, ={}) return (nil, target) if target.is_a?(Hash) action = .delete(:action) || "#{action_name}?" policy = policy(target, ) raise(NotAuthorizedError) unless policy.public_send(action) @_policy_authorized = true target || true end |
#authorized_scope(target, options = {}) ⇒ Scope
Throwing an error if the user is not authorized to perform the given action
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rails_authorize.rb', line 74 def (target, ={}) action = .delete(:action) || "#{action_name}?" policy = policy(target, ) raise(NotAuthorizedError) unless policy.public_send(action) @_policy_scoped = @_policy_authorized = true policy.scope end |
#permitted_attributes(target, options = {}) ⇒ Hash{String => Object}
Retrieves a set of permitted attributes from the policy by instantiating the policy class for the given record and calling ‘permitted_attributes` on it, or `permitted_attributes_for_action` if `action` is defined. It then infers what key the record should have in the params hash and retrieves the permitted attributes from the params hash under that key.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rails_authorize.rb', line 95 def permitted_attributes(target, ={}) return permitted_attributes(nil, target) if target.is_a?(Hash) action = .delete(:action) || action_name policy = policy(target, ) method_name = if policy.respond_to?("permitted_attributes_for_#{action}") "permitted_attributes_for_#{action}" else 'permitted_attributes' end param_key = if [:param_key] [:param_key] elsif policy.try(:param_key).present? policy.param_key else target.model_name.name.underscore end params.require(param_key).permit(*policy.public_send(method_name)) end |
#policy(target, options = {}) ⇒ Object
Finds policy class for given target and returns new instance
20 21 22 23 24 25 |
# File 'lib/rails_authorize.rb', line 20 def policy(target, ={}) user = [:user] || current_user klass = [:policy] || "#{target.model_name.name}Policy".constantize klass.new(user, target, [:context] || {}) end |
#policy_scope(target, options = {}) ⇒ Scope
Retrieves the policy scope for the given target
58 59 60 61 62 |
# File 'lib/rails_authorize.rb', line 58 def policy_scope(target, ={}) @_policy_scoped = true policy(target, ).scope end |
#skip_authorization ⇒ void
This method returns an undefined value.
Allow this action not to perform authorization.
129 130 131 |
# File 'lib/rails_authorize.rb', line 129 def @_policy_authorized = true end |
#skip_policy_scope ⇒ void
This method returns an undefined value.
Allow this action not to perform policy scoping.
144 145 146 |
# File 'lib/rails_authorize.rb', line 144 def skip_policy_scope @_policy_scoped = true end |
#verify_authorized ⇒ void
This method returns an undefined value.
Raises an error if authorization has not been performed
122 123 124 |
# File 'lib/rails_authorize.rb', line 122 def raise AuthorizationNotPerformedError, self.class unless end |
#verify_policy_scoped ⇒ void
This method returns an undefined value.
Raises an error if policy scoping has not been performed
137 138 139 |
# File 'lib/rails_authorize.rb', line 137 def verify_policy_scoped raise ScopingNotPerformedError, self.class unless policy_scoped? end |