Class: Casbin::Rbac::DefaultRoleManager::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/casbin-ruby/rbac/default_role_manager/role.rb

Overview

represents the data structure for a role in RBAC.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Role

Returns a new instance of Role.



10
11
12
13
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 10

def initialize(name)
  @name = name
  @roles = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 8

def name
  @name
end

#rolesObject

Returns the value of attribute roles.



8
9
10
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 8

def roles
  @roles
end

Instance Method Details

#add_role(role) ⇒ Object



15
16
17
18
19
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 15

def add_role(role)
  return if roles.any? { |rr| rr.name == role.name }

  roles << role
end

#delete_role(role) ⇒ Object



21
22
23
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 21

def delete_role(role)
  roles.delete_if { |rr| rr.name == role.name }
end

#get_rolesObject



33
34
35
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 33

def get_roles
  roles.map(&:name)
end

#has_direct_role(name) ⇒ Object



37
38
39
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 37

def has_direct_role(name)
  roles.any? { |role| role.name == name }
end

#has_role(role_name, hierarchy_level) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 25

def has_role(role_name, hierarchy_level)
  return true if role_name == name
  return false if hierarchy_level.to_i <= 0

  roles.each { |role| return true if role.has_role(role_name, hierarchy_level - 1) }
  false
end

#to_stringObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/casbin-ruby/rbac/default_role_manager/role.rb', line 41

def to_string
  return if roles.empty?

  names = get_roles.join(', ')
  if roles.size == 1
    "#{name} < #{names}"
  else
    "#{name} < (#{names})"
  end
end