rbac-ruby 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9600e5634e01bd76377e231fc3854ed30695b284fbf5cc7dc408b502179797d
4
- data.tar.gz: df125b25b7deb799119bd9a5fcca3492939fd0504411381936975a2342933629
3
+ metadata.gz: 96fdbb068740693f2bcead695a1402934da0489f3fc6360ed04a3fb28e2f9064
4
+ data.tar.gz: 3bdbd122a1edae5e313724495287f14de1e97cd362e924efa37564387f5747ed
5
5
  SHA512:
6
- metadata.gz: 7bd4b6562cc69b1a7031f8bb358cdaf05f2678c43fc0daada334f89601c6daa313243f2b8ad5056b6c87abf11a9ec6b81129e006e4b08f9554681153fdeabf7d
7
- data.tar.gz: 1756b4083d7b9d8cef60a886fd946d790554b35805900dcabdfba61fb47ac902f37a626b8347784b64a815844ae4e63b5776091d306c4e6905fe95b3a5814cb9
6
+ metadata.gz: 5d46e7f00932801d65b862ef448bad585cd3b309f7504fcdf49610863c63b713928e3a7ce042a7cfc4244290a532e2f9dd71c2c98c4f4663d30278e4d5ed94cb
7
+ data.tar.gz: 68f9573c5659f50e9c9cabdb43d57cbd79d0ea3da6bb8b135c67f38c8fc9106a9427db6159bf52184e8d083c4ea6a5a1e06ac505e00055e6a0f3625c54de3988
data/.rubocop.yml CHANGED
@@ -1,5 +1,13 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ plugins:
5
+ - rubocop-capybara
6
+ - rubocop-performance
7
+
1
8
  AllCops:
2
- TargetRubyVersion: 2.6
9
+ TargetRubyVersion: 3.2
10
+ NewCops: enable
3
11
 
4
12
  Style/StringLiterals:
5
13
  Enabled: true
@@ -11,3 +19,7 @@ Style/StringLiteralsInInterpolation:
11
19
 
12
20
  Layout/LineLength:
13
21
  Max: 120
22
+
23
+ Metrics/BlockLength:
24
+ AllowedMethods:
25
+ - included
data/Gemfile CHANGED
@@ -9,8 +9,15 @@ gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
11
 
12
- gem "rubocop", "~> 1.21"
12
+ gem "rubocop", "~> 1.76"
13
13
 
14
14
  gem "yaml", "~> 0.2.0"
15
15
 
16
- gem "activesupport", "~> 7.0"
16
+ gem "activerecord", "~> 6.1.1"
17
+ gem "activesupport", "~> 6.1.1"
18
+
19
+ gem "rubocop-performance", "~> 1.19"
20
+ gem "rubocop-rake", "~> 0.6.0"
21
+ gem "rubocop-rspec", "~> 2.12"
22
+
23
+ gem "concurrent-ruby", "1.3.4"
data/Gemfile.lock CHANGED
@@ -1,71 +1,115 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbac (1.0.0)
4
+ rbac-ruby (2.0.0)
5
+ activerecord (>= 6.1, < 9.0)
6
+ activesupport (>= 6.1, < 9.0)
7
+ concurrent-ruby (= 1.3.4)
8
+ yaml (~> 0.2.0)
5
9
 
6
10
  GEM
7
11
  remote: https://rubygems.org/
8
12
  specs:
9
- activesupport (7.0.3)
13
+ activemodel (6.1.7.10)
14
+ activesupport (= 6.1.7.10)
15
+ activerecord (6.1.7.10)
16
+ activemodel (= 6.1.7.10)
17
+ activesupport (= 6.1.7.10)
18
+ activesupport (6.1.7.10)
10
19
  concurrent-ruby (~> 1.0, >= 1.0.2)
11
20
  i18n (>= 1.6, < 2)
12
21
  minitest (>= 5.1)
13
22
  tzinfo (~> 2.0)
14
- ast (2.4.2)
15
- concurrent-ruby (1.1.10)
16
- diff-lcs (1.5.0)
17
- i18n (1.10.0)
23
+ zeitwerk (~> 2.3)
24
+ ast (2.4.3)
25
+ concurrent-ruby (1.3.4)
26
+ diff-lcs (1.6.2)
27
+ i18n (1.14.7)
18
28
  concurrent-ruby (~> 1.0)
19
- json (2.6.2)
20
- minitest (5.16.2)
21
- parallel (1.22.1)
22
- parser (3.1.2.0)
29
+ json (2.12.2)
30
+ language_server-protocol (3.17.0.5)
31
+ lint_roller (1.1.0)
32
+ minitest (5.25.5)
33
+ parallel (1.27.0)
34
+ parser (3.3.8.0)
23
35
  ast (~> 2.4.1)
36
+ racc
37
+ prism (1.4.0)
38
+ racc (1.8.1)
24
39
  rainbow (3.1.1)
25
- rake (13.0.6)
26
- regexp_parser (2.5.0)
27
- rexml (3.2.5)
28
- rspec (3.11.0)
29
- rspec-core (~> 3.11.0)
30
- rspec-expectations (~> 3.11.0)
31
- rspec-mocks (~> 3.11.0)
32
- rspec-core (3.11.0)
33
- rspec-support (~> 3.11.0)
34
- rspec-expectations (3.11.0)
40
+ rake (13.3.0)
41
+ regexp_parser (2.10.0)
42
+ rspec (3.13.1)
43
+ rspec-core (~> 3.13.0)
44
+ rspec-expectations (~> 3.13.0)
45
+ rspec-mocks (~> 3.13.0)
46
+ rspec-core (3.13.4)
47
+ rspec-support (~> 3.13.0)
48
+ rspec-expectations (3.13.5)
35
49
  diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.11.0)
37
- rspec-mocks (3.11.1)
50
+ rspec-support (~> 3.13.0)
51
+ rspec-mocks (3.13.5)
38
52
  diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.11.0)
40
- rspec-support (3.11.0)
41
- rubocop (1.31.2)
53
+ rspec-support (~> 3.13.0)
54
+ rspec-support (3.13.4)
55
+ rubocop (1.76.1)
42
56
  json (~> 2.3)
57
+ language_server-protocol (~> 3.17.0.2)
58
+ lint_roller (~> 1.1.0)
43
59
  parallel (~> 1.10)
44
- parser (>= 3.1.0.0)
60
+ parser (>= 3.3.0.2)
45
61
  rainbow (>= 2.2.2, < 4.0)
46
- regexp_parser (>= 1.8, < 3.0)
47
- rexml (>= 3.2.5, < 4.0)
48
- rubocop-ast (>= 1.18.0, < 2.0)
62
+ regexp_parser (>= 2.9.3, < 3.0)
63
+ rubocop-ast (>= 1.45.0, < 2.0)
49
64
  ruby-progressbar (~> 1.7)
50
- unicode-display_width (>= 1.4.0, < 3.0)
51
- rubocop-ast (1.18.0)
52
- parser (>= 3.1.1.0)
53
- ruby-progressbar (1.11.0)
54
- tzinfo (2.0.4)
65
+ unicode-display_width (>= 2.4.0, < 4.0)
66
+ rubocop-ast (1.45.1)
67
+ parser (>= 3.3.7.2)
68
+ prism (~> 1.4)
69
+ rubocop-capybara (2.22.1)
70
+ lint_roller (~> 1.1)
71
+ rubocop (~> 1.72, >= 1.72.1)
72
+ rubocop-factory_bot (2.27.1)
73
+ lint_roller (~> 1.1)
74
+ rubocop (~> 1.72, >= 1.72.1)
75
+ rubocop-performance (1.25.0)
76
+ lint_roller (~> 1.1)
77
+ rubocop (>= 1.75.0, < 2.0)
78
+ rubocop-ast (>= 1.38.0, < 2.0)
79
+ rubocop-rake (0.6.0)
80
+ rubocop (~> 1.0)
81
+ rubocop-rspec (2.31.0)
82
+ rubocop (~> 1.40)
83
+ rubocop-capybara (~> 2.17)
84
+ rubocop-factory_bot (~> 2.22)
85
+ rubocop-rspec_rails (~> 2.28)
86
+ rubocop-rspec_rails (2.29.1)
87
+ rubocop (~> 1.61)
88
+ ruby-progressbar (1.13.0)
89
+ tzinfo (2.0.6)
55
90
  concurrent-ruby (~> 1.0)
56
- unicode-display_width (2.2.0)
57
- yaml (0.2.0)
91
+ unicode-display_width (3.1.4)
92
+ unicode-emoji (~> 4.0, >= 4.0.4)
93
+ unicode-emoji (4.0.4)
94
+ yaml (0.2.1)
95
+ zeitwerk (2.7.3)
58
96
 
59
97
  PLATFORMS
98
+ arm64-darwin-24
60
99
  x86_64-linux
61
100
 
62
101
  DEPENDENCIES
63
- activesupport (~> 7.0)
102
+ activerecord (~> 6.1.1)
103
+ activesupport (~> 6.1.1)
104
+ concurrent-ruby (= 1.3.4)
64
105
  rake (~> 13.0)
65
- rbac!
106
+ rbac-ruby!
66
107
  rspec (~> 3.0)
67
- rubocop (~> 1.21)
108
+ rubocop (~> 1.76)
109
+ rubocop-performance (~> 1.19)
110
+ rubocop-rake (~> 0.6.0)
111
+ rubocop-rspec (~> 2.12)
68
112
  yaml (~> 0.2.0)
69
113
 
70
114
  BUNDLED WITH
71
- 2.2.33
115
+ 2.4.19
data/README.md CHANGED
@@ -7,7 +7,7 @@ A simple way to manage access and visibillity scopes to objects in your Rails ap
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'rbac'
10
+ gem 'rbac-ruby'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -3,27 +3,33 @@
3
3
  require "yaml"
4
4
 
5
5
  module Rbac
6
- # Authorizer class is responsible for checking
6
+ # Authorizer module is responsible for checking
7
7
  # if user has access to perform some action.
8
- class Authorizer
9
- attr_reader :config
8
+ module Authorizer
9
+ require "active_support/concern"
10
+ extend ActiveSupport::Concern
10
11
 
11
- # Load configuration from a YAML file.
12
- def initialize(config_file)
13
- @config = YAML.load_file(config_file)
12
+ def authorize_user(attrs)
13
+ Rbac::User.current = Rbac::User.new(**attrs)
14
14
  end
15
15
 
16
- def role_allows?(request)
17
- controller, action = fetch_params_from_request(request)
18
- config["features"][controller][action].include?(User.current_user.role)
16
+ def user_role_authorized?
17
+ controller = params[:controller]
18
+ action = params[:action]
19
+ routes_config.dig("features", controller, action)&.include?(Rbac::User.current.role)
19
20
  end
20
21
 
21
22
  private
22
23
 
23
- def fetch_params_from_request(request)
24
- controller = request.params[:controller]
25
- action = request.params[:action]
26
- [controller, action]
24
+ # Load configuration from a YAML file.
25
+ def routes_config
26
+ @routes_config ||= YAML.safe_load_file(ENV.fetch("RBAC_ROUTES_FILE", "config/rbac_routes.yaml"))
27
27
  end
28
28
  end
29
29
  end
30
+
31
+ module ActionController
32
+ class API
33
+ include Rbac::Authorizer
34
+ end
35
+ end
data/lib/rbac/filterer.rb CHANGED
@@ -1,34 +1,50 @@
1
- module Rbac::Filterer
2
- require "active_support/concern"
3
- extend ActiveSupport::Concern
1
+ # frozen_string_literal: true
4
2
 
5
- included do
6
- def self.filtered
7
- require 'yaml'
3
+ require "yaml"
4
+ require "active_support/concern"
5
+ require "active_support/core_ext/string"
6
+ require "active_record"
8
7
 
9
- scopes_map = YAML.load File.open ENV['SCOPES_MAP_FILE']
8
+ module Rbac
9
+ # Filterer module defines some ActiveRecord methods
10
+ # for filtering objects to which user does not have access.
11
+ module Filterer
12
+ extend ActiveSupport::Concern
10
13
 
11
- filters_config = scopes_map[self.name][User.current_user.role]
14
+ included do
15
+ scope :rbac_filtered, lambda {
16
+ scopes_map = YAML.safe_load_file(ENV.fetch("RBAC_SCOPES_FILE", "config/rbac_scopes.yml"))
12
17
 
13
- filters = filters_config['filters']
14
- scope = filters_config['scope']
15
- parent = filters_config['parent']
18
+ current_user = Rbac::User.current
19
+ filters_config = scopes_map.dig(name, current_user.role)
20
+
21
+ filters = filters_config["filters"]
22
+ scope = filters_config["scope"]
23
+ parent = filters_config["parent"]
24
+
25
+ return send(scope) if scope
26
+
27
+ return none unless filters || parent
28
+
29
+ relation = all
16
30
 
17
- # TODO: refactor it and remove complexity of this code
18
- if parent
19
31
  if filters
20
- additional_filters = filters.map { |field_name, attribute| { field_name => User.current_user.send(attribute) } }.reduce Hash.new, :merge
21
- where(additional_filters.merge(parent.downcase.to_sym => parent.constantize.filtered))
22
- else
23
- where(parent.downcase.to_sym => parent.constantize.filtered)
32
+ additional_filters = filters.transform_values do |attribute|
33
+ current_user.attributes[attribute]
34
+ end
35
+ relation = relation.where(additional_filters)
24
36
  end
25
- elsif filters
26
- where(filters.map { |field_name, attribute| { field_name => User.current_user.send(attribute) } }.reduce Hash.new, :merge)
27
- elsif scope
28
- self.send(scope)
29
- else
30
- []
31
- end
37
+
38
+ return relation unless parent
39
+
40
+ relation.where(parent.underscore => parent.constantize.rbac_filtered)
41
+ }
32
42
  end
33
43
  end
34
- end
44
+ end
45
+
46
+ module ActiveRecord
47
+ class Base
48
+ include Rbac::Filterer
49
+ end
50
+ end
data/lib/rbac/user.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module"
4
+
5
+ module Rbac
6
+ # Class Rbac::User describes authorized user
7
+ # and stores its attributes required for RBAC filtration
8
+ class User
9
+ attr_reader :role, :attributes
10
+
11
+ thread_mattr_accessor :current
12
+
13
+ def initialize(role:, **attrs)
14
+ @role = role
15
+ @attributes = attrs.stringify_keys
16
+ end
17
+ end
18
+ end
data/lib/rbac/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rbac
4
- VERSION = "1.0.1"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/rbac.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "rbac/version"
4
4
  require_relative "rbac/authorizer"
5
5
  require_relative "rbac/filterer"
6
+ require_relative "rbac/user"
6
7
 
7
8
  module Rbac
8
9
  class Error < StandardError; end
data/rbac.gemspec CHANGED
@@ -8,13 +8,15 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Aliaksei Hrechushkin"]
9
9
  spec.email = ["ahrechushkin@ibagroup.eu"]
10
10
 
