bullet_train-roles 0.1.7 → 0.1.10
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 +4 -4
- data/.circleci/config.yml +2 -1
- data/Gemfile.lock +7 -6
- data/lib/bullet_train/roles/engine.rb +17 -0
- data/lib/bullet_train/roles/version.rb +1 -1
- data/lib/bullet_train/roles.rb +2 -0
- data/lib/models/role.rb +2 -2
- data/lib/roles/permit.rb +55 -21
- data/lib/roles/user.rb +10 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e682f8f5469478a7c827332cb3c922a3c7657ef48e2e53d15abaf98b4ea8e7ac
|
4
|
+
data.tar.gz: 15ca75ffc2ebacabebc3632ecc18ce5f20348ad434df094de2ee14eaa05c031e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a525dc712139149ea12932af58932a3a8ec11ab582c6a5924b88035ea0c67563fb5ea9958cb9dc581b2045b46ff7add98a164383034ae6e3cb03a50f089076f
|
7
|
+
data.tar.gz: 544e1c9ddbde15bd1945529af7d0ae136ff22fc8c2d12a0d8b91c148a61a2bbfa2f4296890ec0e235d1353bbff0394f811e588675e42c56e3b254c4ec6d7abcb
|
data/.circleci/config.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -9,7 +9,7 @@ GIT
|
|
9
9
|
PATH
|
10
10
|
remote: .
|
11
11
|
specs:
|
12
|
-
bullet_train-roles (0.1.
|
12
|
+
bullet_train-roles (0.1.10)
|
13
13
|
active_hash
|
14
14
|
activesupport
|
15
15
|
cancancan
|
@@ -79,7 +79,7 @@ GEM
|
|
79
79
|
ast (2.4.2)
|
80
80
|
builder (3.2.4)
|
81
81
|
byebug (11.1.3)
|
82
|
-
cancancan (3.
|
82
|
+
cancancan (3.4.0)
|
83
83
|
concurrent-ruby (1.1.9)
|
84
84
|
crass (1.0.6)
|
85
85
|
erubi (1.10.0)
|
@@ -102,11 +102,11 @@ GEM
|
|
102
102
|
marcel (1.0.2)
|
103
103
|
method_source (1.0.0)
|
104
104
|
mini_mime (1.1.2)
|
105
|
+
mini_portile2 (2.6.1)
|
105
106
|
minitest (5.15.0)
|
106
107
|
nio4r (2.5.8)
|
107
|
-
nokogiri (1.12.5
|
108
|
-
|
109
|
-
nokogiri (1.12.5-x86_64-darwin)
|
108
|
+
nokogiri (1.12.5)
|
109
|
+
mini_portile2 (~> 2.6.1)
|
110
110
|
racc (~> 1.4)
|
111
111
|
parallel (1.21.0)
|
112
112
|
parser (3.0.3.2)
|
@@ -176,6 +176,7 @@ GEM
|
|
176
176
|
PLATFORMS
|
177
177
|
arm64-darwin-20
|
178
178
|
arm64-darwin-21
|
179
|
+
ruby
|
179
180
|
x86_64-darwin-21
|
180
181
|
|
181
182
|
DEPENDENCIES
|
@@ -191,4 +192,4 @@ DEPENDENCIES
|
|
191
192
|
standard (~> 1.5.0)
|
192
193
|
|
193
194
|
BUNDLED WITH
|
194
|
-
2.3.
|
195
|
+
2.3.13
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module BulletTrain
|
2
|
+
module Roles
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
config.eager_load_paths << Role.full_path
|
5
|
+
|
6
|
+
initializer "bullet_train-roles.config" do |app|
|
7
|
+
role_reloader = ActiveSupport::FileUpdateChecker.new([Role.full_path]) do
|
8
|
+
Role.reload(true)
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport::Reloader.to_prepare do
|
12
|
+
role_reloader.execute_if_updated
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/bullet_train/roles.rb
CHANGED
data/lib/models/role.rb
CHANGED
@@ -176,7 +176,7 @@ class Role < ActiveYaml::Base
|
|
176
176
|
if @model == @parent
|
177
177
|
return @condition = {id: @parent_ids}
|
178
178
|
end
|
179
|
-
parent_association = possible_parent_associations.find { |association| @model.
|
179
|
+
parent_association = possible_parent_associations.find { |association| @model.reflect_on_association(association) || @intermediary_class&.reflect_on_association(association) }
|
180
180
|
return nil unless parent_association.present?
|
181
181
|
# If possible, use the team_id attribute because it saves us having to join all the way back to the source parent model
|
182
182
|
# In some scenarios this may be quicker, or if the parent model is in a different database shard, it may not even
|
@@ -184,7 +184,7 @@ class Role < ActiveYaml::Base
|
|
184
184
|
parent_with_id = "#{parent_association}_id"
|
185
185
|
@condition = if @model.column_names.include?(parent_with_id)
|
186
186
|
{parent_with_id.to_sym => @parent_ids}
|
187
|
-
elsif @intermediary.present? && @model.
|
187
|
+
elsif @intermediary.present? && @model.reflect_on_association(@intermediary)
|
188
188
|
{@intermediary.to_sym => {parent_with_id.to_sym => @parent_ids}}
|
189
189
|
else
|
190
190
|
{parent_association => {id: @parent_ids}}
|
data/lib/roles/permit.rb
CHANGED
@@ -2,48 +2,82 @@
|
|
2
2
|
|
3
3
|
module Roles
|
4
4
|
module Permit
|
5
|
-
def permit(user, through:, parent:, debug: false, intermediary: nil)
|
6
|
-
# Without this, you need to restart the server each time you make changes to the config/models/role.yml file
|
7
|
-
Role.reload(true) if Rails.env.development?
|
8
|
-
|
5
|
+
def permit(user, through:, parent:, debug: false, intermediary: nil, cache_key: nil)
|
9
6
|
# When changing permissions during development, you may also want to do this on each request:
|
10
7
|
# User.update_all ability_cache: nil if Rails.env.development?
|
11
|
-
|
8
|
+
permissions = if cache_key
|
9
|
+
Rails.cache.fetch(cache_key) do
|
10
|
+
build_permissions(user, through, parent, intermediary)
|
11
|
+
end
|
12
|
+
else
|
13
|
+
build_permissions(user, through, parent, intermediary)
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
assign_permissions(permissions)
|
18
|
+
rescue NameError => e
|
19
|
+
if cache_key
|
20
|
+
# Cache has become stale with model classes that no longer exist
|
21
|
+
Rails.logger.info "Found missing models in cache - #{e.message.squish} - building fresh permissions"
|
22
|
+
Rails.cache.delete(cache_key)
|
23
|
+
permissions = build_permissions(user, through, parent, intermediary)
|
24
|
+
assign_permissions(permissions)
|
25
|
+
else
|
26
|
+
raise e
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if debug
|
31
|
+
puts "###########################"
|
32
|
+
puts "Auto generated `ability.rb` content:"
|
33
|
+
permissions.map do |permission|
|
34
|
+
if permission.is_debug
|
35
|
+
puts permission.info
|
36
|
+
else
|
37
|
+
puts "can #{permission.actions}, #{permission.model}, #{permission.condition}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
puts "############################"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def assign_permissions(permissions)
|
45
|
+
permissions.each do |permission|
|
46
|
+
can(permission.actions, permission.model.constantize, permission.condition) unless permission.is_debug
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_permissions(user, through, parent, intermediary)
|
12
51
|
added_roles = Set.new
|
52
|
+
permissions = []
|
13
53
|
user.send(through).map(&:roles).flatten.uniq.each do |role|
|
14
54
|
unless added_roles.include?(role)
|
15
|
-
|
16
|
-
|
55
|
+
permissions << OpenStruct.new(is_debug: true, info: "########### ROLE: #{role.key}")
|
56
|
+
permissions += add_abilities_for(role, user, through, parent, intermediary)
|
17
57
|
added_roles << role
|
18
58
|
end
|
19
59
|
|
20
60
|
role.included_roles.each do |included_role|
|
21
61
|
unless added_roles.include?(included_role)
|
22
|
-
|
23
|
-
|
62
|
+
permissions << OpenStruct.new(is_debug: true, info: "############# INCLUDED ROLE: #{included_role.key}")
|
63
|
+
permissions += add_abilities_for(included_role, user, through, parent, intermediary)
|
24
64
|
end
|
25
65
|
end
|
26
66
|
end
|
27
67
|
|
28
|
-
|
29
|
-
puts "###########################"
|
30
|
-
puts "Auto generated `ability.rb` content:"
|
31
|
-
puts output
|
32
|
-
puts "############################"
|
33
|
-
end
|
68
|
+
permissions
|
34
69
|
end
|
35
70
|
|
36
71
|
def add_abilities_for(role, user, through, parent, intermediary)
|
37
|
-
|
72
|
+
permissions = []
|
38
73
|
role.ability_generator(user, through, parent, intermediary) do |ag|
|
39
|
-
if ag.valid?
|
40
|
-
|
41
|
-
can(ag.actions, ag.model, ag.condition)
|
74
|
+
permissions << if ag.valid?
|
75
|
+
OpenStruct.new(is_debug: false, actions: ag.actions, model: ag.model.to_s, condition: ag.condition)
|
42
76
|
else
|
43
|
-
|
77
|
+
OpenStruct.new(is_debug: true, info: "# #{ag.model} does not respond to #{parent} so we're not going to add an ability for the #{through} context")
|
44
78
|
end
|
45
79
|
end
|
46
|
-
|
80
|
+
permissions
|
47
81
|
end
|
48
82
|
end
|
49
83
|
end
|
data/lib/roles/user.rb
CHANGED
@@ -9,17 +9,25 @@ module Roles
|
|
9
9
|
included do
|
10
10
|
def parent_ids_for(role, through, parent)
|
11
11
|
parent_id_column = "#{parent}_id"
|
12
|
-
|
12
|
+
key = "#{role.key}_#{through}_#{parent_id_column}s"
|
13
|
+
|
14
|
+
@_parent_ids_for_cache ||= {}
|
15
|
+
return @_parent_ids_for_cache[key] if @_parent_ids_for_cache[key]
|
16
|
+
|
13
17
|
# TODO Maybe we should make ability caching a default feature of the gem?
|
14
18
|
# If we do that, we would just make it check whether `ability_cache` exists.
|
15
19
|
# return ability_cache[key] if ability_cache && ability_cache[key]
|
16
20
|
role = nil if role.default?
|
17
|
-
send(through).with_role(role).distinct.pluck(parent_id_column)
|
21
|
+
@_parent_ids_for_cache[key] = send(through).with_role(role).distinct.pluck(parent_id_column)
|
18
22
|
# TODO Maybe we should make ability caching a default feature of the gem?
|
19
23
|
# current_cache = ability_cache || {}
|
20
24
|
# current_cache[key] = value
|
21
25
|
# update_column :ability_cache, current_cache
|
22
26
|
end
|
27
|
+
|
28
|
+
def invalidate_ability_cache
|
29
|
+
update_column :ability_cache, {}
|
30
|
+
end
|
23
31
|
end
|
24
32
|
end
|
25
33
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-roles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Prabin Poudel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2022-
|
12
|
+
date: 2022-08-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: byebug
|
@@ -185,6 +185,7 @@ files:
|
|
185
185
|
- bin/setup
|
186
186
|
- bullet_train-roles.gemspec
|
187
187
|
- lib/bullet_train/roles.rb
|
188
|
+
- lib/bullet_train/roles/engine.rb
|
188
189
|
- lib/bullet_train/roles/version.rb
|
189
190
|
- lib/generators/bullet_train/roles/install/USAGE
|
190
191
|
- lib/generators/bullet_train/roles/install/install_generator.rb
|
@@ -215,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
215
216
|
- !ruby/object:Gem::Version
|
216
217
|
version: '0'
|
217
218
|
requirements: []
|
218
|
-
rubygems_version: 3.3.
|
219
|
+
rubygems_version: 3.3.7
|
219
220
|
signing_key:
|
220
221
|
specification_version: 4
|
221
222
|
summary: Yaml-backed ApplicationHash for CanCan Roles
|