casbin-ruby 1.0.7 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 432f8bf4cd2d4ff1fad9a35f611ecc47db212177d88939415cafac07913cabd3
4
- data.tar.gz: bb0284c6049cae5551fb46943abc3308a21ff10fa7a57c0bdebcb2be92dc02cd
3
+ metadata.gz: 0a6fa41ab5193602d852ba7652d0d92775f1a26243471d154188aca17a5a3a67
4
+ data.tar.gz: 19174a79a646b395960fd842bec794f8a202d283396c802d872ce7e88914763a
5
5
  SHA512:
6
- metadata.gz: b51625e08d8433a2a3d43c71a62f5fc08c853dc56665a694627b90d348aaecad20ffbe9e24db44144cafc204df234ddce33c97073d1fa033b90bc31b8aa57031
7
- data.tar.gz: 9ca251806e1b6944b2fc13ae028edbf28c66c1481e0ec5f5fe3d24e305721eba30b55879c667a0d2c16396963dea01cfd59251a86c6163cfdc0c46842df5a6c7
6
+ metadata.gz: 9c40a77ea91e4addeba67652af4fe0bd1af3151f67e2bdfc3625f4c90e295a643d3f2d221446957ebd3a1f4321e3a1869a822a31102c5a6cdf78da3dd572e35d
7
+ data.tar.gz: 29c3ff1985b8e1480ce596763ce621790cf19f6676649efc6d6e4ac8bb06c476f3d8618db2e72a89a40e1f277da36a5eba5ba598040a4dd3ada102b91fe9c390
data/README.md CHANGED
@@ -131,7 +131,7 @@ What Casbin does NOT do:
131
131
  ## Installation
132
132
 
133
133
  ```
134
- gem 'casbin', github: 'evrone/casbin-ruby'
134
+ gem 'casbin-ruby'
135
135
  ```
136
136
 
137
137
  ## Documentation
@@ -151,8 +151,21 @@ https://casbin.org/docs/en/tutorials
151
151
  1. New a Casbin enforcer with a model file and a policy file:
152
152
 
153
153
  ```ruby
154
- # TODO: correct `require`
155
- require 'casbin'
154
+ require 'casbin-ruby'
155
+ Casbin::Config.setup do |config|
156
+ config.model = "path/to/model.conf" # default: nil
157
+ config.adapter = "path/to/policy.csv" # default: nil
158
+ config.watcher = Casbin::SomeWatcher # default: nil
159
+ config.logger = Logger.new($stdout) # default: Logger.new($stdout, level: :error)
160
+ end
161
+
162
+ enforcer = Casbin::Enforcer.new
163
+ ```
164
+
165
+ OR
166
+
167
+ ```ruby
168
+ require 'casbin-ruby'
156
169
  enforcer = Casbin::Enforcer.new("path/to/model.conf", "path/to/policy.csv")
157
170
  ```
158
171
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Casbin
6
+ module Config
7
+ class << self
8
+ attr_writer :logger
9
+ attr_accessor :adapter, :model, :watcher
10
+
11
+ def setup
12
+ yield self
13
+ end
14
+
15
+ def logger
16
+ @logger ||= ::Logger.new($stdout, level: :error)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -9,46 +9,50 @@ require 'casbin-ruby/rbac/default_role_manager/role_manager'
9
9
  require 'casbin-ruby/util'
10
10
  require 'casbin-ruby/util/builtin_operators'
11
11
  require 'casbin-ruby/util/evaluator'
12
-
13
- require 'logger'
12
+ require 'casbin-ruby/logger'
13
+ require 'casbin-ruby/config'
14
14
 
15
15
  module Casbin
16
16
  # CoreEnforcer defines the core functionality of an enforcer.
17
17
  # get_attr/set_attr methods is ported from Python as attr/attr=
18
18
  class CoreEnforcer
19
- def initialize(model = nil, adapter = nil, logger: Logger.new($stdout))
19
+ def initialize(model = nil, adapter = nil, watcher = nil)
20
+ model ||= Config.model
21
+ adapter ||= Config.adapter
22
+ @watcher = watcher || Config.watcher
23
+
20
24
  if model.is_a? String
21
25
  if adapter.is_a? String
