casbin-ruby 1.0.10 → 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: 88e68e3c8f15c55fbeba3bf37683124c9d2fb17a8d2dd408f3ed7d36a430fe95
4
- data.tar.gz: 124e945552b694c89ef488a71d286d6171118281041c2823db896efb56ec0d83
3
+ metadata.gz: 0a6fa41ab5193602d852ba7652d0d92775f1a26243471d154188aca17a5a3a67
4
+ data.tar.gz: 19174a79a646b395960fd842bec794f8a202d283396c802d872ce7e88914763a
5
5
  SHA512:
6
- metadata.gz: 10b12e4a479b2c3d4f7b70f33750d604292f15aaae6441968559a7d7ae195e4a5b291a3ce69fe553e7c976020218fa1b2dd2dcb86c58a9483868c30eef38842c
7
- data.tar.gz: 225eaf757fbaab699f3a4c832020b9ca527e2b53e2f03c843631dadf81a1f199c0135447cc9a41bc96f509712011feca18970c02949fa037543e1a1c77aa08c1
6
+ metadata.gz: 9c40a77ea91e4addeba67652af4fe0bd1af3151f67e2bdfc3625f4c90e295a643d3f2d221446957ebd3a1f4321e3a1869a822a31102c5a6cdf78da3dd572e35d
7
+ data.tar.gz: 29c3ff1985b8e1480ce596763ce621790cf19f6676649efc6d6e4ac8bb06c476f3d8618db2e72a89a40e1f277da36a5eba5ba598040a4dd3ada102b91fe9c390
data/README.md CHANGED
@@ -151,7 +151,20 @@ 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`
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
155
168
  require 'casbin-ruby'
156
169
  enforcer = Casbin::Enforcer.new("path/to/model.conf", "path/to/policy.csv")
157
170
  ```
@@ -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
@@ -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
@@ -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.10'
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'
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.10
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-09-01 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/rbac/default_role_manager/role_spec.rb
145
- - spec/casbin/rbac/default_role_manager/role_manager_spec.rb
146
+ - spec/casbin/config/config_spec.rb
146
147
  - spec/casbin/enforcer_spec.rb
147
- - spec/casbin/util/builtin_operators_spec.rb
148
+ - spec/casbin/model/function_map_spec.rb
148
149
  - spec/casbin/util_spec.rb
149
150
  - spec/casbin/core_enforcer_spec.rb
150
- - spec/casbin/config/config_spec.rb
151
- - spec/casbin/model/function_map_spec.rb
151
+ - spec/casbin/util/builtin_operators_spec.rb
152
+ - spec/casbin/rbac/default_role_manager/role_manager_spec.rb
153
+ - spec/casbin/rbac/default_role_manager/role_spec.rb