nexus_cqrs 0.4.2 → 0.4.3

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: 967dfb16241304d1d0f2233b23e3410b87abff47ec4a916569101a53e4b963c5
4
- data.tar.gz: d528d7da45c96cae5fe8a403116e3f9c8abc83c2842dbd165dfa5c3267a81e1d
3
+ metadata.gz: 2ad110fb96498636aa174c3d45aa7783177fb7e5a2475e828f6e694adf392b46
4
+ data.tar.gz: 35fd22cd65fd520c91c93629b365f4573fbc0787158212b12dd3fb1241e0da43
5
5
  SHA512:
6
- metadata.gz: 1f407d669371be6e3fd4f9ad4e55f92678b6fc82825b1dba6f5bf4a1dabb070c79005bdf37a114dd2461f6b99d7213daf777894d04f3337daf4a8255a74fa2f6
7
- data.tar.gz: 6d31c90bff990bd92f7aeb9ffcf289d1fe47bb5fd987d47f14fb12e7a7d9baa0571f731a466dff569999976a370c7028a6380c8f4423a5798ecc1063f8847029
6
+ metadata.gz: faee92b66ddcd2cdcd70fb315f444c2f226ff194ff528eb5c4e3f0a654c5e274e7715df987883b7a0512cbf0245a84824dd24829aa879277424c675435ea5169
7
+ data.tar.gz: 172b58d34c2da223e41e933fd0df3b379450d0d9ec0ce89095e6581ff894006224614cb62455d7d5223a376988413f3e6d7a98e28d1d502ad58b349c3a075fa8
data/Gemfile CHANGED
@@ -11,3 +11,4 @@ gem 'rspec'
11
11
  gem "generator_spec"
12
12
  gem 'thread_safe'
13
13
  gem 'ibsciss-middleware'
14
+ gem 'request_store'
data/Gemfile.lock CHANGED
@@ -5,6 +5,7 @@ PATH
5
5
  generator_spec
6
6
  ibsciss-middleware
7
7
  pundit
8
+ request_store
8
9
  strings-case
9
10
  thread_safe
10
11
 
@@ -74,6 +75,8 @@ GEM
74
75
  rainbow (3.0.0)
75
76
  rake (13.0.6)
76
77
  regexp_parser (2.1.1)
78
+ request_store (1.5.0)
79
+ rack (>= 1.4)
77
80
  rexml (3.2.5)
78
81
  rspec (3.10.0)
79
82
  rspec-core (~> 3.10.0)
@@ -117,6 +120,7 @@ DEPENDENCIES
117
120
  generator_spec
118
121
  ibsciss-middleware
119
122
  nexus_cqrs!
123
+ request_store
120
124
  rspec
121
125
  rubocop
122
126
  rubocop-shopify (~> 1.0.4)
@@ -22,8 +22,15 @@ module NexusCqrs
22
22
 
23
23
  # check entity-specific permissions
24
24
  unless permission_model.nil?
25
- return true if permission_model.where(permission: permission_key, entity_id: entity_id,
26
- user_id: @user_id.id).exists?
25
+
26
+ # get all permissions for this entity. NOTE: This will be cached per-request.
27
+ permissions = cached_permissions(permission_model)
28
+
29
+ # if there are no permissions for this entity and user, return false
30
+ return false if permissions[entity_id].nil?
31
+
32
+ # if the permission key is in the user's permissions for this entity, return true
33
+ return true if permissions[entity_id].include?(permission_key)
27
34
  end
28
35
 
29
36
  false
@@ -35,7 +42,7 @@ module NexusCqrs
35
42
  # @param [Integer] entity_id ID of the entity
36
43
  # @return [Array] Returns an array of hashes representing permission keys, along with their global status
37
44
  # @example Get a list of permissions
38
- # permissions.for_user(CollectionPermissions, collection.id) #=>
45
+ # permissions.for_user_on_entity(CollectionPermissions, collection.id) #=>
39
46
  # [
40
47
  # {:global=>false, :key=>"collection:discard"},
41
48
  # {:global=>false, :key=>"collection:publish"},
@@ -56,6 +63,35 @@ module NexusCqrs
56
63
  (global_permissions + entity_permissions).uniq { |p| p[:key] }
57
64
  end
58
65
 
66
+ # Retrieves a list of permissions assigned to a user for ANY entity ID
67
+ #
68
+ # @param [ApplicationRecord] permission_model Permission model
69
+ # @return [Hash] Returns a hash representing each entity and the user's permissions
70
+ # @example Get a list of permissions
71
+ # permissions.for_user(CollectionPermissions) #=>
72
+ # {
73
+ # 1 => ["collection:discard"],
74
+ # 2 => ["collection:discard"],
75
+ # 3 => ["collection:discard", "collection:edit"],
76
+ # 4 => ["collection:discard"],
77
+ # }
78
+ def for_user(permission_model)
79
+ return {} if @user_id.nil?
80
+
81
+ permissions = {}
82
+
83
+ # retrieve entity-specific permissions from DB and map to hash
84
+ permission_model.where(user_id: @user_id).each do |p|
85
+ if permissions[p.entity_id].nil?
86
+ permissions[p.entity_id] = [p.permission]
87
+ else
88
+ permissions[p.entity_id] << p.permission
89
+ end
90
+ end
91
+
92
+ permissions
93
+ end
94
+
59
95
  private
60
96
 
61
97
  def parse_permissions_array(permissions_array)
@@ -71,6 +107,15 @@ module NexusCqrs
71
107
 
72
108
  permissions
73
109
  end
110
+
111
+ # Retrieve all the permissions for this user for a specific model and cache it for this request (as all calls to
112
+ # a PermissionProvider in the same request will all have the same permissions, this saves on many SQL calls)
113
+ #
114
+ # @param [ApplicationRecord] permission_model Permission model
115
+ # @see PermissionProvider#for_user
116
+ def cached_permissions(permission_model)
117
+ ::RequestStore.store[:permissions] ||= for_user(permission_model)
118
+ end
74
119
  end
75
120
  end
76
121
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module NexusCqrs
3
- # Leave this as 0.4.2 in order for CI process to replace with the tagged version.
4
- VERSION = '0.4.2'
3
+ # Leave this as 0.4.3 in order for CI process to replace with the tagged version.
4
+ VERSION = '0.4.3'
5
5
  end
data/nexus_cqrs.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency('ibsciss-middleware')
24
24
  spec.add_dependency('pundit')
25
25
  spec.add_dependency('strings-case')
26
+ spec.add_dependency('request_store')
26
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_cqrs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Lovett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-18 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: generator_spec
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: request_store
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - dean.lovett@nexusmods.com
@@ -140,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
154
  - !ruby/object:Gem::Version
141
155
  version: '0'
142
156
  requirements: []
143
- rubygems_version: 3.2.31
157
+ rubygems_version: 3.2.32
144
158
  signing_key:
145
159
  specification_version: 4
146
160
  summary: Core package for the nexus cqrs gem