22
- init_with_file(model, adapter, logger: logger)
26
+ init_with_file(model, adapter)
23
27
  else
24
- init_with_adapter(model, adapter, logger: logger)
28
+ init_with_adapter(model, adapter)
25
29
  end
26
30
  elsif adapter.is_a? String
27
31
  raise 'Invalid parameters for enforcer.'
28
32
  else
29
- init_with_model_and_adapter(model, adapter, logger: logger)
33
+ init_with_model_and_adapter(model, adapter)
30
34
  end
31
35
  end
32
36
 
33
- attr_accessor :adapter, :auto_build_role_links, :auto_save, :effector, :enabled, :watcher, :rm_map
34
- attr_reader :model
37
+ attr_accessor :auto_build_role_links, :auto_save, :effector, :enabled, :rm_map
38
+ attr_reader :adapter, :model, :watcher
35
39
 
36
40
  # initializes an enforcer with a model file and a policy file.
37
- def init_with_file(model_path, policy_path, logger: Logger.new($stdout))
41
+ def init_with_file(model_path, policy_path)
38
42
  a = Persist::Adapters::FileAdapter.new(policy_path)
39
- init_with_adapter(model_path, a, logger: logger)
43
+ init_with_adapter(model_path, a)
40
44
  end
41
45
 
42
46
  # initializes an enforcer with a database adapter.
43
- def init_with_adapter(model_path, adapter = nil, logger: Logger.new($stdout))
47
+ def init_with_adapter(model_path, adapter = nil)
44
48
  m = new_model(model_path)
45
- init_with_model_and_adapter(m, adapter, logger: logger)
49
+ init_with_model_and_adapter(m, adapter)
46
50
 
47
51
  self.model_path = model_path
48
52
  end
49
53
 
50
54
  # initializes an enforcer with a model and a database adapter.
51
- def init_with_model_and_adapter(m, adapter = nil, logger: Logger.new($stdout))
55
+ def init_with_model_and_adapter(m, adapter = nil)
52
56
  if !m.is_a?(Model::Model) || (!adapter.nil? && !adapter.is_a?(Persist::Adapter))
53
57
  raise StandardError, 'Invalid parameters for enforcer.'
54
58
  end
@@ -59,15 +63,15 @@ module Casbin
59
63
  model.print_model
60
64
  self.fm = Model::FunctionMap.load_function_map
61
65
 
62
- init(logger: logger)
66
+ init
63
67
 
64
68
  # Do not initialize the full policy when using a filtered adapter
65
69
  load_policy if adapter && !filtered?
66
70
  end
67
71
 
68
72
  # creates a model.
69
- def self.new_model(path = '', text = '', logger: Logger.new($stdout))
70
- m = Model::Model.new logger: logger
73
+ def self.new_model(path = '', text = '')
74
+ m = Model::Model.new
71
75
  if path.length.positive?
72
76
  m.load_model(path)
73
77
  else
@@ -291,13 +295,13 @@ module Casbin
291
295
  protected
292
296
 
293
297
  attr_accessor :model_path, :fm, :auto_motify_watcher
294
- attr_reader :logger
295
298
 
296
299
  private
297
300
 
298
301
  attr_accessor :matcher_map
302
+ attr_writer :adapter
299
303
 
300
- def init(logger: Logger.new($stdout))
304
+ def init
301
305
  self.rm_map = {}
302
306
  self.effector = Effect::DefaultEffector.get_effector(model.model['e']['e'].value)
303
307
 
@@ -305,8 +309,6 @@ module Casbin
305
309
  self.auto_save = true
306
310
  self.auto_build_role_links = true
307
311
 
308
- @logger = logger
309
-
310
312
  init_rm_map
311
313
  end
312
314
 
@@ -338,10 +340,10 @@ module Casbin
338
340
  req_str = "Request: #{rvals.map(&:to_s).join ', '} ---> #{result}"
339
341
 
340
342
  if result
341
- logger.info(req_str)
343
+ Logger.info(req_str)
342
344
  else
343
345
  # leaving this in error for now, if it's very noise this can be changed to info or debug
344
- logger.error(req_str)
346
+ Logger.error(req_str)
345
347
  end
