Class: IQ::ACL::Basic
- Inherits:
-
Object
- Object
- IQ::ACL::Basic
- Defined in:
- lib/iq/acl/basic.rb
Overview
This class provides a really simple way of handling access control. By simply supplying a hash of paths with user privileges for each of them, a powerful ACL system can be created. Wildcards (in this case asterisks) can be used to denote global rules.
Instance Method Summary collapse
-
#authenticate(user, path) ⇒ nil, Object
Returns the rights that a user has for a given path.
-
#authenticate!(user, path, &block) ⇒ Object
Returns the rights that a user has for a given path.
-
#initialize(permissions) ⇒ Basic
constructor
Returns a new instance to be authenticated against.
Constructor Details
#initialize(permissions) ⇒ Basic
Returns a new instance to be authenticated against.
60 61 62 63 |
# File 'lib/iq/acl/basic.rb', line 60 def initialize() raise ArgumentError, 'Must supply permissions as a hash' unless .is_a?(Hash) @permissions = end |
Instance Method Details
#authenticate(user, path) ⇒ nil, Object
Returns the rights that a user has for a given path. When the user has no access to the given path, nil is returned.
When a block is supplied the user rights are yielded as the block parameter and the block is expected to return true when the rights are sufficient.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/iq/acl/basic.rb', line 75 def authenticate(user, path) raise ArgumentError, 'Path must be a string' unless path.is_a?(String) segments = path.split('/') rights = until segments.empty? if rights = [segments.join('/')] access = rights[user] || rights['*'] return nil if (rights.has_key?(user) || rights.has_key?('*')) && access.nil? break access if access end segments.pop end || (global = ['*']) && (global[user] || global['*']) || nil return nil if block_given? && (yield(rights) != true) rights end |
#authenticate!(user, path, &block) ⇒ Object
Returns the rights that a user has for a given path. When the user has no access to the given path, an IQ::ACL::AccessDeniedError is raised. When a block is supplied the user rights are yielded as the block parameter and the block is expected to return true when the rights are sufficient.
102 103 104 |
# File 'lib/iq/acl/basic.rb', line 102 def authenticate!(user, path, &block) authenticate(user, path, &block) || raise(IQ::ACL::AccessDeniedError, 'User does not have access to path') end |