passpartu 0.5.5 → 0.6.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: 34188ab985989f3022e6fdcefa8ebdae5140e954e3e92ae59483cf29e523b30d
4
- data.tar.gz: 8268980e9fe35799d4409205d902691c9a7127e33f1a8638138d82a53612a01a
3
+ metadata.gz: f1074ae228e7b5f0cfff3a9a2fc0acd58536a161451cab914b814e8b1bf44470
4
+ data.tar.gz: 4ecf5e75ec63192afd3d739b47dd646912de8b594f01e8553fd6f9df0d1dbfb6
5
5
  SHA512:
6
- metadata.gz: 97826c98b5cb98587b30fe29bedb5c7e8c20ee6e2de9c76f3d7ab198ba5fd25e5cb7b68f2b4848f8b50e6447ca4463eebf3988e3339931c91dd87ddc01a0b9a1
7
- data.tar.gz: 5383f59ecb8aca48eb483670cb051428c529845891c26e5f45534cb550e4db761cde0f6a894945b695314c5f418ea2a33fe10333598c64cd7e5ee51d3df4ac49
6
+ metadata.gz: ceeac19430a29384cf70bf5755ed6786e06f76d981d1c42e81a677f8163211f46bc6a4bdb4eca6d9968cc7751d0c234392edfe6227c2dfd2c0c2f1132ab36cb0
7
+ data.tar.gz: 5d4aef534482fc686033d67c043e8378dc92c8bfecbb00672d81949fa5a63ccba6c36c3fd492267375a38854085eae4f13af6da8aa20c3df7e1c1c935ee39c40
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Passpartu v0.5.5 - [changelog](https://github.com/coaxsoft/passpartu/blob/master/CHANGELOG.md)
1
+ # Passpartu v0.6.0 - [changelog](https://github.com/coaxsoft/passpartu/blob/master/CHANGELOG.md)
2
2
 
3
3
  Passpartu makes policies great again (works awesome with [Pundit](https://rubygems.org/gems/pundit)).
4
4
 
@@ -31,7 +31,7 @@ NOTE: Your `User` model must respond to `role` method that returns a string or a
31
31
  Keep all your policies in one place.
32
32
  Create `./config/passpartu.yml` and start writing your policies.
33
33
 
34
- #### Example of `passpartu.yml`
34
+ #### Example of `passpartu.yml`
35
35
  ```yml
36
36
  # ./config/passpartu.yml
37
37
  manager: &manager
@@ -70,6 +70,25 @@ It's possible to use `crud` key to set values for `create`, `read`, `update`, `d
70
70
  `create`, `read`, `update`, `delete` has higher priority than `crud`
71
71
  In case `crud: true` and `delete: false` - result `false`
72
72
 
73
+ #### Only
74
+ It's possible to include specific roles to checks
75
+ ```ruby
76
+ user_admin.can?(:orders, :edit) # check policy for admin and returns true if policy true
77
+ user_admin.can?(:orders, :edit, only: :admin) # returns true because the user is an admin and we included only admin
78
+ user_manager.can?(:orders, :edit, only: :admin) # returns false because user is manager and we included only admin
79
+ ```
80
+ It's possible to give an array as only attribute
81
+
82
+ ```ruby
83
+ user_admin.can?(:orders, :edit, only: [:admin, :manager]) # returns true
84
+ user_manager.can?(:orders, :edit, only: [:admin, :manager]) # returns true
85
+ ```
86
+
87
+ Note: `only` has higher priority than `except/skip`. Do not use both.
88
+ ```ruby
89
+ user_admin.can?(:orders, :edit, only: :admin, except: :admin) # returns true
90
+ ```
91
+
73
92
 
74
93
  #### Skip (except)
75
94
  It's possible to exclude roles from checks
@@ -90,7 +109,7 @@ It's possible to give an array as except attribute
90
109
  Note: `expect` has higher priority than `skip`. Do not use both.
91
110
  ```ruby
92
111
  user_agent.can?(:orders, :edit, except: [:admin, :manager]) { user_agent.orders.include?(order) }
93
- # equals to
112
+ # equals to
94
113
  user_agent.can?(:orders, :edit, skip: [:admin, :manager]) { user_agent.orders.include?(order) }
95
114
  ```
96
115
 
@@ -183,7 +202,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
183
202
 
184
203
  ## Contributing
185
204
 
186
- Bug reports and pull requests are welcome on GitHub at https://github.com/OrestF/passpartu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
205
+ Bug reports and pull requests are welcome on GitHub at https://github.com/coaxsoft/passpartu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
187
206
 
188
207
  ## License
189
208
 
@@ -11,13 +11,17 @@ module Passpartu
11
11
 
12
12
  def call
13
13
  klass.class_eval do
14
- define_method('can?') do |*keys, except: nil, skip: nil, &block|
15
- Passpartu::BlockVerify.call(role, keys, except: except, skip: skip, &block)
14
+ define_method('can?') do |*keys, only: nil, except: nil, skip: nil, &block|
15
+ Passpartu::BlockVerify.call(role, keys, only: only, except: except, skip: skip, &block)
16
16
  end
17
17
 
18
18
  Passpartu.policy.keys.each do |policy_role|
19
- define_method("#{policy_role}_can?") do |*keys, except: nil, skip: nil, &block|
20
- role.to_s == policy_role && Passpartu::BlockVerify.call(role, keys, except: except, skip: skip, &block)
19
+ define_method("#{policy_role}_can?") do |*keys, only: nil, except: nil, skip: nil, &block|
20
+ role.to_s == policy_role && Passpartu::BlockVerify.call(role, keys,
21
+ only: only,
22
+ except: except,
23
+ skip: skip,
24
+ &block)
21
25
  end
22
26
  end
23
27
  end
@@ -2,22 +2,24 @@ module Passpartu
2
2
  class Verify
3
3
  CRUD_KEY = 'crud'.freeze
4
4
 
5
- attr_reader :role, :keys, :result, :except, :block
6
- def initialize(role, keys, except, skip, block)
5
+ attr_reader :role, :keys, :result, :only, :except, :block
6
+
7
+ def initialize(role, keys, only, except, skip, block)
7
8
  exclusion = except || skip # alias
8
9
 
9
10
  @role = role.to_s
10
11
  @keys = keys.map(&:to_s)
11
- @except = Array(exclusion).map(&:to_s) if present?(exclusion)
12
+ @only = Array(only).map(&:to_s) if present?(only)
13
+ @except = Array(exclusion).map(&:to_s) if present?(exclusion) && !@only
12
14
  @block = block
13
15
  end
14
16
 
15
- def self.call(role, keys, except: nil, skip: nil, &block)
16
- new(role, keys, except, skip, block).call
17
+ def self.call(role, keys, only: nil, except: nil, skip: nil, &block)
18
+ new(role, keys, only, except, skip, block).call
17
19
  end
18
20
 
19
21
  def call
20
- return false if role_excepted?
22
+ return false if role_ignore?
21
23
 
22
24
  check_policy
23
25
  check_crud if policy_missed? && last_key_crud?
@@ -27,10 +29,11 @@ module Passpartu
27
29
 
28
30
  private
29
31
 
30
- def role_excepted?
31
- return false if blank?(except)
32
+ def role_ignore?
33
+ return !only.include?(role) if present?(only)
34
+ return except.include?(role) if present?(except)
32
35
 
33
- except.include?(role)
36
+ false
34
37
  end
35
38
 
36
39
  def check_policy
@@ -1,3 +1,3 @@
1
1
  module Passpartu
2
- VERSION = '0.5.5'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passpartu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-23 00:00:00.000000000 Z
11
+ date: 2019-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,8 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.7.7
92
+ rubygems_version: 3.0.1
94
93
  signing_key:
95
94
  specification_version: 4
96
95
  summary: Passpartu makes policies great again