Class: Casbin::Rbac::DefaultRoleManager::RoleManager
- Inherits:
-
RoleManager
- Object
- RoleManager
- Casbin::Rbac::DefaultRoleManager::RoleManager
- Defined in:
- lib/casbin-ruby/rbac/default_role_manager/role_manager.rb
Overview
provides a default implementation for the RoleManager interface
Instance Attribute Summary collapse
-
#all_roles ⇒ Object
Returns the value of attribute all_roles.
-
#domain_matching_func ⇒ Object
Returns the value of attribute domain_matching_func.
-
#has_domain_pattern ⇒ Object
Returns the value of attribute has_domain_pattern.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#matching_func ⇒ Object
Returns the value of attribute matching_func.
-
#max_hierarchy_level ⇒ Object
Returns the value of attribute max_hierarchy_level.
Instance Method Summary collapse
- #add_domain_matching_func(fn) ⇒ Object
- #add_link(name1, name2, *domain) ⇒ Object
- #add_matching_func(fn) ⇒ Object
- #clear ⇒ Object
- #create_role(name) ⇒ Object
- #delete_link(name1, name2, *domain) ⇒ Object
-
#get_roles(name, *domain) ⇒ Object
gets the roles that a subject inherits.
-
#get_users(name, *domain) ⇒ Object
gets the users that inherits a subject.
- #has_link(name1, name2, *domain) ⇒ Object
- #has_role(name) ⇒ Object
-
#initialize(max_hierarchy_level, logger: Logger.new($stdout)) ⇒ RoleManager
constructor
A new instance of RoleManager.
- #print_roles ⇒ Object
Constructor Details
#initialize(max_hierarchy_level, logger: Logger.new($stdout)) ⇒ RoleManager
Returns a new instance of RoleManager.
15 16 17 18 19 20 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 15 def initialize(max_hierarchy_level, logger: Logger.new($stdout)) super() @logger = logger @all_roles = {} @max_hierarchy_level = max_hierarchy_level end |
Instance Attribute Details
#all_roles ⇒ Object
Returns the value of attribute all_roles.
12 13 14 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 12 def all_roles @all_roles end |
#domain_matching_func ⇒ Object
Returns the value of attribute domain_matching_func.
12 13 14 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 12 def domain_matching_func @domain_matching_func end |
#has_domain_pattern ⇒ Object
Returns the value of attribute has_domain_pattern.
12 13 14 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 12 def has_domain_pattern @has_domain_pattern end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 13 def logger @logger end |
#matching_func ⇒ Object
Returns the value of attribute matching_func.
12 13 14 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 12 def matching_func @matching_func end |
#max_hierarchy_level ⇒ Object
Returns the value of attribute max_hierarchy_level.
12 13 14 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 12 def max_hierarchy_level @max_hierarchy_level end |
Instance Method Details
#add_domain_matching_func(fn) ⇒ Object
26 27 28 29 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 26 def add_domain_matching_func(fn) self.has_domain_pattern = true self.domain_matching_func = fn end |
#add_link(name1, name2, *domain) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 53 def add_link(name1, name2, *domain) names = names_by_domain(name1, name2, *domain) role1 = create_role(names[0]) role2 = create_role(names[1]) role1.add_role(role2) end |
#add_matching_func(fn) ⇒ Object
22 23 24 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 22 def add_matching_func(fn) @matching_func = fn end |
#clear ⇒ Object
49 50 51 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 49 def clear @all_roles = {} end |
#create_role(name) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 38 def create_role(name) all_roles[name] = Role.new(name) unless all_roles.key?(name) if matching_func all_roles.each do |key, role| all_roles[name].add_role(role) if matching_func.call(name, key) && name != key end end all_roles[name] end |
#delete_link(name1, name2, *domain) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 61 def delete_link(name1, name2, *domain) names = names_by_domain(name1, name2, *domain) raise 'error: name1 or name2 does not exist' if !has_role(names[0]) || !has_role(names[1]) role1 = create_role(names[0]) role2 = create_role(names[1]) role1.delete_role(role2) end |
#get_roles(name, *domain) ⇒ Object
gets the roles that a subject inherits. domain is a prefix to the roles.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 92 def get_roles(name, *domain) name = name_by_domain(name, *domain) return [] unless has_role(name) roles = create_role(name).get_roles if domain.size == 1 roles.each_with_index { |value, index| roles[index] = value[domain[0].size + 2..value.size] } end roles end |
#get_users(name, *domain) ⇒ Object
gets the users that inherits a subject. domain is an unreferenced parameter here, may be used in other implementations.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 106 def get_users(name, *domain) name = name_by_domain(name, *domain) return [] unless has_role(name) all_roles.map do |_key, role| next unless role.has_direct_role(name) if domain.size == 1 role.name[domain[0].size + 2..role.name.size] else role.name end end.compact end |
#has_link(name1, name2, *domain) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 71 def has_link(name1, name2, *domain) names = names_by_domain(name1, name2, *domain) return true if names[0] == names[1] return false if !has_role(names[0]) || !has_role(names[1]) if matching_func.nil? role1 = create_role names[0] role1.has_role names[1], max_hierarchy_level else all_roles.each do |key, role| return true if matching_func.call(names[0], key) && role.has_role(names[1], max_hierarchy_level) end false end end |
#has_role(name) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 31 def has_role(name) return all_roles.key?(name) if matching_func.nil? all_roles.each_key { |key| return true if matching_func.call(name, key) } false end |
#print_roles ⇒ Object
121 122 123 124 |
# File 'lib/casbin-ruby/rbac/default_role_manager/role_manager.rb', line 121 def print_roles line = all_roles.map { |_key, role| role.to_string }.compact logger.info(line.join(', ')) end |