resource_policy 1.0.0 → 1.1.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 +13 -15
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/docs/_sidebar.md +1 -0
- data/docs/components/attributes_validator.md +37 -0
- data/lib/resource_policy/rails.rb +2 -1
- data/lib/resource_policy/validators/{action_policy_validator.rb → action_validator.rb} +0 -0
- data/lib/resource_policy/validators/attributes_validator.rb +68 -0
- data/lib/resource_policy/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f65fc6083e96120d13e9f16f9209da6821690d5868fbb8c5e220af87c2cca00
|
4
|
+
data.tar.gz: 69f4d3e2969688640b28d0621625735caf47c4601aee45aa9f4b499bf2f7c708
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6a57863a8e23dc296549b227e612ae64837431d3f7f2512f795e0277e262d9eb3482f77a26844a4c028e97f4f21f3605616a52795e8088b62d32899baacfc4a
|
7
|
+
data.tar.gz: 42568d8e854a936150c590ca4691a7b40f69881de02fe909b53fa0f1ec5f5763731c51e0bf0f3eea91c83a4ef721be023eb9f4b5782e4bd2fd9d8b23a7cd6085
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
name: Ruby
|
2
|
-
|
3
|
-
on: [push]
|
4
|
-
|
2
|
+
on: [push, pull_request]
|
5
3
|
jobs:
|
6
|
-
|
4
|
+
specs:
|
5
|
+
strategy:
|
6
|
+
matrix:
|
7
|
+
ruby-version: ['2.7', '3.0', '3.1']
|
7
8
|
|
8
9
|
runs-on: ubuntu-latest
|
9
|
-
|
10
|
+
env:
|
11
|
+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
10
12
|
steps:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
run: |
|
18
|
-
gem install bundler
|
19
|
-
bundle install --jobs 4 --retry 3
|
20
|
-
bundle exec rake
|
13
|
+
- uses: actions/checkout@v2
|
14
|
+
- uses: ruby/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby-version }}
|
17
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
18
|
+
- run: bundle exec rake
|
data/CHANGELOG.md
CHANGED
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
9
9
|
|
10
10
|
* Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE
|
11
11
|
|
12
|
+
## [1.1.0]
|
13
|
+
|
14
|
+
* Added AttributesValidator
|
15
|
+
|
12
16
|
## [1.0.0]
|
13
17
|
|
14
18
|
* Added Ruby on Rails validator
|
data/Gemfile.lock
CHANGED
data/docs/_sidebar.md
CHANGED
@@ -0,0 +1,37 @@
|
|
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 either :read or :write.
|
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
|
+
|
File without changes
|
@@ -0,0 +1,68 @@
|
|
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
|
+
add_missing_policy_error_for(record, attribute: hash_attribute)
|
44
|
+
elsif !attribute_policy.allowed_to?(access_level)
|
45
|
+
add_not_permitted_error_for(record, attribute: hash_attribute)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def access_level
|
50
|
+
@access_level ||= options.fetch(:allowed_to)
|
51
|
+
end
|
52
|
+
|
53
|
+
def hash_value_for(record)
|
54
|
+
record.send(options.fetch(:apply_to))
|
55
|
+
end
|
56
|
+
|
57
|
+
def add_missing_policy_error_for(record, attribute:)
|
58
|
+
record.errors.add(attribute, 'does not have attribute policy defined')
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_not_permitted_error_for(record, attribute:)
|
62
|
+
record.errors.add(
|
63
|
+
attribute,
|
64
|
+
"attribute action #{access_level.to_s.inspect} is not allowed"
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resource_policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Povilas Jurcys
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- docs/components/action_validator.md
|
165
165
|
- docs/components/actions_policy.md
|
166
166
|
- docs/components/attributes_policy.md
|
167
|
+
- docs/components/attributes_validator.md
|
167
168
|
- docs/components/policy.md
|
168
169
|
- docs/index.html
|
169
170
|
- lib/resource_policy.rb
|
@@ -180,7 +181,8 @@ files:
|
|
180
181
|
- lib/resource_policy/policy/policy_configuration.rb
|
181
182
|
- lib/resource_policy/protected_resource.rb
|
182
183
|
- lib/resource_policy/rails.rb
|
183
|
-
- lib/resource_policy/validators/
|
184
|
+
- lib/resource_policy/validators/action_validator.rb
|
185
|
+
- lib/resource_policy/validators/attributes_validator.rb
|
184
186
|
- lib/resource_policy/version.rb
|
185
187
|
- resource_policy.gemspec
|
186
188
|
homepage: https://github.com/samesystem/resource_policy
|
@@ -189,7 +191,7 @@ licenses:
|
|
189
191
|
metadata:
|
190
192
|
homepage_uri: https://github.com/samesystem/resource_policy
|
191
193
|
source_code_uri: https://github.com/samesystem/resource_policy
|
192
|
-
changelog_uri: https://github.com/samesystem/resource_policy/blob/v1.
|
194
|
+
changelog_uri: https://github.com/samesystem/resource_policy/blob/v1.1.0/CHANGELOG.md
|
193
195
|
post_install_message:
|
194
196
|
rdoc_options: []
|
195
197
|
require_paths:
|