Class: NexusCqrs::Auth::PermissionProvider
- Inherits:
-
Object
- Object
- NexusCqrs::Auth::PermissionProvider
- Defined in:
- lib/nexus_cqrs/auth/permission_provider.rb
Instance Method Summary collapse
-
#for_user(permission_model) ⇒ Hash
Retrieves a list of permissions assigned to a user for ANY entity ID.
-
#for_user_on_entity(permission_model, entity_id) ⇒ Array
Retrieves a list of permissions assigned to a user for a specific entity.
-
#has_permission?(permission_key, permission_model = nil, entity_id = nil) ⇒ Boolean
Returns true if the current user has the requested permission on the requested entity (if passed), or globally.
-
#initialize(user_id, global_permissions) ⇒ PermissionProvider
constructor
A new instance of PermissionProvider.
Constructor Details
#initialize(user_id, global_permissions) ⇒ PermissionProvider
Returns a new instance of PermissionProvider.
7 8 9 10 |
# File 'lib/nexus_cqrs/auth/permission_provider.rb', line 7 def initialize(user_id, ) @user_id = user_id @global_permissions = () end |
Instance Method Details
#for_user(permission_model) ⇒ Hash
Retrieves a list of permissions assigned to a user for ANY entity ID
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nexus_cqrs/auth/permission_provider.rb', line 84 def for_user() return {} if @user_id.nil? = {} # retrieve entity-specific permissions from DB and map to hash .where(user_id: @user_id).each do |p| if [p.entity_id].nil? [p.entity_id] = [p.] else [p.entity_id] << p. end end end |
#for_user_on_entity(permission_model, entity_id) ⇒ Array
Retrieves a list of permissions assigned to a user for a specific entity
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nexus_cqrs/auth/permission_provider.rb', line 54 def for_user_on_entity(, entity_id) return [] if @user_id.nil? # retrieve entity-specific permissions from cached user permissions and map to hash = if ().key?(entity_id) ()[entity_id] .map { |p| { global: false, key: p } } else [] end # Map global permissions to hash = @global_permissions.map { |p| { global: true, key: p } } # Combine hashes and ensure global permissions take priority ( + ).uniq { |p| p[:key] } end |
#has_permission?(permission_key, permission_model = nil, entity_id = nil) ⇒ Boolean
Returns true if the current user has the requested permission on the requested entity (if passed), or globally
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nexus_cqrs/auth/permission_provider.rb', line 20 def (, = nil, entity_id = nil) return false if @user_id.nil? return true if @global_permissions.include?() # check entity-specific permissions unless .nil? # get all permissions for this entity. NOTE: This will be cached per-request. = () # if there are no permissions for this entity and user, return false return false if [entity_id].nil? # if the permission key is in the user's permissions for this entity, return true return true if [entity_id].include?() end false end |