resource_policy 1.0.0 → 1.2.0
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/.github/workflows/ruby.yml +16 -15
- data/.ruby-version +1 -1
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +224 -168
- data/docs/README.md +12 -11
- data/docs/_sidebar.md +2 -1
- data/docs/components/action_validator.md +28 -2
- data/docs/components/actions_policy.md +4 -4
- data/docs/components/attributes_policy.md +1 -1
- data/docs/components/attributes_validator.md +75 -0
- data/docs/components/policy.md +43 -15
- data/lib/resource_policy/policy/actions_policy/actions_policy_model.rb +8 -0
- data/lib/resource_policy/policy/attributes_policy/attributes_policy_model.rb +7 -11
- data/lib/resource_policy/policy/policy_configuration.rb +6 -0
- data/lib/resource_policy/protected_resource.rb +4 -8
- data/lib/resource_policy/rails.rb +2 -1
- data/lib/resource_policy/validators/action_validator.rb +71 -0
- data/lib/resource_policy/validators/attributes_validator.rb +79 -0
- data/lib/resource_policy/version.rb +1 -1
- data/resource_policy.gemspec +2 -2
- metadata +11 -14
- data/.travis.yml +0 -7
- data/lib/resource_policy/validators/action_policy_validator.rb +0 -54
|
@@ -11,8 +11,8 @@ class UserPolicy
|
|
|
11
11
|
include ResourcePolicy::Policy
|
|
12
12
|
|
|
13
13
|
policy do |c|
|
|
14
|
-
c.action(:
|
|
15
|
-
c.action(:
|
|
14
|
+
c.action(:show).allowed
|
|
15
|
+
c.action(:update).allowed(if: %i[admin? admin?])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def initialize(user, current_user)
|
|
@@ -35,8 +35,8 @@ If no condition is given then action will be always allowed.
|
|
|
35
35
|
|
|
36
36
|
This config means:
|
|
37
37
|
|
|
38
|
-
* `
|
|
39
|
-
* `
|
|
38
|
+
* `show` action is always allowed;
|
|
39
|
+
* `update` action is allowed only if both `admin?` and `writable?` methods returns `true`.
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
### policy#group
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# ResourcePolicy::AttributesValidator
|
|
2
|
+
|
|
3
|
+
`ResourcePolicy::AttributesValidator` is a validator that validates the attributes of an object to ensure they comply with specified policies. The validator can be used to validate a hash of attributes with the `apply_to` option and the desired access level with the `allowed_to` option.
|
|
4
|
+
|
|
5
|
+
## Options
|
|
6
|
+
|
|
7
|
+
The validates method requires two options:
|
|
8
|
+
|
|
9
|
+
- `:apply_to` (required) - The name of the method that returns the hash that needs to be validated.
|
|
10
|
+
- `:allowed_to` (required) - The access level that we need to check. This can be Symbol, String or Proc.
|
|
11
|
+
|
|
12
|
+
## Usage example
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
class SomeClass
|
|
16
|
+
include ActiveModel::Validations
|
|
17
|
+
validates :some_policy, 'resource_policy/attributes': { apply_to: :some_params, allowed_to: :write }
|
|
18
|
+
|
|
19
|
+
def some_policy
|
|
20
|
+
SomePolicy.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def some_params
|
|
24
|
+
{ foo: :foo, bar: :bar }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
some_object = SomeClass.new
|
|
29
|
+
if some_object.valid?
|
|
30
|
+
# No validation errors, continue with the process
|
|
31
|
+
else
|
|
32
|
+
some_object.errors.messages # => { foo: ['attribute action "write" is not allowed'], bar: ['attribute action "write" is not allowed'] }
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
In this example, the `SomeClass` has an attribute named `some_policy` which is being validated using the `ResourcePolicy::AttributesValidator`. The validator checks if attributes from the `some_params` satisfy access level conditions (such as `:write`). It adds an error for each hash key that does not satisfy policy conditions.
|
|
37
|
+
|
|
38
|
+
## Usage example using proc
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
class SomeClass < ActiveRecord::Base
|
|
42
|
+
validates :some_policy, 'resource_policy/attributes': { apply_to: :some_params, allowed_to: -> { custom_allowed_to } }
|
|
43
|
+
|
|
44
|
+
def some_policy
|
|
45
|
+
SomePolicy.new
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def some_params
|
|
49
|
+
{ foo: :foo, bar: :bar }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def custom_allowed_to
|
|
53
|
+
new_record? :write : :change
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
some_object = SomeClass.new
|
|
58
|
+
if some_object.valid?
|
|
59
|
+
# No validation errors, continue with the process
|
|
60
|
+
else
|
|
61
|
+
some_object.errors.messages # => { foo: ['attribute action "write" is not allowed'], bar: ['attribute action "write" is not allowed'] }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
new_object = SomeClass.new
|
|
65
|
+
new_object.valid?
|
|
66
|
+
new_object.errors.messages # => { foo: ['attribute action "write" is not allowed'], bar: ['attribute action "write" is not allowed'] }
|
|
67
|
+
|
|
68
|
+
existing_object = SomeClass.find(1)
|
|
69
|
+
existing_object.valid?
|
|
70
|
+
existing_object.errors.messages # => { foo: ['attribute action "change" is not allowed'], bar: ['attribute action "change" is not allowed'] }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
In this example, the `SomeClass` has an attribute named `some_policy` which is being validated using the `ResourcePolicy::AttributesValidator`. The validator checks if attributes from the `some_params` satisfy access level conditions (such as `:write`). It adds an error for each hash key that does not satisfy policy conditions.
|
|
74
|
+
|
|
75
|
+
In this example, the `SomeClass` has an attribute named `some_policy` which is validated using the `ResourcePolicy::ActionValidator`. The validator calls `custom_allowed_to` method on our model to determine which access level to use. If record is new, it will check policy attributes for the `:write` access. If the record saved, it will check policy attributes for `:change` access level. It adds an error for each hash key that does not satisfy policy conditions.
|
data/docs/components/policy.md
CHANGED
|
@@ -13,14 +13,14 @@ class UserPolicy
|
|
|
13
13
|
policy do |c|
|
|
14
14
|
c.policy_target :user
|
|
15
15
|
|
|
16
|
-
c.action(:
|
|
16
|
+
c.action(:show).allowed(if: :readable?)
|
|
17
17
|
|
|
18
|
-
c.attribute(:
|
|
18
|
+
c.attribute(:email)
|
|
19
19
|
.allowed(:read, if: :readable?)
|
|
20
20
|
.allowed(:write, if: :writable?)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
def initialize(user, current_user
|
|
23
|
+
def initialize(user, current_user)
|
|
24
24
|
@user = user
|
|
25
25
|
@current_user = current_user
|
|
26
26
|
end
|
|
@@ -75,11 +75,11 @@ class UserPolicy
|
|
|
75
75
|
include ResourcePolicy::Policy
|
|
76
76
|
|
|
77
77
|
policy do |c|
|
|
78
|
-
c.action(:
|
|
79
|
-
c.action(:
|
|
78
|
+
c.action(:show).allowed # current_user can always see user
|
|
79
|
+
c.action(:update).allowed(if: :admin?) # only admin current_user can update user
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
-
def initialize(user, current_user
|
|
82
|
+
def initialize(user, current_user)
|
|
83
83
|
@user = user
|
|
84
84
|
@current_user = current_user
|
|
85
85
|
end
|
|
@@ -95,9 +95,9 @@ end
|
|
|
95
95
|
then we can check each action like this:
|
|
96
96
|
|
|
97
97
|
```ruby
|
|
98
|
-
policy = UserPolicy.new(user, current_user
|
|
99
|
-
policy.action(:
|
|
100
|
-
policy.action(:
|
|
98
|
+
policy = UserPolicy.new(user, current_user)
|
|
99
|
+
policy.action(:show).allowed? # => true
|
|
100
|
+
policy.action(:update).allowed? # ... depends on `admin?` result
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
## Policy#actions_policy
|
|
@@ -105,12 +105,30 @@ policy.action(:write).allowed? # ... depends on `admin?` result
|
|
|
105
105
|
Another way to check each action is to use `actions_policy` object like this:
|
|
106
106
|
|
|
107
107
|
```ruby
|
|
108
|
-
policy = UserPolicy.new(user, current_user
|
|
108
|
+
policy = UserPolicy.new(user, current_user)
|
|
109
109
|
actions_policy = policy.actions_policy
|
|
110
110
|
actions_policy.read.allowed? # => true
|
|
111
111
|
actions_policy.write.allowed? # ... depends on `admin?` result
|
|
112
112
|
```
|
|
113
113
|
|
|
114
|
+
In order to get all actions do:
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
policy = UserPolicy.new(user, current_user)
|
|
118
|
+
actions_policy = policy.actions_policy
|
|
119
|
+
|
|
120
|
+
actions_policy.actions
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
In order to get all only allowed action names do:
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
policy = UserPolicy.new(user, current_user)
|
|
127
|
+
actions_policy = policy.actions_policy
|
|
128
|
+
|
|
129
|
+
actions_policy.allowed_action_names
|
|
130
|
+
```
|
|
131
|
+
|
|
114
132
|
## Usage of Policy#attribute
|
|
115
133
|
|
|
116
134
|
Suppose we have policy like this:
|
|
@@ -125,7 +143,7 @@ class UserPolicy
|
|
|
125
143
|
.allowed(:write, if: :admin?) # only admin current_user can change email
|
|
126
144
|
end
|
|
127
145
|
|
|
128
|
-
def initialize(user, current_user
|
|
146
|
+
def initialize(user, current_user)
|
|
129
147
|
@user = user
|
|
130
148
|
@current_user = current_user
|
|
131
149
|
end
|
|
@@ -141,7 +159,7 @@ end
|
|
|
141
159
|
then we can check each attribute like this:
|
|
142
160
|
|
|
143
161
|
```ruby
|
|
144
|
-
policy = UserPolicy.new(user, current_user
|
|
162
|
+
policy = UserPolicy.new(user, current_user)
|
|
145
163
|
policy.attribute(:email).allowed_to?(:change) # => false - no such rule
|
|
146
164
|
policy.attribute(:email).readable? # => true
|
|
147
165
|
policy.attribute(:email).writable? # ... depends on `admin?` result
|
|
@@ -152,7 +170,7 @@ policy.attribute(:email).writable? # ... depends on `admin?` result
|
|
|
152
170
|
Another way to check each action is to use `attributes_policy` object like this:
|
|
153
171
|
|
|
154
172
|
```ruby
|
|
155
|
-
policy = UserPolicy.new(user, current_user
|
|
173
|
+
policy = UserPolicy.new(user, current_user)
|
|
156
174
|
attributes_policy = policy.attributes_policy
|
|
157
175
|
|
|
158
176
|
attributes_policy.email.allowed_to?(:change) # => false - no such rule
|
|
@@ -160,6 +178,16 @@ attributes_policy.email.readable? # same as `allowed_to?(:read)`
|
|
|
160
178
|
attributes_policy.email.writable? # same as `allowed_to?(:write)`
|
|
161
179
|
```
|
|
162
180
|
|
|
181
|
+
In order to get all allowed attributes do:
|
|
182
|
+
|
|
183
|
+
```ruby
|
|
184
|
+
policy = UserPolicy.new(user, current_user)
|
|
185
|
+
attributes_policy = policy.attributes_policy
|
|
186
|
+
|
|
187
|
+
readable_attributes = attributes_policy.all_allowed_to(:read)
|
|
188
|
+
writable_attributes = attributes_policy.all_allowed_to(:write)
|
|
189
|
+
```
|
|
190
|
+
|
|
163
191
|
## Usage of Policy#protected_resource
|
|
164
192
|
|
|
165
193
|
Policy provides `#protected_resource` method which returns wrapped model instance and does not allow to view fields which current_user does not have access to. You must define `policy_target` in order to be able to use `protected_resource` feature
|
|
@@ -176,7 +204,7 @@ class UserPolicy
|
|
|
176
204
|
c.attribute(:salary).allowed(:read, if: :admin?) # only visible to admin
|
|
177
205
|
end
|
|
178
206
|
|
|
179
|
-
def initialize(user, current_user
|
|
207
|
+
def initialize(user, current_user)
|
|
180
208
|
@user = user
|
|
181
209
|
@current_user = current_user
|
|
182
210
|
end
|
|
@@ -196,7 +224,7 @@ user = User.find(1337)
|
|
|
196
224
|
user.id #=> 1337
|
|
197
225
|
user.email #=> "john.doe@example.com"
|
|
198
226
|
|
|
199
|
-
policy = UserPolicy.new(user, current_user
|
|
227
|
+
policy = UserPolicy.new(user, current_user)
|
|
200
228
|
policy.protected_resource.id #=> 1337
|
|
201
229
|
policy.protected_resource.email # nil
|
|
202
230
|
```
|
|
@@ -18,6 +18,14 @@ module ResourcePolicy
|
|
|
18
18
|
policy_item(method_name.to_sym)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
def actions
|
|
22
|
+
config.actions.keys.map { |name| policy_item(name.to_sym) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def allowed_action_names
|
|
26
|
+
actions.select(&:allowed?).map(&:name)
|
|
27
|
+
end
|
|
28
|
+
|
|
21
29
|
def respond_to_missing?(method_name, *args)
|
|
22
30
|
config.actions.key?(method_name.to_sym) || super
|
|
23
31
|
end
|
|
@@ -13,9 +13,7 @@ module ResourcePolicy
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def all
|
|
16
|
-
@all ||= config.attributes.
|
|
17
|
-
policy_item(attribute_name)
|
|
18
|
-
end
|
|
16
|
+
@all ||= config.attributes.values.map { |attribute| policy_item(attribute) }
|
|
19
17
|
end
|
|
20
18
|
|
|
21
19
|
def all_allowed_to(status)
|
|
@@ -23,13 +21,14 @@ module ResourcePolicy
|
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
def method_missing(method_name)
|
|
26
|
-
|
|
24
|
+
attribute = config.configured_attribute(method_name)
|
|
25
|
+
return super unless attribute
|
|
27
26
|
|
|
28
|
-
policy_item(
|
|
27
|
+
policy_item(attribute)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
def respond_to_missing?(method_name, *args)
|
|
32
|
-
config.
|
|
31
|
+
!config.configured_attribute(method_name).nil? || super
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
private
|
|
@@ -40,11 +39,8 @@ module ResourcePolicy
|
|
|
40
39
|
policy.class.policy
|
|
41
40
|
end
|
|
42
41
|
|
|
43
|
-
def policy_item(
|
|
44
|
-
@policy_item_by_name[name] ||=
|
|
45
|
-
attribute = config.attributes.fetch(name)
|
|
46
|
-
AttributePolicy.new(attribute, policy: policy)
|
|
47
|
-
end
|
|
42
|
+
def policy_item(attribute)
|
|
43
|
+
@policy_item_by_name[attribute.name] ||= AttributePolicy.new(attribute, policy: policy)
|
|
48
44
|
end
|
|
49
45
|
end
|
|
50
46
|
end
|
|
@@ -46,6 +46,12 @@ module ResourcePolicy
|
|
|
46
46
|
@attributes.select { |_name, action| action.configured? }
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
# Same as `attributes[attribute_name.to_sym]`, without building the whole hash on every read.
|
|
50
|
+
def configured_attribute(attribute_name)
|
|
51
|
+
attribute = @attributes[attribute_name.to_sym]
|
|
52
|
+
attribute if attribute&.configured?
|
|
53
|
+
end
|
|
54
|
+
|
|
49
55
|
def actions
|
|
50
56
|
@actions.select { |_name, action| action.configured? }
|
|
51
57
|
end
|
|
@@ -24,16 +24,12 @@ module ResourcePolicy
|
|
|
24
24
|
attr_reader :policy
|
|
25
25
|
|
|
26
26
|
def target_respond_to?(method_name, *args)
|
|
27
|
-
|
|
28
|
-
policy_target.respond_to?(method_name, *args)
|
|
27
|
+
readable_attribute?(method_name) && policy_target.respond_to?(method_name, *args)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
def
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
attributes.map(&:name)
|
|
30
|
+
def readable_attribute?(name)
|
|
31
|
+
attribute = policy.class.policy.configured_attribute(name)
|
|
32
|
+
!attribute.nil? && attribute.defined_action?(:read)
|
|
37
33
|
end
|
|
38
34
|
|
|
39
35
|
def policy_target
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Available options:
|
|
4
|
+
#
|
|
5
|
+
# * `:allowed_to` (required) - action type which we need to check.
|
|
6
|
+
# * `:as` (optional) - key which will be used to display errors.
|
|
7
|
+
#
|
|
8
|
+
# Usage example:
|
|
9
|
+
#
|
|
10
|
+
# class SomeClass
|
|
11
|
+
# validates :some_policy, 'resource_policy/action': { allowed_to: :create, as: :some_item }
|
|
12
|
+
#
|
|
13
|
+
# def some_policy
|
|
14
|
+
# SomePolicy.new
|
|
15
|
+
# end
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
module ResourcePolicy
|
|
19
|
+
class ActionValidator < ActiveModel::EachValidator
|
|
20
|
+
def validate_each(record, default_attribute, policy)
|
|
21
|
+
validation_options = validation_options_for(record, default_attribute, policy)
|
|
22
|
+
|
|
23
|
+
validate_action_policy(**validation_options)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def validation_options_for(record, default_attribute, policy)
|
|
29
|
+
attribute = fetch_option_value(:as, record: record) { default_attribute }
|
|
30
|
+
action_name = fetch_option_value(:allowed_to, record: record)
|
|
31
|
+
action_policy = policy.action(action_name)
|
|
32
|
+
|
|
33
|
+
{
|
|
34
|
+
policy: action_policy,
|
|
35
|
+
record: record,
|
|
36
|
+
attribute: attribute,
|
|
37
|
+
action_name: action_name
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def validate_action_policy(policy:, record:, attribute:, action_name:)
|
|
42
|
+
if policy.nil?
|
|
43
|
+
add_missing_policy_error_for(record, attribute, action_name)
|
|
44
|
+
elsif !policy.allowed?
|
|
45
|
+
add_not_permitted_error_for(record, attribute, action_name)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def add_missing_policy_error_for(record, attribute, action_name)
|
|
50
|
+
record.errors.add(
|
|
51
|
+
attribute,
|
|
52
|
+
"does not have #{action_name.to_s.inspect} action policy defined"
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def add_not_permitted_error_for(record, attribute, action_name)
|
|
57
|
+
record.errors.add(
|
|
58
|
+
attribute,
|
|
59
|
+
"action #{action_name.to_s.inspect} is not allowed"
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def fetch_option_value(key, record:, &block)
|
|
64
|
+
value = options.fetch(key, &block)
|
|
65
|
+
|
|
66
|
+
return record.instance_exec(&value) if value.is_a?(Proc)
|
|
67
|
+
|
|
68
|
+
value
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Validates attributes hash.
|
|
4
|
+
#
|
|
5
|
+
# Available options:
|
|
6
|
+
#
|
|
7
|
+
# * `:apply_to` (required) - hash which needs to be validated using policy.
|
|
8
|
+
# * `:allowed_to` (required) - access level which we need to check. In most cases it's `:read` or `:write`.
|
|
9
|
+
#
|
|
10
|
+
# Usage example:
|
|
11
|
+
#
|
|
12
|
+
# class MyClass
|
|
13
|
+
# include ActiveModel::Validations
|
|
14
|
+
# validates :some_policy, 'resource_policy/attributes': { apply_to: :some_params, allowed_to: :write }
|
|
15
|
+
#
|
|
16
|
+
# def some_policy
|
|
17
|
+
# SomePolicy.new
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# def some_params
|
|
21
|
+
# { foo: :foo, bar: :bar }
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
module ResourcePolicy
|
|
26
|
+
class AttributesValidator < ActiveModel::EachValidator
|
|
27
|
+
def validate_each(record, _attribute, policy)
|
|
28
|
+
hash_value = hash_value_for(record)
|
|
29
|
+
|
|
30
|
+
hash_value.each_key do |hash_attribute|
|
|
31
|
+
validate_attribute_policy(
|
|
32
|
+
policy.attribute(hash_attribute),
|
|
33
|
+
record: record,
|
|
34
|
+
hash_attribute: hash_attribute
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def validate_attribute_policy(attribute_policy, record:, hash_attribute:)
|
|
42
|
+
if attribute_policy.nil?
|
|
43
|
+
return add_missing_policy_error_for(record, attribute: hash_attribute)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
access_level = access_level_for(record)
|
|
47
|
+
if !attribute_policy.allowed_to?(access_level)
|
|
48
|
+
return add_not_permitted_error_for(record, attribute: hash_attribute)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def access_level_for(record)
|
|
53
|
+
fetch_option_value(:allowed_to, record: record)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def hash_value_for(record)
|
|
57
|
+
record.send(options.fetch(:apply_to))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def add_missing_policy_error_for(record, attribute:)
|
|
61
|
+
record.errors.add(attribute, 'does not have attribute policy defined')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def add_not_permitted_error_for(record, attribute:)
|
|
65
|
+
record.errors.add(
|
|
66
|
+
attribute,
|
|
67
|
+
"attribute action #{access_level_for(record).to_s.inspect} is not allowed"
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def fetch_option_value(key, record:, &block)
|
|
72
|
+
value = options.fetch(key, &block)
|
|
73
|
+
|
|
74
|
+
return record.instance_exec(&value) if value.is_a?(Proc)
|
|
75
|
+
|
|
76
|
+
value
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
data/resource_policy.gemspec
CHANGED
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
|
|
|
35
35
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
36
36
|
spec.require_paths = ['lib']
|
|
37
37
|
|
|
38
|
-
spec.add_development_dependency 'bundler', '~>
|
|
38
|
+
spec.add_development_dependency 'bundler', '~> 4.0'
|
|
39
39
|
spec.add_development_dependency 'pry-byebug'
|
|
40
40
|
spec.add_development_dependency 'rake', '~> 13.0'
|
|
41
41
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
@@ -43,5 +43,5 @@ Gem::Specification.new do |spec|
|
|
|
43
43
|
spec.add_development_dependency 'rubocop-performance'
|
|
44
44
|
spec.add_development_dependency 'rubocop-rails'
|
|
45
45
|
spec.add_development_dependency 'rubocop-rspec'
|
|
46
|
-
spec.add_development_dependency 'rails', '~>
|
|
46
|
+
spec.add_development_dependency 'rails', '~> 8.0.0'
|
|
47
47
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: resource_policy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Povilas Jurcys
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
@@ -16,14 +15,14 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '4.0'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '4.0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: pry-byebug
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -128,15 +127,14 @@ dependencies:
|
|
|
128
127
|
requirements:
|
|
129
128
|
- - "~>"
|
|
130
129
|
- !ruby/object:Gem::Version
|
|
131
|
-
version:
|
|
130
|
+
version: 8.0.0
|
|
132
131
|
type: :development
|
|
133
132
|
prerelease: false
|
|
134
133
|
version_requirements: !ruby/object:Gem::Requirement
|
|
135
134
|
requirements:
|
|
136
135
|
- - "~>"
|
|
137
136
|
- !ruby/object:Gem::Version
|
|
138
|
-
version:
|
|
139
|
-
description:
|
|
137
|
+
version: 8.0.0
|
|
140
138
|
email:
|
|
141
139
|
- po.jurcys@gmail.com
|
|
142
140
|
executables: []
|
|
@@ -149,7 +147,6 @@ files:
|
|
|
149
147
|
- ".rspec"
|
|
150
148
|
- ".rubocop.yml"
|
|
151
149
|
- ".ruby-version"
|
|
152
|
-
- ".travis.yml"
|
|
153
150
|
- CHANGELOG.md
|
|
154
151
|
- CODE_OF_CONDUCT.md
|
|
155
152
|
- Gemfile
|
|
@@ -164,6 +161,7 @@ files:
|
|
|
164
161
|
- docs/components/action_validator.md
|
|
165
162
|
- docs/components/actions_policy.md
|
|
166
163
|
- docs/components/attributes_policy.md
|
|
164
|
+
- docs/components/attributes_validator.md
|
|
167
165
|
- docs/components/policy.md
|
|
168
166
|
- docs/index.html
|
|
169
167
|
- lib/resource_policy.rb
|
|
@@ -180,7 +178,8 @@ files:
|
|
|
180
178
|
- lib/resource_policy/policy/policy_configuration.rb
|
|
181
179
|
- lib/resource_policy/protected_resource.rb
|
|
182
180
|
- lib/resource_policy/rails.rb
|
|
183
|
-
- lib/resource_policy/validators/
|
|
181
|
+
- lib/resource_policy/validators/action_validator.rb
|
|
182
|
+
- lib/resource_policy/validators/attributes_validator.rb
|
|
184
183
|
- lib/resource_policy/version.rb
|
|
185
184
|
- resource_policy.gemspec
|
|
186
185
|
homepage: https://github.com/samesystem/resource_policy
|
|
@@ -189,8 +188,7 @@ licenses:
|
|
|
189
188
|
metadata:
|
|
190
189
|
homepage_uri: https://github.com/samesystem/resource_policy
|
|
191
190
|
source_code_uri: https://github.com/samesystem/resource_policy
|
|
192
|
-
changelog_uri: https://github.com/samesystem/resource_policy/blob/v1.
|
|
193
|
-
post_install_message:
|
|
191
|
+
changelog_uri: https://github.com/samesystem/resource_policy/blob/v1.2.0/CHANGELOG.md
|
|
194
192
|
rdoc_options: []
|
|
195
193
|
require_paths:
|
|
196
194
|
- lib
|
|
@@ -205,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
205
203
|
- !ruby/object:Gem::Version
|
|
206
204
|
version: '0'
|
|
207
205
|
requirements: []
|
|
208
|
-
rubygems_version:
|
|
209
|
-
signing_key:
|
|
206
|
+
rubygems_version: 4.0.16
|
|
210
207
|
specification_version: 4
|
|
211
208
|
summary: Access control for single resource and it's methods
|
|
212
209
|
test_files: []
|
data/.travis.yml
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Available options:
|
|
4
|
-
#
|
|
5
|
-
# * `:allowed_to` (required) - action type which we need to check.
|
|
6
|
-
# * `:as` (optional) - key which will be used to display errors.
|
|
7
|
-
#
|
|
8
|
-
# Usage example:
|
|
9
|
-
#
|
|
10
|
-
# class SomeClass
|
|
11
|
-
# validates :some_policy, 'resource_policy/action': { allowed_to: :create, as: :some_item }
|
|
12
|
-
#
|
|
13
|
-
# def some_policy
|
|
14
|
-
# SomePolicy.new
|
|
15
|
-
# end
|
|
16
|
-
# end
|
|
17
|
-
#
|
|
18
|
-
module ResourcePolicy
|
|
19
|
-
class ActionValidator < ActiveModel::EachValidator
|
|
20
|
-
def validate_each(record, default_attribute, policy)
|
|
21
|
-
attribute = options.fetch(:as, default_attribute)
|
|
22
|
-
action_policy = policy.action(action_name)
|
|
23
|
-
validate_action_policy(action_policy, record:, attribute:)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
private
|
|
27
|
-
|
|
28
|
-
def validate_action_policy(policy, record:, attribute:)
|
|
29
|
-
if policy.nil?
|
|
30
|
-
add_missing_policy_error_for(record, attribute:)
|
|
31
|
-
elsif !policy.allowed?
|
|
32
|
-
add_not_permitted_error_for(record, attribute:)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def action_name
|
|
37
|
-
@action_name ||= options.fetch(:allowed_to)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def add_missing_policy_error_for(record, attribute:)
|
|
41
|
-
record.errors.add(
|
|
42
|
-
attribute,
|
|
43
|
-
"does not have #{action_name.to_s.inspect} action policy defined"
|
|
44
|
-
)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def add_not_permitted_error_for(record, attribute:)
|
|
48
|
-
record.errors.add(
|
|
49
|
-
attribute,
|
|
50
|
-
"action #{action_name.to_s.inspect} is not allowed"
|
|
51
|
-
)
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|