Class: Verifica::Authorizer
- Inherits:
-
Object
- Object
- Verifica::Authorizer
- Defined in:
- lib/verifica/authorizer.rb
Overview
Authorizer is the heart of Verifica. It's an isolated container with no global state which has a list of resource types registered with their companion AclProviders.
Authorizer pairs great with Dependency Injection or can be configured and passed in a way that is compatible with your framework.
Instance Method Summary collapse
-
#allowed_actions(subject, resource, **context) ⇒ Array<Symbol>
Array of actions allowed for
subject
or empty array if none. -
#authorize(subject, resource, action, **context) ⇒ AuthorizationResult
Checks the authorization of a subject to perform an action on a resource.
-
#authorized?(subject, resource, action, **context) ⇒ Boolean
The same as #authorize but returns true/false instead of rising an exception.
-
#initialize(resource_configs) ⇒ Authorizer
constructor
A new instance of Authorizer.
-
#resource_acl(resource, **context) ⇒ Acl
Access Control List for
resource
. -
#resource_config(resource_type) ⇒ ResourceConfiguration
Configuration for
resource_type
. -
#resource_type?(resource_type) ⇒ Boolean
True if
resource_type
is registered inself
.
Constructor Details
#initialize(resource_configs) ⇒ Authorizer
Use Verifica.authorizer instead of this constructor directly
Returns a new instance of Authorizer.
34 35 36 37 |
# File 'lib/verifica/authorizer.rb', line 34 def initialize(resource_configs) @resources = index_resources(resource_configs).freeze freeze end |
Instance Method Details
#allowed_actions(subject, resource, **context) ⇒ Array<Symbol>
Returns array of actions allowed for subject
or empty array if none.
95 96 97 98 99 |
# File 'lib/verifica/authorizer.rb', line 95 def allowed_actions(subject, resource, **context) acl = resource_acl(resource, **context) sids = Verifica.subject_sids(subject) acl.allowed_actions(sids) end |
#authorize(subject, resource, action, **context) ⇒ AuthorizationResult
Checks the authorization of a subject to perform an action on a resource
-
The
subject
is asked for its Security Identifiers (SIDs) bysubject.subject_sids
-
The
resource
is asked for its type byresource.resource_type
-
ACL provider registered for this resource type is asked for Verifica::Acl by #call(resource, **context)
-
ACL is checked whether the
action
is allowed for the subject SIDs
67 68 69 70 71 72 |
# File 'lib/verifica/authorizer.rb', line 67 def (subject, resource, action, **context) result = (subject, resource, action, **context) raise AuthorizationError, result if result.failure? result end |
#authorized?(subject, resource, action, **context) ⇒ Boolean
The same as #authorize but returns true/false instead of rising an exception
80 81 82 |
# File 'lib/verifica/authorizer.rb', line 80 def (subject, resource, action, **context) (subject, resource, action, **context).success? end |
#resource_acl(resource, **context) ⇒ Acl
Returns Access Control List for resource
.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/verifica/authorizer.rb', line 141 def resource_acl(resource, **context) config = config_by_resource(resource) acl = config.acl_provider.call(resource, **context) unless acl.is_a?(Verifica::Acl) type = resource.resource_type raise Error, "'#{type}' resource acl_provider should respond to #call with Acl object but got '#{acl.class}'" end acl end |
#resource_config(resource_type) ⇒ ResourceConfiguration
Returns configuration for resource_type
.
110 111 112 113 114 115 116 117 118 |
# File 'lib/verifica/authorizer.rb', line 110 def resource_config(resource_type) resource_type = resource_type.to_sym config = @resources[resource_type] if config.nil? raise Error, "Unknown resource '#{resource_type}'. Did you forget to register this resource type?" end config end |
#resource_type?(resource_type) ⇒ Boolean
Returns true if resource_type
is registered in self
.
127 128 129 |
# File 'lib/verifica/authorizer.rb', line 127 def resource_type?(resource_type) @resources.key?(resource_type.to_sym) end |