11
- spec.summary = "Role-based access control gem."
12
- spec.description = "Use dynamicly configurable RBAC system to control access to your application."
13
- spec.required_ruby_version = ">= 2.6.0"
14
- spec.homepage = "https://icdc.io"
15
- spec.licenses = ["Apache-2.0"]
16
- spec.metadata["source_code_uri"] = "https://github.com/icdc-io/rbac-ruby"
17
- spec.metadata["changelog_uri"] = "https://github.com/icdc-io/rbac-ruby/blob/master/CHANGELOG.md"
11
+ spec.summary = "Role-based access control gem."
12
+ spec.description = "Use dynamicly configurable RBAC system to control access to your application."
13
+ spec.required_ruby_version = ">= 3.2.0"
14
+ spec.homepage = "https://icdc.io"
15
+ spec.licenses = ["Apache-2.0"]
16
+
17
+ spec.metadata["source_code_uri"] = "https://github.com/icdc-io/rbac-ruby"
18
+ spec.metadata["changelog_uri"] = "https://github.com/icdc-io/rbac-ruby/blob/master/CHANGELOG.md"
19
+ spec.metadata["rubygems_mfa_required"] = "true"
18
20
 
19
21
  # Specify which files should be added to the gem when it is released.
20
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -27,6 +29,11 @@ Gem::Specification.new do |spec|
27
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
30
  spec.require_paths = ["lib"]
29
31
 
32
+ spec.add_dependency "activerecord", ">= 6.1", "< 9.0"
33
+ spec.add_dependency "activesupport", ">= 6.1", "< 9.0"
34
+ spec.add_dependency "concurrent-ruby", "1.3.4"
35
+ spec.add_dependency "yaml", "~> 0.2.0"
36
+
30
37
  # Uncomment to register a new dependency of your gem
31
38
  # spec.add_dependency "example-gem", "~> 1.0"
32
39
 
metadata CHANGED
@@ -1,15 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbac-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aliaksei Hrechushkin
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-08-04 00:00:00.000000000 Z
12
- dependencies: []
10
+ date: 2025-06-10 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '6.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: activesupport
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '6.1'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9.0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '6.1'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9.0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: concurrent-ruby
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '='
57
+ - !ruby/object:Gem::Version
58
+ version: 1.3.4
59
+ type: :runtime
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.4
66
+ - !ruby/object:Gem::Dependency
67
+ name: yaml
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 0.2.0
73
+ type: :runtime
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.2.0
13
80
  description: Use dynamicly configurable RBAC system to control access to your application.
14
81
  email:
15
82
  - ahrechushkin@ibagroup.eu
@@ -30,6 +97,7 @@ files:
30
97
  - lib/rbac.rb
31
98
  - lib/rbac/authorizer.rb
32
99
  - lib/rbac/filterer.rb
100
+ - lib/rbac/user.rb
33
101
  - lib/rbac/version.rb
34
102
  - rbac.gemspec
35
103
  - rbac_routes.example.yml
@@ -41,7 +109,7 @@ licenses:
41
109
  metadata:
42
110
  source_code_uri: https://github.com/icdc-io/rbac-ruby
43
111
  changelog_uri: https://github.com/icdc-io/rbac-ruby/blob/master/CHANGELOG.md
44
- post_install_message:
112
+ rubygems_mfa_required: 'true'
45
113
  rdoc_options: []
46
114
  require_paths:
47
115
  - lib
@@ -49,15 +117,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
117
  requirements:
50
118
  - - ">="
51
119
  - !ruby/object:Gem::Version
52
- version: 2.6.0
120
+ version: 3.2.0
53
121
  required_rubygems_version: !ruby/object:Gem::Requirement
54
122
  requirements:
55
123
  - - ">="
56
124
  - !ruby/object:Gem::Version
57
125
  version: '0'
58
126
  requirements: []
59
- rubygems_version: 3.2.3
60
- signing_key:
127
+ rubygems_version: 3.6.3
61
128
  specification_version: 4
62
129
  summary: Role-based access control gem.
63
130
  test_files: []