rabarber 4.0.1 → 4.1.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: 44b43bb0c28c871575c4e723415cd3df577a2fa715b07178c4a8a21102183791
4
- data.tar.gz: 7cf707c61a560d271345162834a0557f51519a0fbfe9d1e81eb3d74cfb401cff
3
+ metadata.gz: 5450b60dd272da8e0b78af00e44a3e82b29c7b9a9e8b510c68efccbba30ba150
4
+ data.tar.gz: 7bbdd6de4ed8f2e33be07363c9c9d4e099170099ca7cb32a50df217373503284
5
5
  SHA512:
6
- metadata.gz: '09d9897bdd2df705e37bf9800a10f30904b102d8d33773e35cec4ed0c7ceebeca78fc47ce038cc6b617f698ee89cebc7d34da7d0cd3ef67685cc2d61ed022ebb'
7
- data.tar.gz: 610d8700782cc500bfa83d62de0816c2b6253f6fcb42d5c233213675da9e4c7d9b0b7e9b07287514d48cae68b0cc56440cb0e225147487f2f15d02ea5a8e7aab
6
+ metadata.gz: 8c9aa4a196f99dbb5250dd1b625fed829418843f5de8f35a4b2cde5f933f8780be0865f8c23e90c57490a3358f004fd1168dc28bcc9be9f66f0bb56b9ebcfddf
7
+ data.tar.gz: 3ddc04e91777a31631d4e9db9a54d6d1e23f588935e09c9d9691f8c607fffbabe18a49553cc9bad22ed8944dbec8b6cb58ecaf6a432c7ac7da59c1aca27a334f
data/CHANGELOG.md CHANGED
@@ -1,8 +1,26 @@
1
+ ## v4.1.0
2
+
3
+ ### Features:
4
+
5
+ - Added `Rabarber::Role.all_names` method to retrieve all roles available in the application, grouped by context
6
+ - Added `Rabarber::HasRoles#all_roles` method to retrieve all roles assigned to a user, grouped by context
7
+
8
+ ### Bugs:
9
+
10
+ - Fixed potential bug in role revocation caused by checking for the presence of a role in the cache instead of the database
11
+
12
+ ## v4.0.2
13
+
14
+ ### Misc:
15
+
16
+ - Added support for Ruby 3.4
17
+ - Updated some error messages for clarity
18
+
1
19
  ## v4.0.1
2
20
 
3
21
  ### Bugs:
4
22
 
5
- - Resolved an issue preventing the gem from being used with rbs_rails
23
+ - Resolved an issue preventing Rabarber from being used with the RBS Rails gem
6
24
 
7
25
  ## v4.0.0
8
26
 
data/README.md CHANGED
@@ -158,6 +158,14 @@ To get the list of roles assigned to the user, use:
158
158
  user.roles
159
159
  ```
160
160
 
161
+ **`#all_roles`**
162
+
163
+ To get all roles assigned to the user, grouped by context, use:
164
+
165
+ ```rb
166
+ user.all_roles
167
+ ```
168
+
161
169
  ---
162
170
 
163
171
  To manipulate roles directly, you can use `Rabarber::Role` methods:
@@ -203,12 +211,20 @@ Rabarber::Role.remove(:admin, force: true)
203
211
 
204
212
  **`.names(context: nil)`**
205
213
 
206
- If you need to list the role names available in your application, use:
214
+ If you need to list the roles available in your application, use:
207
215
 
208
216
  ```rb
209
217
  Rabarber::Role.names
210
218
  ```
211
219
 
220
+ **`.all_names`**
221
+
222
+ If you need list all roles available in your application, grouped by context, use:
223
+
224
+ ```rb
225
+ Rabarber::Role.all_names
226
+ ```
227
+
212
228
  **`.assignees(role_name, context: nil)`**
213
229
 
214
230
  To get all the users to whom the role is assigned, use:
@@ -475,7 +491,7 @@ This method accepts the same options as `skip_before_action` method in Rails.
475
491
 
476
492
  ## View Helpers
477
493
 
478
- Rabarber also provides a couple of helpers that can be used in views: `visible_to(*roles, &block)` and `hidden_from(*roles, &block)`. To use them, simply include `Rabarber::Helpers` in the desired helper. Usually it is `ApplicationHelper`, but it can be any helper of your choice.
494
+ Rabarber also provides a couple of helpers that can be used in views: `visible_to(*roles, context: nil, &block)` and `hidden_from(*roles, context: nil, &block)`. To use them, simply include `Rabarber::Helpers` in the desired helper. Usually it is `ApplicationHelper`, but it can be any helper of your choice.
479
495
 
