ez-permissions 0.3.0 → 0.4.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: 2561daddeb0d81e3d7916f64a0d334eeb181cc1bc1244fbfd96090abb6d0b5a3
4
- data.tar.gz: 648bd08668a2def0f908fa5be6125fbce12d186a760f1c2e51bc87b1e935041d
3
+ metadata.gz: e66c7c3aaf11e53f4f1049546d344eef5ef7ef0c4ab2af0ffb8940d11a64afa1
4
+ data.tar.gz: 6833c0293e997dd7d6f9f82d1c7794f1197f5167012254260b1d0f763e0bc32c
5
5
  SHA512:
6
- metadata.gz: a43ee4b3d8416de6d87e2dec796528698a3dc51e44e1b4357153f59081033ebdf8525fb9acf9c4b3263df7a745292ba9a7095a93dd119624f7844c18a5e4d3cf
7
- data.tar.gz: f945fff2beff8c9543e8e6d1fa8742c8e8dc5259d055b82b31930010be0e1f37a2d0196af9b57855e7c786b3a87406b5d5b532a28039abf68726f5abe19e76e9
6
+ metadata.gz: d99a291847509881c4d11c804fd1b92a9bfc62da9f28d4f802bfcc0d3b61fcd15dc6da4ad980ef6b2ee861b9c28806b09e194b7160b4e74a65a5d54716758977
7
+ data.tar.gz: c20c12f67b68b8bac4f88402eeff521d06eb6e731c2811385ab20d64d9dc2f334003acc03ab512daf61cce6adcaef37ef8e58678747aff3e449e1518c3559e30
data/README.md CHANGED
@@ -191,6 +191,28 @@ Permissions.can?(user, :create, :users) => # true
191
191
  Permissions.can?(user, :create, :users, scoped: project) => # false
192
192
  ```
193
193
 
194
+ ### Caching user permissions
195
+
196
+ 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
197
+
198
+ ```ruby
199
+ user_permissions = Permissions.model_permissions_map(user)
200
+ user_permissions # => #<Ez::Permissions::API::Authorize::ModelPermissions...
201
+
202
+ # You can fetch permissions as a hash
203
+ user_permissions.permissions_map # => { :read_users => true}
204
+
205
+ # and the in your code just fetch by the key:
206
+ if user_permissions.permissions_map[:read_users]
207
+ # execute authrorized code
208
+ end
209
+
210
+ # or user #can? and #authorize! helper methods
211
+ user_permissions.can?(:read, :users) # => true
212
+ user_permissions.can?(:create, :users) # => false
213
+ user_permissions.authorize!(:create, :users) # => raise Ez::Permissions::NotAuthorized
214
+ ```
215
+
194
216
  ### Testing
195
217
  EzPermissions ships with bunch of RSpec helper methods that helps mock permission.
196
218
  For large test suite (more than 5000 specs) it saves up to 30% of test runs time.
@@ -1,9 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'authorize/model_permissions'
4
+
3
5
  module Ez
4
6
  module Permissions
5
7
  module API
6
8
  module Authorize
9
+ def model_permissions(model)
10
+ ModelPermissions.new(
11
+ model.permissions.each_with_object({}) do |permission, acum|
12
+ acum["#{permission.action}_#{permission.resource}".to_sym] = true
13
+ end
14
+ )
15
+ end
16
+
7
17
  def authorize!(model, *actions, resource, scoped: nil, &block)
8
18
  authorize(model, *actions, resource, scoped: scoped, raise_exception: true, &block)
9
19
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ez
4
+ module Permissions
5
+ module API
6
+ module Authorize
7
+ class ModelPermissions
8
+ attr_reader :permissions_map
9
+
10
+ def initialize(permissions_map)
11
+ @permissions_map = permissions_map
12
+ end
13
+
14
+ def can?(action_name, resource_name)
15
+ permissions_map[to_key(action_name, resource_name)] == true
16
+ end
17
+
18
+ def authorize!(action_name, resource_name)
19
+ permissions_map.fetch(to_key(action_name, resource_name))
20
+ rescue KeyError => ex
21
+ raise Ez::Permissions::NotAuthorizedError
22
+ end
23
+
24
+ private
25
+
26
+ def to_key(action_name, resource_name)
27
+ "#{action_name}_#{resource_name}".to_sym
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ez
4
4
  module Permissions
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ez-permissions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk
@@ -176,6 +176,7 @@ files:
176
176
  - lib/ez/permissions.rb
177
177
  - lib/ez/permissions/api.rb
178
178
  - lib/ez/permissions/api/authorize.rb
179
+ - lib/ez/permissions/api/authorize/model_permissions.rb
179
180
  - lib/ez/permissions/api/models.rb
180
181
  - lib/ez/permissions/api/permissions.rb
181
182
  - lib/ez/permissions/api/roles.rb