resource_policy 1.0.0 → 1.1.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: fed24d9cd11a66ed01e1c3502226d08552e28613059ebd7ec7df33b9516dd50b
4
- data.tar.gz: 46b4678e5d4c56bf3553869672e07fef6a31dca7a72b4c05633fa1845f79d0a1
3
+ metadata.gz: 2f65fc6083e96120d13e9f16f9209da6821690d5868fbb8c5e220af87c2cca00
4
+ data.tar.gz: 69f4d3e2969688640b28d0621625735caf47c4601aee45aa9f4b499bf2f7c708
5
5
  SHA512:
6
- metadata.gz: a92ff288f3b96e0733ffe975b44acace545db00526aca10da31aa947ae3c9f58e71458d88e76d24380f98cb438fab12edfac3d53e627ff53b4d9966bd39a95b3
7
- data.tar.gz: 4dd64068ede10a86e019872728f6d93522b266c6463e9853211067c5e2b990db6ecbd520d84230dec7a7ca784eed18bb0942e4be033f38aa600e09624564aebf
6
+ metadata.gz: d6a57863a8e23dc296549b227e612ae64837431d3f7f2512f795e0277e262d9eb3482f77a26844a4c028e97f4f21f3605616a52795e8088b62d32899baacfc4a
7
+ data.tar.gz: 42568d8e854a936150c590ca4691a7b40f69881de02fe909b53fa0f1ec5f5763731c51e0bf0f3eea91c83a4ef721be023eb9f4b5782e4bd2fd9d8b23a7cd6085
@@ -1,20 +1,18 @@
1
1
  name: Ruby
2
-
3
- on: [push]
4
-
2
+ on: [push, pull_request]
5
3
  jobs:
6
- build:
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
- - uses: actions/checkout@v1
12
- - name: Set up Ruby 2.6
13
- uses: actions/setup-ruby@v1
14
- with:
15
- ruby-version: 2.6.x
16
- - name: Build and test with Rake
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resource_policy (1.0.0)
4
+ resource_policy (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/docs/_sidebar.md CHANGED
@@ -4,3 +4,4 @@
4
4
  * [ActionsPolicy](components/actions_policy)
5
5
  * [ActionsPolicy](components/actions_validator)
6
6
  * [AttributesPolicy](components/attributes_policy)
7
+ * [ActionsPolicy](components/attributes_validator)
@@ -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
+
@@ -2,4 +2,5 @@
2
2
 
3
3
  # Incudes resource policy and rails specific helpers
4
4
  require 'resource_policy'
5
- require 'resource_policy/validators/action_policy_validator'
5
+ require 'resource_policy/validators/action_validator'
6
+ require 'resource_policy/validators/attributes_validator'
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ResourcePolicy
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  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.0.0
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/action_policy_validator.rb
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.0.0/CHANGELOG.md
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: