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

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

Overview

The assignment of a role to an agent within a scope.

Constant Summary collapse

DEFAULT_SCOPE =
Roles::RESOURCE_SCOPE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(args = {}) ⇒ Role Also known as: deserialize

Build a Role instance from hash attributes

Examples:

Role.build type: "Curator", agent: "bob", scope: "resource"

Parameters:

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

    the attributes

Returns:

  • (Role)

    the role



29
30
31
32
33
34
35
36
# File 'lib/ddr/auth/roles/role.rb', line 29

def build(args={})
  new.tap do |role|
    role.attributes = build_attributes(args)
    if role.invalid?
      raise Ddr::Models::Error, "Invalid #{self.name}: #{role.errors.full_messages.join('; ')}"
    end
  end
end

.from_json(json) ⇒ Role

Deserialize a Role from JSON

Parameters:

  • json (String)

    the JSON string

Returns:

  • (Role)

    the role



43
44
45
# File 'lib/ddr/auth/roles/role.rb', line 43

def from_json(json)
  deserialize JSON.parse(json)
end

Instance Method Details

#==(other) ⇒ Boolean

Roles are considered equivalent (== and eql?) if they are of the same type and have the same agent and scope.

Parameters:

  • other (Object)

    the object of comparison

Returns:

  • (Boolean)

    the result



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

def ==(other)
  if self.class == other.class
    self.to_h == other.to_h
  else
    super
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ddr/auth/roles/role.rb', line 77

def eql?(other)
  (self == other) && (hash == other.hash)
end

#hashObject



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

def hash
  to_h.hash
end

#in_policy_scope?Boolean

Returns:

  • (Boolean)


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

def in_policy_scope?
  scope.first == Roles::POLICY_SCOPE
end

#in_resource_scope?Boolean

Returns:

  • (Boolean)


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

def in_resource_scope?
  scope.first == Roles::RESOURCE_SCOPE
end

#inspectObject



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

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

#permissionsArray<Symbol>

Returns the permissions associated with the role

Returns:

  • (Array<Symbol>)

    the permissions



110
111
112
# File 'lib/ddr/auth/roles/role.rb', line 110

def permissions
  Roles.type_map[role_type.first].permissions
end

#to_hObject Also known as: to_hash, serialize



102
103
104
# File 'lib/ddr/auth/roles/role.rb', line 102

def to_h
  as_json
end

#to_sObject



85
86
87
# File 'lib/ddr/auth/roles/role.rb', line 85

def to_s
  to_h.to_s
end