Class: Ddr::Auth::Roles::Role Abstract

Inherits:
ActiveTriples::Resource
  • Object
show all
Includes:
RDF::Isomorphic
Defined in:
lib/ddr/auth/roles/role.rb

Overview

This class is abstract.

An abstract class representing the assignment of a role to an agent within a scope.

Direct Known Subclasses

Contributor, Curator, Downloader, Editor, MetadataEditor, Viewer

Constant Summary collapse

AGENT_TYPES =
[:person, :group, :agent]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.permissionsObject

Returns the value of attribute permissions.



25
26
27
# File 'lib/ddr/auth/roles/role.rb', line 25

def permissions
  @permissions
end

Class Method Details

.build(args = {}) ⇒ Ddr::Auth::Roles::Role

Builds a new role object from the hash arguments

Parameters:

  • args (Hash) (defaults to: {})

    a hash of options

Returns:



34
35
36
37
38
39
40
41
42
# File 'lib/ddr/auth/roles/role.rb', line 34

def build(args={})
  new.tap do |role| 
    agent_key = AGENT_TYPES.detect { |k| args.key?(k) }
    agent_class = Ddr::Auth.get_agent_class(agent_key)
    role.agent = agent_class.build(args[agent_key])
    role.scope = args.fetch(:scope, Scopes::DEFAULT)
    # validate!
  end
end

.has_permission(*permissions) ⇒ Object



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

def has_permission(*permissions)
  self.permissions += permissions.map { |p| Ddr::Auth::Permission.get(p) }
end

.inherited(subclass) ⇒ Object



52
53
54
# File 'lib/ddr/auth/roles/role.rb', line 52

def inherited(subclass)            
  subclass.permissions = []
end

.role_typeSymbol

Return the role “type” (not RDF type) – i.e., a symbol for the role class

This should be the inverse operation of Ddr::Auth::Roles.get_role_class(type)

Examples:

Ddr::Auth::Roles::Curator.role_type => :curator

Returns:

  • (Symbol)

    the role type



48
49
50
# File 'lib/ddr/auth/roles/role.rb', line 48

def role_type
  @role_type ||= self.name.split("::").last.underscore.to_sym
end

Instance Method Details

#==(other) ⇒ Boolean

Roles are considered equivalent if the RDF graphs are isomorphic – i.e., if the RDF type, Agent name, scope are all equal.

Returns:

  • (Boolean)

    the result



61
62
63
# File 'lib/ddr/auth/roles/role.rb', line 61

def ==(other)
  isomorphic_with? other
end

#agent_nameString?

Return the agent name associated with the role

Returns:

  • (String, nil)

    the agent name, or nil if there is no agent



80
81
82
83
84
# File 'lib/ddr/auth/roles/role.rb', line 80

def agent_name
  if ag = get_agent
    ag.agent_name
  end
end

#agent_typeSymbol?

Return the agent type associated with the role

Returns:

  • (Symbol, nil)

    the agent type, or nil if there is no agent



88
89
90
91
92
# File 'lib/ddr/auth/roles/role.rb', line 88

def agent_type
  if ag = get_agent
    ag.agent_type
  end
end

#get_agentObject



119
120
121
122
123
# File 'lib/ddr/auth/roles/role.rb', line 119

def get_agent
  if agent.present?
    agent.first
  end
end

#group_agent?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ddr/auth/roles/role.rb', line 98

def group_agent?
  agent_type == :group
end

#inspectObject



69
70
71
# File 'lib/ddr/auth/roles/role.rb', line 69

def inspect
  "#<#{self.class.name} agent=#{agent.first.to_h.inspect}, scope=#{scope.first.inspect}>"
end

#permissionsObject

See Also:



115
116
117
# File 'lib/ddr/auth/roles/role.rb', line 115

def permissions
  self.class.permissions
end

#person_agent?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ddr/auth/roles/role.rb', line 94

def person_agent?
  agent_type == :person
end

#role_typeObject

See Also:



74
75
76
# File 'lib/ddr/auth/roles/role.rb', line 74

def role_type
  self.class.role_type
end

#to_hObject Also known as: to_hash



102
103
104
105
106
107
108
109
110
111
# File 'lib/ddr/auth/roles/role.rb', line 102

def to_h
  val = { 
    type: role_type,
    scope: scope.first
  }
  if agent.present?
    val[agent_type] = agent_name 
  end
  val
end

#to_sObject



65
66
67
# File 'lib/ddr/auth/roles/role.rb', line 65

def to_s
  to_h.to_s
end