480
496
  ```rb
481
497
  module ApplicationHelper
@@ -40,7 +40,7 @@ module Rabarber
40
40
  in { context_type: nil, context_id: nil } then "Global"
41
41
  in { context_type:, context_id: nil } then context_type
42
42
  in { context_type:, context_id: } then "#{context_type}##{context_id}"
43
- else raise "Unexpected context: #{context}"
43
+ else raise Rabarber::Error, "Unexpected context: #{context.inspect}"
44
44
  end
45
45
  end
46
46
  end
@@ -32,7 +32,7 @@ module Rabarber
32
32
  end
33
33
 
34
34
  def key_for(id, context)
35
- "#{CACHE_PREFIX}:#{Digest::SHA2.hexdigest("#{id}#{context}")}"
35
+ "#{CACHE_PREFIX}:#{Digest::SHA2.hexdigest("#{id}#{context.fetch(:context_type)}#{context.fetch(:context_id)}")}"
36
36
  end
37
37
  end
38
38
  end
@@ -14,7 +14,7 @@ module Rabarber
14
14
 
15
15
  raise(
16
16
  Rabarber::Error,
17
- "Following actions were passed to 'grant_access' method but are not defined in the controller: #{missing_list}"
17
+ "Following actions were passed to 'grant_access' method but are not defined in the controller:\n#{missing_list.to_yaml}"
18
18
  )
19
19
  end
20
20
 
@@ -19,6 +19,10 @@ module Rabarber
19
19
  Rabarber::Core::Cache.fetch(roleable_id, context: processed_context) { rabarber_roles.names(context: processed_context) }
20
20
  end
21
21
 
22
+ def all_roles
23
+ rabarber_roles.all_names
24
+ end
25
+
22
26
  def has_role?(*role_names, context: nil)
23
27
  processed_context = process_context(context)
24
28
  processed_roles = process_role_names(role_names)
@@ -55,7 +59,7 @@ module Rabarber
55
59
  processed_context = process_context(context)
56
60
 
57
61
  roles_to_revoke = Rabarber::Role.where(
58
- name: processed_role_names.intersection(roles(context: processed_context)), **processed_context
62
+ name: processed_role_names.intersection(rabarber_roles.names(context: processed_context)), **processed_context
59
63
  )
60
64
 
61
65
  if roles_to_revoke.any?
@@ -9,6 +9,8 @@ module Rabarber
9
9
  format: { with: Rabarber::Input::Role::REGEX },
10
10
  strict: true
11
11
 
12
+ belongs_to :context, polymorphic: true, optional: true
13
+
12
14
  before_destroy :delete_assignments
13
15
 
14
16
  class << self
@@ -16,6 +18,14 @@ module Rabarber
16
18
  where(process_context(context)).pluck(:name).map(&:to_sym)
17
19
  end
18
20
 
21
+ def all_names
22
+ includes(:context).group_by(&:context).transform_values { |roles| roles.map { _1.name.to_sym } }
23
+ rescue ActiveRecord::RecordNotFound => e
24
+ raise Rabarber::Error, "Context not found: #{e.model}##{e.id}"
25
+ rescue NameError => e
26
+ raise Rabarber::Error, "Context not found: #{e.name}"
27
+ end
28
+
19
29
  def add(name, context: nil)
20
30
  name = process_role_name(name)
21
31
  processed_context = process_context(context)
@@ -77,6 +87,16 @@ module Rabarber
77
87
  end
78
88
  end
79
89
 
90
+ def context
91
+ return context_type.constantize if context_type.present? && context_id.blank?
92
+
93
+ record = super
94
+
95
+ raise ActiveRecord::RecordNotFound.new(nil, context_type, nil, context_id) if context_id.present? && !record
96
+
97
+ record
98
+ end
99
+
80
100
  private
81
101
 
82
102
  def delete_assignments
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rabarber
4
- VERSION = "4.0.1"
4
+ VERSION = "4.1.0"
5
5
  end
data/rabarber.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.metadata["rubygems_mfa_required"] = "true"
15
15
  spec.summary = "Simple role-based authorization library for Ruby on Rails"
16
16
  spec.license = "MIT"
17
- spec.required_ruby_version = ">= 3.1", "< 3.4"
17
+ spec.required_ruby_version = ">= 3.1", "< 3.5"
18
18
 
19
19
  spec.files = [
20
20
  "rabarber.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabarber
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - enjaku4
8
8
  - trafium
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2024-11-21 00:00:00.000000000 Z
11
+ date: 2024-12-29 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
@@ -31,7 +30,6 @@ dependencies:
31
30
  - - "<"
32
31
  - !ruby/object:Gem::Version
33
32
  version: '8.1'
34
- description:
35
33
  email:
36
34
  - rabarber_gem@icloud.com
37
35
  executables: []
@@ -82,7 +80,6 @@ metadata:
82
80
  source_code_uri: https://github.com/enjaku4/rabarber
83
81
  changelog_uri: https://github.com/enjaku4/rabarber/blob/main/CHANGELOG.md
84
82
  rubygems_mfa_required: 'true'
85
- post_install_message:
86
83
  rdoc_options: []
87
84
  require_paths:
88
85
  - lib
@@ -93,15 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
90
  version: '3.1'
94
91
  - - "<"
95
92
  - !ruby/object:Gem::Version
96
- version: '3.4'
93
+ version: '3.5'
97
94
  required_rubygems_version: !ruby/object:Gem::Requirement
98
95
  requirements:
99
96
  - - ">="
100
97
  - !ruby/object:Gem::Version
101
98
  version: '0'
102
99
  requirements: []
103
- rubygems_version: 3.3.27
104
- signing_key:
100
+ rubygems_version: 3.6.2
105
101
  specification_version: 4
106
102
  summary: Simple role-based authorization library for Ruby on Rails
107
103
  test_files: []