346
348
  end
347
349
 
@@ -349,7 +351,7 @@ module Casbin
349
351
  return unless model.model.keys.include?('g')
350
352
 
351
353
  model.model['g'].each_key do |ptype|
352
- rm_map[ptype] = Rbac::DefaultRoleManager::RoleManager.new(10, logger: logger)
354
+ rm_map[ptype] = Rbac::DefaultRoleManager::RoleManager.new(10)
353
355
  end
354
356
  end
355
357
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'casbin-ruby/config'
4
+
5
+ module Casbin
6
+ module Logger
7
+ module_function
8
+
9
+ def info(value)
10
+ Config.logger.info(value)
11
+ end
12
+
13
+ def error(value)
14
+ Config.logger.error(value)
15
+ end
16
+ end
17
+ end
@@ -102,7 +102,7 @@ module Casbin
102
102
  if params.size == 1 && params[0].is_a?(Array)
103
103
  model.has_policy('p', ptype, params[0])
104
104
  else
105
- model.has_policy('p', ptype, [params])
105
+ model.has_policy('p', ptype, params)
106
106
  end
107
107
  end
108
108
 
@@ -130,7 +130,7 @@ module Casbin
130
130
  if params.size == 1 && params[0].is_a?(Array)
131
131
  parent_add_policy('p', ptype, params[0])
132
132
  else
133
- parent_add_policy('p', ptype, [params])
133
+ parent_add_policy('p', ptype, params)
134
134
  end
135
135
  end
136
136
 
@@ -182,7 +182,7 @@ module Casbin
182
182
  if params.size == 1 && params[0].is_a?(Array)
183
183
  parent_remove_policy('p', ptype, params[0])
184
184
  else
185
- parent_remove_policy('p', ptype, [params])
185
+ parent_remove_policy('p', ptype, params)
186
186
  end
187
187
  end
188
188
 
@@ -206,7 +206,7 @@ module Casbin
206
206
  if params.size == 1 && params[0].is_a?(Array)
207
207
  model.has_policy('g', ptype, params[0])
208
208
  else
209
- model.has_policy('g', ptype, [params])
209
+ model.has_policy('g', ptype, params)
210
210
  end
211
211
  end
212
212
 
@@ -235,7 +235,7 @@ module Casbin
235
235
  rule_added = if params.size == 1 && params[0].is_a?(Array)
236
236
  parent_add_policy('g', ptype, params[0])
237
237
  else
238
- parent_add_policy('g', ptype, [params])
238
+ parent_add_policy('g', ptype, params)
239
239
  end
240
240
 
241
241
  auto_build_role_links ? build_role_links : rule_added
@@ -271,7 +271,7 @@ module Casbin
271
271
  rule_added = if params.size == 1 && params[0].is_a?(Array)
272
272
  parent_remove_policy('g', ptype, params[0])
273
273
  else
274
- parent_remove_policy('g', ptype, [params])
274
+ parent_remove_policy('g', ptype, params)
275
275
  end
276
276
 
277
277
  auto_build_role_links ? build_role_links : rule_added
@@ -1,19 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
3
+ require 'casbin-ruby/logger'
4
4
 
5
5
  module Casbin
6
6
  module Model
7
7
  class Assertion
8
8
  attr_accessor :key, :value, :tokens, :policy, :rm
9
- attr_reader :logger
10
9
 
11
10
  def initialize(hash = {})
12
11
  @key = hash[:key].to_s
13
12
  @value = hash[:value].to_s
14
13
  @tokens = [*hash[:tokens]]
15
14
  @policy = [*hash[:policy]]
16
- @logger = hash[:logger] || Logger.new($stdout)
17
15
  end
18
16
 
19
17
  def build_role_links(rm)
@@ -24,7 +22,7 @@ module Casbin
24
22
  raise 'grouping policy elements do not meet role definition' if rule.size < count
25
23
 
26
24
  rm.add_link(*rule)
27
- logger.info("Role links for: #{key}")
25
+ Logger.info("Role links for: #{key}")
28
26
  rm.print_roles
29
27
  end
30
28
  end
@@ -4,6 +4,7 @@ require 'casbin-ruby/model/policy'
4
4
  require 'casbin-ruby/model/assertion'
