Class: Shamu::Security::Policy
- Inherits:
-
Object
- Object
- Shamu::Security::Policy
- Includes:
- Roles
- Defined in:
- lib/shamu/security/policy.rb
Overview
...
Direct Known Subclasses
Dependencies collapse
-
#principal ⇒ Principal
Principal holding user identity and access credentials.
-
#roles ⇒ Array<Roles>
Roles that have been granted to the #principal.
DSL collapse
-
#alias_action(*actions, to: fail)
Add an action alias so that granting the alias will result in permits for any of the listed actions.
-
#deny(*actions, resource) {|resource, additional_context| ... }
Explicitly deny an action previously granted with #permit.
-
#permissions
Hook to be overridden by a derived class to define the set of rules that #permit? should consider when evaluating the #principal's permissions on a resource.
-
#permit(*actions, resource) {|resource, additional_context| ... }
Permit one or more
actionsto be performed on a givenresource. -
#when_elevated(&block)
Only #authorize! the permissions defined in the given block when the #principal has elevated this session by providing their credentials.
Instance Method Summary collapse
-
#authorize!(action, resource, additional_context = nil) ⇒ resource
Authorize the given
actionon the given resource. -
#in_role?(*roles) ⇒ Boolean
True if the #principal has been granted one of the given roles.
-
#initialize(principal: nil, roles: nil) ⇒ Policy
constructor
A new instance of Policy.
-
#permit?(action, resource, additional_context = nil) ⇒ :yes, ...
Determines if the given
actionmay be performed on the givenresource.
Methods included from Roles
expand_roles, role, role_defined?, roles
Constructor Details
#initialize(principal: nil, roles: nil) ⇒ Policy
Returns a new instance of Policy.
53 54 55 56 |
# File 'lib/shamu/security/policy.rb', line 53 def initialize( principal: nil, roles: nil ) @principal = principal || Principal.new @roles = roles || [] end |
Instance Attribute Details
#principal ⇒ Principal
Returns principal holding user identity and access credentials.
45 46 47 |
# File 'lib/shamu/security/policy.rb', line 45 def principal @principal end |
#roles ⇒ Array<Roles>
Returns roles that have been granted to the #principal.
49 50 51 |
# File 'lib/shamu/security/policy.rb', line 49 def roles @roles end |
Instance Method Details
#alias_action(*actions, to: fail)
This method returns an undefined value.
Add an action alias so that granting the alias will result in permits for any of the listed actions.
246 247 248 249 |
# File 'lib/shamu/security/policy.rb', line 246 def alias_action( *actions, to: fail ) # bug in rubocop chokes on trailing required keyword aliases[to] ||= [] aliases[to] |= actions end |
#authorize!(action, resource, additional_context = nil) ⇒ resource
Authorize the given action on the given resource. If it is not
permitted then an exception is raised.
65 66 67 68 69 70 71 72 73 |
# File 'lib/shamu/security/policy.rb', line 65 def ( action, resource, additional_context = nil ) return resource if permit?( action, resource, additional_context ) == :yes fail Security::AccessDeniedError, action: action, resource: resource, additional_context: additional_context, principal: principal end |
#deny(*actions, resource) {|resource, additional_context| ... }
This method returns an undefined value.
Explicitly deny an action previously granted with #permit.
204 205 206 |
# File 'lib/shamu/security/policy.rb', line 204 def deny( *actions, resource, &block ) add_rule( actions, resource, false, &block ) end |
#in_role?(*roles) ⇒ Boolean
Returns true if the #principal has been granted one of the given roles.
120 121 122 |
# File 'lib/shamu/security/policy.rb', line 120 def in_role?( *roles ) ( principal_roles & roles ).any? end |
#permissions
This method returns an undefined value.
Hook to be overridden by a derived class to define the set of rules that #permit? should consider when evaluating the #principal's permissions on a resource.
Rules defined in the permissions block are evaluated in reverse order such that the last matching #permit or #deny will determine the permission.
If no rules match, the permission is denied.
158 159 160 |
# File 'lib/shamu/security/policy.rb', line 158 def fail IncompleteSetupError, "Permissions have not been defined. Add a private `permissions` method to #{ self.class.name }" # rubocop:disable Metrics/LineLength end |
#permit(*actions, resource) {|resource, additional_context| ... }
This method returns an undefined value.
Permit one or more actions to be performed on a given resource.
When a block is provided the policy will yield to the block to allow for more complex or context aware policy checks. The block is not called if the resource offered to #permit? is a Class or Module.
189 190 191 192 193 |
# File 'lib/shamu/security/policy.rb', line 189 def permit( *actions, resource, &block ) result = @when_elevated ? :maybe : :yes add_rule( actions, resource, result, &block ) end |
#permit?(action, resource, additional_context = nil) ⇒ :yes, ...
Determines if the given action may be performed on the given
resource.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/shamu/security/policy.rb', line 87 def permit?( action, resource, additional_context = nil ) fail_on_active_record_check( resource ) rules.each do |rule| next unless rule.match?( action, resource, additional_context ) return rule.result end false end |
#when_elevated(&block)
This method returns an undefined value.
Only #authorize! the permissions defined in the given block when the #principal has elevated this session by providing their credentials.
Permissions defined in the block will yield a :maybe result when
queried via #permit? and will raise an AccessDeniedError when
an #authorize! check is enforced.
This allows you to enable/disable UX in response to what a user should be capable of doing but wait to actually allow it until they have offered their credentials.
222 223 224 225 226 227 |
# File 'lib/shamu/security/policy.rb', line 222 def when_elevated( &block ) current = @when_elevated @when_elevated = true yield @when_elevated = current end |