ez-permissions 0.6.0 → 0.8.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: bc03e8e58f184793c6e903393db062e06d8a8887fe4887f455807d4c624cbaa0
4
- data.tar.gz: f6b5695a2a45a9908846b72b09c75455605cc28d25583cdc8439c14a8479a538
3
+ metadata.gz: 14233de27c1a87232242a256615faa9443996ce14e9b56129b1afdc38dfd7816
4
+ data.tar.gz: 8a8244a9cd54adbbed779753187de42ec20b3b84450f83af916ea3307b61711f
5
5
  SHA512:
6
- metadata.gz: 76ab990a4c789b1da51a2265ec5af08f4fad689a5a958be1b947c4cff33e7cd18dfe035f4a375f0fa8df616481dbf2401273d6eba36164a1cdae844ea6925852
7
- data.tar.gz: 995f632e48f0bc79d6785cd7ad27fea4e9b6bf36c7aace149729d573ab25aa0f0b16d6a7bce39fa38f33ae50b3a6d9fe994d8785452890823a58f45efc65e097
6
+ metadata.gz: 177882488f847bf944c1fba31e35e269ad2a7135012012d1ad7584f0956794127a464b735ada592a9fee6bf0fcf01095918908a1096e77f65112b005e92f5393
7
+ data.tar.gz: 294759120b1ea3e348e0f65b3dcc4079299986834ee6c1d14a81d23bf8c1e19c85dda262ea8b6096911269daaa297199f1dd1bd5ef52cedcb57947465ff5e632
data/README.md CHANGED
@@ -31,7 +31,7 @@ Configuration interface allows you to change default behavior
31
31
  ```ruby
32
32
  Ez::Permissions.configure do |config|
33
33
  # If in generated migrations you changed table names, please configure them here:
34
- config.permission_table_name = 'my_permissions'
34
+ config.permissions_table_name = 'my_permissions'
35
35
  config.roles_table_name = 'my_roles'
36
36
  config.models_roles_table_name = 'my_model_roles'
37
37
  config.permissions_roles_table_name = 'my_permissions_roles'
@@ -157,7 +157,7 @@ Permissions.list_by_role(:manager, scoped: project)
157
157
  # Create a role
158
158
  Permissions.create_role(:user)
159
159
 
160
- # Grant role's possibility to have action per resource
160
+ # Grant role's ability to have action per resource
161
161
  Permissions.grant_permission(:user, :read, :projects)
162
162
 
163
163
  # Grant all defined actions per resource
@@ -200,7 +200,7 @@ Permissions.can?(user, :create, :users, scoped: project) => # false
200
200
  If in one HTTP request (e.g. navigation menu rendering) you don't want to hit the database with dozens of queries, you can cache all user permission in a hash
201
201
 
202
202
  ```ruby
203
- user_permissions = Permissions.model_permissions_map(user)
203
+ user_permissions = Permissions.model_permissions(user)
204
204
  user_permissions # => #<Ez::Permissions::API::Authorize::ModelPermissions...
205
205
 
206
206
  # You can fetch permissions as a hash
@@ -214,6 +214,7 @@ end
214
214
  # or user #can? and #authorize! helper methods
215
215
  user_permissions.can?(:read, :users) # => true
216
216
  user_permissions.can?(:create, :users) # => false
217
+ user_permissions.can?(:create, :users, scoped: project) # => false
217
218
  user_permissions.authorize!(:create, :users) # => raise Ez::Permissions::NotAuthorized
