Class: Ddr::Managers::RoleManager

Inherits:
Manager
  • Object
show all
Defined in:
lib/ddr/managers/role_manager.rb

Instance Attribute Summary

Attributes inherited from Manager

#object

Instance Method Summary collapse

Methods inherited from Manager

#initialize

Constructor Details

This class inherits a constructor from Ddr::Managers::Manager

Instance Method Details

#grantedObject



8
9
10
# File 'lib/ddr/managers/role_manager.rb', line 8

def granted
  @granted ||= Ddr::Auth::Roles::RoleSet.new(ds.access_role)
end

#index_fieldsHash

Return a hash of role information to index

Returns:

  • (Hash)

    the fields



59
60
61
62
63
64
65
66
67
68
# File 'lib/ddr/managers/role_manager.rb', line 59

def index_fields
  granted.each_with_object({}) do |role, fields|
    scope_field = scope_index_field(role)
    fields[scope_field] ||= []
    fields[scope_field] |= [role.agent_name]
    scope_role_field = scope_role_index_field(role)
    fields[scope_role_field] ||= []
    fields[scope_role_field] << role.agent_name
  end
end

#permissions_in_scope_for_agents(scope, agents) ⇒ Object

Return a list of the permissions granted in scope to any of the agents



23
24
25
# File 'lib/ddr/managers/role_manager.rb', line 23

def permissions_in_scope_for_agents(scope, agents)
  where(scope: scope, agent: agents).map(&:permissions).flatten.uniq
end

#policy_permissions_for_agents(agents) ⇒ Object

Return a list of the permissions granted in policy scope to any of the agents



33
34
35
# File 'lib/ddr/managers/role_manager.rb', line 33

def policy_permissions_for_agents(agents)
  permissions_in_scope_for_agents(:policy, agents)
end

#policy_permissions_for_user(user) ⇒ Object

Return the permissions granted to the user in policy scope (via roles on the object)



43
44
45
# File 'lib/ddr/managers/role_manager.rb', line 43

def policy_permissions_for_user(user)
  policy_permissions_for_agents(user.agents)
end

#resource_permissions_for_agents(agents) ⇒ Object

Return a list of the permissions granted in resource scope to any of the agents



28
29
30
# File 'lib/ddr/managers/role_manager.rb', line 28

def resource_permissions_for_agents(agents)
  permissions_in_scope_for_agents(:resource, agents)
end

#resource_permissions_for_user(user) ⇒ Object

Return the permissions granted to the user in resource scope (via roles on the object)



38
39
40
# File 'lib/ddr/managers/role_manager.rb', line 38

def resource_permissions_for_user(user)
  resource_permissions_for_agents(user.agents)
end

#revoke_policy_rolesObject

Revoke all roles in policy scope



13
14
15
# File 'lib/ddr/managers/role_manager.rb', line 13

def revoke_policy_roles
  revoke *(where(scope: :policy))
end

#revoke_resource_rolesObject

Revoke all role in resource scope



18
19
20
# File 'lib/ddr/managers/role_manager.rb', line 18

def revoke_resource_roles
  revoke *(where(scope: :resource))
end

#role_based_permissions(user) ⇒ Object

Return the permissions granted to the user on the object in resource scope, plus the permissions granted to the user on the object’s admin policy in policy scope



49
50
51
52
53
54
55
# File 'lib/ddr/managers/role_manager.rb', line 49

def role_based_permissions(user)
  perms = resource_permissions_for_user(user)
  if policy = object.admin_policy
    perms |= policy.roles.policy_permissions_for_user(user)
  end
  perms
end