5
5
  require 'casbin-ruby/config/config'
6
6
  require 'casbin-ruby/util'
7
+ require 'casbin-ruby/logger'
7
8
 
8
9
  module Casbin
9
10
  module Model
@@ -29,7 +30,7 @@ module Casbin
29
30
  def add_def(sec, key, value)
30
31
  return false if value == ''
31
32
 
32
- ast = Assertion.new(key: key, value: value, logger: logger)
33
+ ast = Assertion.new(key: key, value: value)
33
34
  %w[r p].include?(sec) ? ast_tokens_set(ast, key) : model_sec_set(ast)
34
35
 
35
36
  model[sec] ||= {}
@@ -37,11 +38,11 @@ module Casbin
37
38
  end
38
39
 
39
40
  def print_model
40
- logger.info 'Model:'
41
+ Logger.info 'Model:'
41
42
 
42
43
  model.each do |k, v|
43
44
  v.each do |i, j|
44
- logger.info "#{k}.#{i}: #{j.value}"
45
+ Logger.info "#{k}.#{i}: #{j.value}"
45
46
  end
46
47
  end
47
48
  end
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
3
+ require 'casbin-ruby/logger'
4
4
 
5
5
  module Casbin
6
6
  module Model
7
7
  class Policy
8
- attr_reader :model, :logger
8
+ attr_reader :model
9
9
 
10
- def initialize(logger: Logger.new($stdout))
10
+ def initialize
11
11
  @model = {}
12
- @logger = logger
13
12
  end
14
13
 
15
14
  # initializes the roles in RBAC.
@@ -24,13 +23,13 @@ module Casbin
24
23
 
25
24
  # Log using info
26
25
  def print_policy
27
- logger.info 'Policy:'
26
+ Logger.info 'Policy:'
28
27
 
29
28
  %w[p g].each do |sec|
30
29
  next unless model.key? sec
31
30
 
32
31
  model[sec].each do |key, ast|
33
- logger.info "#{key} : #{ast.value} : #{ast.policy}"
32
+ Logger.info "#{key} : #{ast.value} : #{ast.policy}"
34
33
  end
35
34
  end
36
35
  end
@@ -112,6 +111,13 @@ module Casbin
112
111
  true
113
112
  end
114
113
 
114
+ # gets rules based on field filters from a policy.
115
+ def get_filtered_policy(sec, ptype, field_index, *field_values)
116
+ model[sec][ptype].policy.select do |rule|
117
+ field_values.select { |value| rule[field_index] == value }.any?
118
+ end.compact
119
+ end
120
+
115
121
  # removes policy rules based on field filters from the model.
116
122
  def remove_filtered_policy(sec, ptype, field_index, *field_values)
117
123
  return false unless model.key?(sec)
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
4
3
  require 'casbin-ruby/rbac/role_manager'
5
4
  require 'casbin-ruby/rbac/default_role_manager/role'
5
+ require 'casbin-ruby/logger'
6
6
 
7
7
  module Casbin
8
8
  module Rbac
@@ -10,11 +10,9 @@ module Casbin
10
10
  # provides a default implementation for the RoleManager interface
11
11
  class RoleManager < Rbac::RoleManager
12
12
  attr_accessor :all_roles, :max_hierarchy_level, :matching_func, :has_domain_pattern, :domain_matching_func
13
- attr_reader :logger
14
13
 
15
- def initialize(max_hierarchy_level, logger: Logger.new($stdout))
14
+ def initialize(max_hierarchy_level)
16
15
  super()
17
- @logger = logger
18
16
  @all_roles = {}
19
17
  @max_hierarchy_level = max_hierarchy_level
20
18
  end
@@ -120,7 +118,7 @@ module Casbin
120
118
 
121
119
  def print_roles
122
120
  line = all_roles.map { |_key, role| role.to_string }.compact
123
- logger.info(line.join(', '))
121
+ Logger.info(line.join(', '))
124
122
  end
125
123
 
126
124
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Casbin
4
- VERSION = '1.0.7'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/casbin-ruby.rb CHANGED
@@ -4,6 +4,7 @@ module Casbin
4
4
  require 'casbin-ruby/version'
