Class: Ddr::Auth::Roles::RoleSet

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/ddr/auth/roles/role_set.rb

Overview

Wraps a set of Roles (ActiveTriples::Term)

Instance Method Summary collapse

Instance Method Details

#grant(*roles) ⇒ Object

Grants roles - i.e., adds them to the role set

Note that we reject roles that are included because
ActiveTriples::Term#<< does not support isomorphism. 
https://github.com/ActiveTriples/ActiveTriples/issues/42

Examples:

  • default scope (:resource)

grant type: :curator, person: "bob"

  • explicit scope

grant type: :curator, person: "sue", scope: :policy

Parameters:



20
21
22
# File 'lib/ddr/auth/roles/role_set.rb', line 20

def grant(*roles)
  self << coerce(roles).reject { |r| include?(r) }
end

#granted?(role) ⇒ Boolean

Return true/false depending on whether the role has been granted

Parameters:

Returns:

  • (Boolean)

    whether the role has been granted



27
28
29
# File 'lib/ddr/auth/roles/role_set.rb', line 27

def granted?(role)
  include? coerce(role)
end

#replace(*roles) ⇒ Object

Replace the current roles in the role set with new roles

Parameters:



49
50
51
52
53
# File 'lib/ddr/auth/roles/role_set.rb', line 49

def replace(*roles)
  revoke_all
  # XXX Not sure why we have to use __getobj__ here
  __getobj__.set coerce(roles)
end

#revoke(*roles) ⇒ Object

Revokes roles - i.e., removes them from the role set

Note that we have to destroy resources on the 
ActiveTriples::Term because Term#delete does not
support isomorphism.
https://github.com/ActiveTriples/ActiveTriples/issues/42

Examples:

revoke type: :curator, agent: "bob", scope: :resource

Parameters:



39
40
41
42
43
44
45
# File 'lib/ddr/auth/roles/role_set.rb', line 39

def revoke(*roles)
  coerce(roles).each do |role|
    if role_index = find_index(role)
      self[role_index].destroy
    end
  end
end

#revoke_allObject

Remove all roles from the role set



56
57
58
# File 'lib/ddr/auth/roles/role_set.rb', line 56

def revoke_all          
  delete(*__getobj__)
end

#to_aObject



60
61
62
# File 'lib/ddr/auth/roles/role_set.rb', line 60

def to_a
  map.to_a
end

#where(criteria) ⇒ Object



64
65
66
# File 'lib/ddr/auth/roles/role_set.rb', line 64

def where(criteria)
  query.where(criteria)
end