Class: Users::Role

Inherits:
Object show all
Defined in:
lib/users/role.rb

Overview

Entity representing a collection of privileges which may be associated with entities. May be assigned to a user

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Role

Role initializer

Parameters:

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

    hash of options to initialize user with

Options Hash (args):


23
24
25
26
# File 'lib/users/role.rb', line 23

def initialize(args = {})
  @id        = args['id']        || args[:id]
  @privileges = args['privileges'] || args[:privileges] || []
end

Instance Attribute Details

#idObject

String

unique string identifier of the role


14
15
16
# File 'lib/users/role.rb', line 14

def id
  @id
end

#privilegesObject (readonly)

Array<Users::Privilege>

array of privileges associated with the role


17
18
19
# File 'lib/users/role.rb', line 17

def privileges
  @privileges
end

Class Method Details

.json_create(o) ⇒ Object

Create new role from json representation


98
99
100
101
# File 'lib/users/role.rb', line 98

def self.json_create(o)
  role = new(o['data'])
  return role
end

Instance Method Details

#add_privilege(*args) ⇒ Object

Adds the privilege specified by its arguments to the role

Parameters:

  • args (Array<Users::Privilege>, Array<String,String>)

    catch all array of args to use when adding privilege. May take one of two forms specifying the instance of the privilege class itself to add, or an id and entity_id to create the privilege with


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/users/role.rb', line 46

def add_privilege(*args)
  privilege = args[0].is_a?(Users::Privilege) ?
              args[0] : Privilege.new(:id => args.first)

  if privilege.entity_id.nil? && !args[1].nil?
    privilege.entity_id = args[1]
  end

  @privileges << privilege unless privilege.nil? ||
                                   @privileges.include?(privilege) ||
                                  !@privileges.find { |p| p.id == privilege.id && p.entity_id == privilege.entity_id }.nil?
end

#clear_privilegesObject

Clear the privileges the role has


37
38
39
# File 'lib/users/role.rb', line 37

def clear_privileges
  @privileges.clear
end

#has_privilege?(privilege_id) ⇒ true, false

Returns boolean indicating if the role has the specified privilege

Parameters:

  • privilege_id (String)

    id of privilege to lookup in local privileges array

Returns:

  • (true, false)

    indicating if user has / does not have privilege


80
81
82
# File 'lib/users/role.rb', line 80

def has_privilege?(privilege_id)
  has_privilege_on?(privilege_id, nil)
end

#has_privilege_on?(privilege_id, entity_id) ⇒ true, false

Returns boolean indicating if the role has the specified privilege on the specified entity

Parameters:

  • privilege_id (String)

    id of privilege to lookup in local privileges array

  • entity_id (String)

    id of entity to lookup in local privileges array

Returns:

  • (true, false)

    indicating if user has / does not have privilege


72
73
74
# File 'lib/users/role.rb', line 72

def has_privilege_on?(privilege_id, entity_id)
  ! @privileges.find { |p| p.id == privilege_id && p.entity_id == entity_id }.nil?
end

#remove_privilege(privilege_id, entity_id = nil) ⇒ Object

Remove privilege from role

Parameters:

  • privilege_id

    id of the privilege to remove

  • entity_id (defaults to: nil)

    optional id of the entity which to remove privilege to


63
64
65
# File 'lib/users/role.rb', line 63

def remove_privilege(privilege_id, entity_id=nil)
  @privileges.reject! { |p| p.id == privilege_id && p.entity_id == entity_id }
end

#to_json(*a) ⇒ Object

Convert role to json representation and return it


90
91
92
93
94
95
# File 'lib/users/role.rb', line 90

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => {:id => id, :privileges => privileges}
  }.to_json(*a)
end

#to_sObject

Convert role to human readable string and return it


85
86
87
# File 'lib/users/role.rb', line 85

def to_s
  "role-#{@id}"
end

#update(new_role) ⇒ Object

Update this roles's properties from other roles.

Parameters:

  • new_role (Users::Role)

    role from which to copy values from


31
32
33
34
# File 'lib/users/role.rb', line 31

def update(new_role)
  #@id = new_role.id
  @privileges = new_role.privileges
end