5
5
  require 'casbin-ruby/enforcer'
6
6
  require 'casbin-ruby/synced_enforcer'
7
+ require 'casbin-ruby/config'
7
8
 
8
9
  module Persist
9
10
  require 'casbin-ruby/persist/adapter'
@@ -29,6 +29,11 @@ describe Casbin::Enforcer do
29
29
  expect(enf.model).not_to be_nil
30
30
  end
31
31
 
32
+ it '#get_filtered_policy' do
33
+ expect(enf.enforce('alice', 'data1', 'read')).to be_truthy
34
+ expect(enf.get_filtered_policy(0, 'alice')).to match_array([%w[alice data1 read]])
35
+ end
36
+
32
37
  it '#remove_filtered_policy' do
33
38
  expect(enf.enforce('alice', 'data1', 'read')).to be_truthy
34
39
  enf.remove_filtered_policy(1, 'data1')
@@ -38,6 +43,63 @@ describe Casbin::Enforcer do
38
43
  enf.remove_filtered_policy(2, 'write')
39
44
  expect(enf.enforce('bob', 'data2', 'write')).to be_falsey
40
45
  end
46
+
47
+ it '#add_policy' do
48
+ expect(enf.enforce('alice', 'data3', 'read')).to be_falsey
49
+ enf.add_policy('alice', 'data3', 'read')
50
+ enf.add_policy('alice', 'data4', 'read')
51
+ enf.add_policy(%w[alice data5 read])
52
+ expect(enf.enforce('alice', 'data3', 'read')).to be_truthy
53
+ expect(enf.enforce('alice', 'data4', 'read')).to be_truthy
54
+ expect(enf.enforce('alice', 'data5', 'read')).to be_truthy
55
+ end
56
+
57
+ it '#remove_policy' do
58
+ enf.add_policy('alice', 'data3', 'read')
59
+ expect(enf.enforce('alice', 'data3', 'read')).to be_truthy
60
+ enf.remove_policy('alice', 'data3', 'read')
61
+ expect(enf.enforce('alice', 'data3', 'read')).to be_falsey
62
+ enf.add_policy('alice', 'data3', 'read')
63
+ expect(enf.enforce('alice', 'data3', 'read')).to be_truthy
64
+ enf.remove_policy(%w[alice data3 read])
65
+ expect(enf.enforce('alice', 'data3', 'read')).to be_falsey
66
+ end
67
+
68
+ # rubocop:disable RSpec/RepeatedExample
69
+ it '#delete_permission' do
70
+ # TODO: Add support this method
71
+ # expect(enf.enforce('bob', 'data2', 'write')).to be_truthy
72
+ # expect(enf.enforce('data2_admin', 'data2', 'read')).to be_truthy
73
+ # expect(enf.enforce('data2_admin', 'data2', 'write')).to be_truthy
74
+ # expect(enf.delete_permission('data2')).to be_truthy
75
+ # expect(enf.enforce('bob', 'data2', 'write')).to be_falsey
76
+ # expect(enf.enforce('data2_admin', 'data2', 'read')).to be_falsey
77
+ # expect(enf.enforce('data2_admin', 'data2', 'write')).to be_falsey
78
+ end
79
+
80
+ it '#delete_permissions_for_user' do
81
+ # TODO: Add support this method
82
+ # expect(enf.enforce('alice', 'data1', 'read')).to be_truthy
83
+ # expect(enf.delete_permissions_for_user('alice')).to be_truthy
84
+ # expect(enf.enforce('alice', 'data1', 'read')).to be_falsey
85
+ end
86
+
87
+ it '#get_permissions_for_user' do
88
+ # TODO: Add support this method
89
+ # expect(enf.get_permissions_for_user('alice')).to match_array([%w[alice data1 read],
90
+ # %w[data2_admin data2 read],
91
+ # %w[data2_admin data2 write]])
92
+ end
93
+ # rubocop:enable RSpec/RepeatedExample
94
+
95
+ it '#has_permission_for_user' do
96
+ expect(enf.has_permission_for_user('alice', 'data1', 'read')).to be_truthy
97
+ expect(enf.has_permission_for_user('alice', 'data1', 'write')).to be_falsey
98
+ end
99
+
100
+ it '#get_implicit_permissions_for_user' do
101
+ expect(enf.get_implicit_permissions_for_user('alice')).to match_array([%w[alice data1 read]])
102
+ end
41
103
  end
42
104
 
43
105
  describe 'basic without spaces' do
@@ -150,6 +212,80 @@ describe Casbin::Enforcer do
150
212
  expect(enf.enforce('alice', 'data2', 'write')).to be_truthy
151
213
  expect(enf.enforce('bogus', 'data2', 'write')).to be_falsey
152
214
  end
215
+
216
+ it '#add_grouping_policy' do
217
+ expect(enf.enforce('alice', 'data3', 'read')).to be_falsey
218
+ enf.add_policy('base', 'data3', 'read')
219
+ enf.add_policy(%w[alice data4 read])
220
+ enf.add_grouping_policy('alice', 'base')
221
+ enf.add_grouping_policy(%w[bob base])
222
+ expect(enf.enforce('alice', 'data3', 'read')).to be_truthy
223
+ expect(enf.enforce('bob', 'data3', 'read')).to be_truthy
224
+ expect(enf.enforce('alice', 'data4', 'read')).to be_truthy
225
+ expect(enf.enforce('bob', 'data4', 'read')).to be_falsey
226
+ end
227
+
228
+ it '#get_roles_for_user' do
229
+ expect(enf.get_roles_for_user('alice')).to match_array(['data2_admin'])
230
+ expect(enf.get_roles_for_user('bob')).to match_array([])
231
+ end
232
+
233
+ it '#get_users_for_role' do
234
+ expect(enf.get_users_for_role('data2_admin')).to match_array(['alice'])
235
+ expect(enf.get_users_for_role('data1_admin')).to match_array([])
236
+ end
237
+
238
+ it '#has_role_for_user' do
239
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_truthy
240
+ expect(enf.has_role_for_user('bob', 'data2_admin')).to be_falsey
241
+ end
242
+
243
+ it '#add_role_for_user' do
244
+ expect(enf.has_role_for_user('alice', 'manager')).to be_falsey
245
+ expect(enf.add_role_for_user('alice', 'manager')).to be_truthy
246
+ expect(enf.has_role_for_user('alice', 'manager')).to be_truthy
247
+ end
248
+
249
+ it '#delete_role_for_user' do
250
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_truthy
251
+ expect(enf.delete_role_for_user('alice', 'data2_admin')).to be_truthy
252
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_falsey
253
+ end
254
+
255
+ it '#delete_roles_for_user' do
256
+ enf.add_grouping_policy('alice', 'base')
257
+ expect(enf.delete_roles_for_user('alice')).to be_truthy
258
+ expect(enf.get_roles_for_user('alice')).to match_array([])
259
+ end
260
+
261
+ it '#delete_user' do
262
+ expect(enf.enforce('alice', 'data1', 'read')).to be_truthy
263
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_truthy
264
+ expect(enf.delete_user('alice')).to be_truthy
265
+ expect(enf.enforce('alice', 'data1', 'read')).to be_falsey
266
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_falsey
267
+ end
268
+
269
+ it '#delete_role' do
270
+ expect(enf.enforce('data2_admin', 'data2', 'read')).to be_truthy
271
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_truthy
272
+ expect(enf.delete_role('data2_admin')).to be_truthy
273
+ expect(enf.enforce('data2_admin', 'data2', 'read')).to be_falsey
274
+ expect(enf.has_role_for_user('alice', 'data2_admin')).to be_falsey
275
+ end
276
+
277
+ # rubocop:disable RSpec/RepeatedExample
278
+ it '#get_implicit_roles_for_user' do
279
+ # TODO: Add support this method
280
+ # enf.add_role_for_user('data2_admin', 'super_admin')
281
+ # expect(enf.get_implicit_roles_for_user('alice')).to match_array(%w[data2_admin super_admin])
282
+ end
283
+
284
+ it '#get_implicit_users_for_permission' do
285
+ # TODO: Add support this method
286
+ # expect(enf.get_implicit_users_for_permission('data2', 'write')).to match_array(%w[alice bob])
287
+ end
288
+ # rubocop:enable RSpec/RepeatedExample
153
289
  end