218
219
  ```
219
220
 
@@ -280,8 +281,6 @@ Of course, you can use them as mixins, but it's up to you.
280
281
 
281
282
  ## TODO
282
283
  - [ ] Add helper methods for seed grant permissions
283
- - [ ] Cached permissions. If single UI has multiple checks for one user - we can cache it!
284
- - [ ] Not all permissions should be manageable through UI, like roles and permissions.
285
284
 
286
285
  ## Contributing
287
286
  Contribution directions go here.
@@ -5,7 +5,7 @@ module Ez
5
5
  class Role < ApplicationRecord
6
6
  self.table_name = Ez::Permissions.config.roles_table_name
7
7
 
8
- has_and_belongs_to_many :permissions
8
+ has_and_belongs_to_many :permissions, join_table: Ez::Permissions.config.permissions_roles_table_name
9
9
 
10
10
  validates :name, presence: true
11
11
  validates :name, uniqueness: true
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ez
4
+ module Permissions
5
+ module API
6
+ module Authorize
7
+ class GodmodPermissions < ModelPermissions
8
+ def can?(_action_name, _resource_name, **)
9
+ true
10
+ end
11
+
12
+ def authorize!(_action_name, _resource_name, **)
13
+ true
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -11,20 +11,21 @@ module Ez
11
11
  @permissions_map = permissions_map
12
12
  end
13
13
 
14
- def can?(action_name, resource_name)
15
- permissions_map[to_key(action_name, resource_name)] == true
14
+ def can?(action_name, resource_name, scoped: nil)
15
+ permissions_map[to_key(action_name, resource_name, scoped)] == true
16
16
  end
17
17
 
18
- def authorize!(action_name, resource_name)
19
- permissions_map.fetch(to_key(action_name, resource_name))
18
+ def authorize!(action_name, resource_name, scoped: nil)
19
+ permissions_map.fetch(to_key(action_name, resource_name, scoped))
20
20
  rescue KeyError
21
21
  raise Ez::Permissions::NotAuthorizedError
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def to_key(action_name, resource_name)
27
- "#{action_name}_#{resource_name}".to_sym
26
+ def to_key(action_name, resource_name, scoped = nil)
27
+ scoped_key = [scoped&.class, scoped&.id].compact.join('_')
28
+ "#{action_name}_#{resource_name}_#{scoped_key}".to_sym
28
29
  end
29
30
  end
30
31
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'authorize/model_permissions'
4
+ require_relative 'authorize/godmode_permissions'
4
5
 
5
6
  module Ez
6
7
  module Permissions
@@ -8,12 +9,19 @@ module Ez
8
9
  module Authorize
9
10
  def model_permissions(model)
10
11
  ModelPermissions.new(
11
- model.permissions.each_with_object({}) do |permission, acum|
12
- acum["#{permission.action}_#{permission.resource}".to_sym] = true
12
+ model.class.includes(assigned_roles: { role: :permissions }).find(model.id).assigned_roles.each_with_object({}) do |assigned_role, acum|
13
+ scoped_key = [assigned_role.scoped_type, assigned_role.scoped_id].compact.join('_')
14
+ assigned_role.role.permissions.each do |permission|
15
+ acum["#{permission.action}_#{permission.resource}_#{scoped_key}".to_sym] = true
16
+ end
13
17
  end
14
18
  )
15
19
  end
16
20
 
21
+ def godmode_permissions
22
+ GodmodPermissions.new({})
23
+ end
24
+
17
25
  def authorize!(model, *actions, resource, scoped: nil, &block)
18
26
  authorize(model, *actions, resource, scoped: scoped, raise_exception: true, &block)
19
27
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ez
4
4
  module Permissions
5
- VERSION = '0.6.0'
5
+ VERSION = '0.8.0'
6
6
  end
7
7
  end
@@ -8,7 +8,7 @@ module Ez
8
8
  "# frozen_string_literal: true
9
9
 
10
10
  Ez::Permissions.configure do |config|
11
- # config.permission_table_name = 'ez_permissions_permissions'
11
+ # config.permissions_table_name = 'ez_permissions_permissions'
12
12
  # config.roles_table_name = 'ez_permissions_roles'
13
13
  # config.models_roles_table_name = 'ez_permissions_model_roles'
14
14
  # config.permissions_roles_table_name = 'ez_permissions_permissions_roles'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ez-permissions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-01 00:00:00.000000000 Z
11
+ date: 2022-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ez-core
@@ -31,9 +31,6 @@ dependencies:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '5.2'
34
- - - "<="
35
- - !ruby/object:Gem::Version
36
- version: '7.0'
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
@@ -41,9 +38,6 @@ dependencies:
41
38
  - - ">="
42
39
  - !ruby/object:Gem::Version
43
40
  version: '5.2'
44
- - - "<="
45
- - !ruby/object:Gem::Version
46
- version: '7.0'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: bundler
49
43
  requirement: !ruby/object:Gem::Requirement
@@ -176,6 +170,7 @@ files:
176
170
  - lib/ez/permissions.rb
177
171
  - lib/ez/permissions/api.rb
178
172
  - lib/ez/permissions/api/authorize.rb
173
+ - lib/ez/permissions/api/authorize/godmode_permissions.rb
179
174
  - lib/ez/permissions/api/authorize/model_permissions.rb
180
175
  - lib/ez/permissions/api/models.rb
181
176
  - lib/ez/permissions/api/permissions.rb