154
290
 
155
291
  describe 'rbac empty policy' do
@@ -189,6 +325,34 @@ describe Casbin::Enforcer do
189
325
  expect(enf.enforce('bob', 'domain2', 'data2', 'read')).to be_truthy
190
326
  expect(enf.enforce('bob', 'domain2', 'data2', 'write')).to be_truthy
191
327
  end
328
+
329
+ it '#get_roles_for_user_in_domain' do
330
+ expect(enf.get_roles_for_user_in_domain('alice', 'domain1')).to match_array(%w[admin])
331
+ expect(enf.get_roles_for_user_in_domain('bob', 'domain2')).to match_array(%w[admin])
332
+ end
333
+
334
+ it '#get_users_for_role_in_domain' do
335
+ expect(enf.get_users_for_role_in_domain('admin', 'domain1')).to match_array(%w[alice])
336
+ expect(enf.get_users_for_role_in_domain('admin', 'domain2')).to match_array(%w[bob])
337
+ end
338
+
339
+ it '#delete_roles_for_user_in_domain' do
340
+ enf.delete_roles_for_user_in_domain('alice', 'admin', 'domain1')
341
+ expect(enf.get_roles_for_user_in_domain('alice', 'domain1')).to match_array(%w[])
342
+ end
343
+
344
+ it '#get_permissions_for_user_in_domain' do
345
+ expect(enf.get_permissions_for_user_in_domain('admin', 'domain1'))
346
+ .to match_array([%w[admin domain1 data1 read],
347
+ %w[admin domain1 data1 write],
348
+ %w[admin domain2 data2 read],
349
+ %w[admin domain2 data2 write]])
350
+ expect(enf.get_permissions_for_user_in_domain('admin', 'domain2'))
351
+ .to match_array([%w[admin domain1 data1 read],
352
+ %w[admin domain1 data1 write],
353
+ %w[admin domain2 data2 read],
354
+ %w[admin domain2 data2 write]])
355
+ end
192
356
  end
193
357
 
194
358
  describe 'rbac with not deny' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casbin-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kutyavin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-06-03 00:00:00.000000000 Z
12
+ date: 2021-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: keisan
@@ -78,6 +78,7 @@ extra_rdoc_files: []
78
78
  files:
79
79
  - README.md
80
80
  - lib/casbin-ruby.rb
81
+ - lib/casbin-ruby/config.rb
81
82
  - lib/casbin-ruby/config/config.rb
82
83
  - lib/casbin-ruby/core_enforcer.rb
83
84
  - lib/casbin-ruby/effect/allow_and_deny_effector.rb
@@ -88,6 +89,7 @@ files:
88
89
  - lib/casbin-ruby/effect/priority_effector.rb
89
90
  - lib/casbin-ruby/enforcer.rb
90
91
  - lib/casbin-ruby/internal_enforcer.rb
92
+ - lib/casbin-ruby/logger.rb
91
93
  - lib/casbin-ruby/management_enforcer.rb
92
94
  - lib/casbin-ruby/model/assertion.rb
93
95
  - lib/casbin-ruby/model/function_map.rb
@@ -141,11 +143,11 @@ specification_version: 4
141
143
  summary: Casbin in Ruby
142
144
  test_files:
143
145
  - spec/support/model_helper.rb
144
- - spec/casbin/core_enforcer_spec.rb
145
- - spec/casbin/rbac/default_role_manager/role_manager_spec.rb
146
- - spec/casbin/rbac/default_role_manager/role_spec.rb
147
146
  - spec/casbin/config/config_spec.rb
147
+ - spec/casbin/enforcer_spec.rb
148
148
  - spec/casbin/model/function_map_spec.rb
149
149
  - spec/casbin/util_spec.rb
150
+ - spec/casbin/core_enforcer_spec.rb
150
151
  - spec/casbin/util/builtin_operators_spec.rb
151
- - spec/casbin/enforcer_spec.rb
152
+ - spec/casbin/rbac/default_role_manager/role_manager_spec.rb
153
+ - spec/casbin/rbac/default_role_manager/role_spec.rb