resource_policy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +20 -0
  3. data/.gitignore +15 -0
  4. data/.hound.yml +3 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +43 -0
  7. data/.ruby-version +1 -0
  8. data/.travis.yml +7 -0
  9. data/CHANGELOG.md +19 -0
  10. data/CODE_OF_CONDUCT.md +74 -0
  11. data/Gemfile +11 -0
  12. data/Gemfile.lock +232 -0
  13. data/LICENSE.txt +21 -0
  14. data/Rakefile +6 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/docs/.nojekyll +0 -0
  18. data/docs/README.md +163 -0
  19. data/docs/_sidebar.md +6 -0
  20. data/docs/components/action_validator.md +34 -0
  21. data/docs/components/actions_policy.md +68 -0
  22. data/docs/components/attributes_policy.md +68 -0
  23. data/docs/components/policy.md +202 -0
  24. data/docs/index.html +70 -0
  25. data/lib/resource_policy/policy/action_policy_configuration.rb +37 -0
  26. data/lib/resource_policy/policy/actions_policy/action_policy.rb +32 -0
  27. data/lib/resource_policy/policy/actions_policy/actions_policy_model.rb +39 -0
  28. data/lib/resource_policy/policy/actions_policy.rb +35 -0
  29. data/lib/resource_policy/policy/attributes_policy/attribute_configuration.rb +72 -0
  30. data/lib/resource_policy/policy/attributes_policy/attribute_policy.rb +49 -0
  31. data/lib/resource_policy/policy/attributes_policy/attributes_policy_model.rb +52 -0
  32. data/lib/resource_policy/policy/attributes_policy.rb +58 -0
  33. data/lib/resource_policy/policy/merge_policies.rb +44 -0
  34. data/lib/resource_policy/policy/policy_configuration.rb +87 -0
  35. data/lib/resource_policy/policy.rb +31 -0
  36. data/lib/resource_policy/protected_resource.rb +43 -0
  37. data/lib/resource_policy/rails.rb +5 -0
  38. data/lib/resource_policy/validators/action_policy_validator.rb +54 -0
  39. data/lib/resource_policy/version.rb +5 -0
  40. data/lib/resource_policy.rb +11 -0
  41. data/resource_policy.gemspec +47 -0
  42. metadata +212 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fed24d9cd11a66ed01e1c3502226d08552e28613059ebd7ec7df33b9516dd50b
4
+ data.tar.gz: 46b4678e5d4c56bf3553869672e07fef6a31dca7a72b4c05633fa1845f79d0a1
5
+ SHA512:
6
+ metadata.gz: a92ff288f3b96e0733ffe975b44acace545db00526aca10da31aa947ae3c9f58e71458d88e76d24380f98cb438fab12edfac3d53e627ff53b4d9966bd39a95b3
7
+ data.tar.gz: 4dd64068ede10a86e019872728f6d93522b266c6463e9853211067c5e2b990db6ecbd520d84230dec7a7ca784eed18bb0942e4be033f38aa600e09624564aebf
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ 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
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.history/
10
+ /.vscode/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ *.gem
data/.hound.yml ADDED
@@ -0,0 +1,3 @@
1
+ rubocop:
2
+ config_file: .rubocop.yml
3
+ version: 0.75.0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ require: rubocop-rspec
2
+
3
+ RSpec/NestedGroups:
4
+ Enabled: false
5
+
6
+ Metrics/LineLength:
7
+ Enabled: true
8
+ Max: 120
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - spec/**/*.rb
13
+ - resource_policy.gemspec
14
+ Metrics/ModuleLength:
15
+ Exclude:
16
+ - spec/**/*_spec.rb
17
+ Metrics/ClassLength:
18
+ Exclude:
19
+ - spec/**/*_spec.rb
20
+
21
+ Lint/AmbiguousBlockAssociation:
22
+ Exclude:
23
+ - spec/**/*.rb
24
+
25
+ Naming/UncommunicativeMethodParamName:
26
+ AllowedNames:
27
+ - 'to'
28
+ - 'at'
29
+ - 'on'
30
+ - 'id'
31
+ - 'in'
32
+ - 'as'
33
+
34
+ Style/ClassAndModuleChildren:
35
+ Exclude:
36
+ - spec/**/*_spec.rb
37
+
38
+ AllCops:
39
+ TargetRubyVersion: 2.5
40
+ Exclude:
41
+ - bin/*
42
+ - graphql_rails.gemspec
43
+ - Rakefile
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ * Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE
11
+
12
+ ## [1.0.0]
13
+
14
+ * Added Ruby on Rails validator
15
+ * Fixed: attribute policy no longer depends on action policy conditions
16
+
17
+ ## [0.2.0]
18
+
19
+ * Changed: resource protection is now done using policy instance method instead of class method
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at povilas@samesystem.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ group :test do
6
+ gem 'codecov', require: false
7
+ gem 'simplecov', require: false
8
+ end
9
+
10
+ # Specify your gem's dependencies in resource_policy.gemspec
11
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,232 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resource_policy (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (7.0.4.2)
10
+ actionpack (= 7.0.4.2)
11
+ activesupport (= 7.0.4.2)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (7.0.4.2)
15
+ actionpack (= 7.0.4.2)
16
+ activejob (= 7.0.4.2)
17
+ activerecord (= 7.0.4.2)
18
+ activestorage (= 7.0.4.2)
19
+ activesupport (= 7.0.4.2)
20
+ mail (>= 2.7.1)
21
+ net-imap
22
+ net-pop
23
+ net-smtp
24
+ actionmailer (7.0.4.2)
25
+ actionpack (= 7.0.4.2)
26
+ actionview (= 7.0.4.2)
27
+ activejob (= 7.0.4.2)
28
+ activesupport (= 7.0.4.2)
29
+ mail (~> 2.5, >= 2.5.4)
30
+ net-imap
31
+ net-pop
32
+ net-smtp
33
+ rails-dom-testing (~> 2.0)
34
+ actionpack (7.0.4.2)
35
+ actionview (= 7.0.4.2)
36
+ activesupport (= 7.0.4.2)
37
+ rack (~> 2.0, >= 2.2.0)
38
+ rack-test (>= 0.6.3)
39
+ rails-dom-testing (~> 2.0)
40
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
41
+ actiontext (7.0.4.2)
42
+ actionpack (= 7.0.4.2)
43
+ activerecord (= 7.0.4.2)
44
+ activestorage (= 7.0.4.2)
45
+ activesupport (= 7.0.4.2)
46
+ globalid (>= 0.6.0)
47
+ nokogiri (>= 1.8.5)
48
+ actionview (7.0.4.2)
49
+ activesupport (= 7.0.4.2)
50
+ builder (~> 3.1)
51
+ erubi (~> 1.4)
52
+ rails-dom-testing (~> 2.0)
53
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
54
+ activejob (7.0.4.2)
55
+ activesupport (= 7.0.4.2)
56
+ globalid (>= 0.3.6)
57
+ activemodel (7.0.4.2)
58
+ activesupport (= 7.0.4.2)
59
+ activerecord (7.0.4.2)
60
+ activemodel (= 7.0.4.2)
61
+ activesupport (= 7.0.4.2)
62
+ activestorage (7.0.4.2)
63
+ actionpack (= 7.0.4.2)
64
+ activejob (= 7.0.4.2)
65
+ activerecord (= 7.0.4.2)
66
+ activesupport (= 7.0.4.2)
67
+ marcel (~> 1.0)
68
+ mini_mime (>= 1.1.0)
69
+ activesupport (7.0.4.2)
70
+ concurrent-ruby (~> 1.0, >= 1.0.2)
71
+ i18n (>= 1.6, < 2)
72
+ minitest (>= 5.1)
73
+ tzinfo (~> 2.0)
74
+ ast (2.4.2)
75
+ builder (3.2.4)
76
+ byebug (11.1.3)
77
+ codecov (0.6.0)
78
+ simplecov (>= 0.15, < 0.22)
79
+ coderay (1.1.3)
80
+ concurrent-ruby (1.2.0)
81
+ crass (1.0.6)
82
+ date (3.3.3)
83
+ diff-lcs (1.5.0)
84
+ docile (1.4.0)
85
+ erubi (1.12.0)
86
+ globalid (1.1.0)
87
+ activesupport (>= 5.0)
88
+ i18n (1.12.0)
89
+ concurrent-ruby (~> 1.0)
90
+ json (2.6.3)
91
+ loofah (2.19.1)
92
+ crass (~> 1.0.2)
93
+ nokogiri (>= 1.5.9)
94
+ mail (2.8.0.1)
95
+ mini_mime (>= 0.1.1)
96
+ net-imap
97
+ net-pop
98
+ net-smtp
99
+ marcel (1.0.2)
100
+ method_source (1.0.0)
101
+ mini_mime (1.1.2)
102
+ mini_portile2 (2.8.1)
103
+ minitest (5.17.0)
104
+ net-imap (0.3.4)
105
+ date
106
+ net-protocol
107
+ net-pop (0.1.2)
108
+ net-protocol
109
+ net-protocol (0.2.1)
110
+ timeout
111
+ net-smtp (0.3.3)
112
+ net-protocol
113
+ nio4r (2.5.8)
114
+ nokogiri (1.14.1)
115
+ mini_portile2 (~> 2.8.0)
116
+ racc (~> 1.4)
117
+ parallel (1.22.1)
118
+ parser (3.2.0.0)
119
+ ast (~> 2.4.1)
120
+ pry (0.14.2)
121
+ coderay (~> 1.1)
122
+ method_source (~> 1.0)
123
+ pry-byebug (3.10.1)
124
+ byebug (~> 11.0)
125
+ pry (>= 0.13, < 0.15)
126
+ racc (1.6.2)
127
+ rack (2.2.6.2)
128
+ rack-test (2.0.2)
129
+ rack (>= 1.3)
130
+ rails (7.0.4.2)
131
+ actioncable (= 7.0.4.2)
132
+ actionmailbox (= 7.0.4.2)
133
+ actionmailer (= 7.0.4.2)
134
+ actionpack (= 7.0.4.2)
135
+ actiontext (= 7.0.4.2)
136
+ actionview (= 7.0.4.2)
137
+ activejob (= 7.0.4.2)
138
+ activemodel (= 7.0.4.2)
139
+ activerecord (= 7.0.4.2)
140
+ activestorage (= 7.0.4.2)
141
+ activesupport (= 7.0.4.2)
142
+ bundler (>= 1.15.0)
143
+ railties (= 7.0.4.2)
144
+ rails-dom-testing (2.0.3)
145
+ activesupport (>= 4.2.0)
146
+ nokogiri (>= 1.6)
147
+ rails-html-sanitizer (1.5.0)
148
+ loofah (~> 2.19, >= 2.19.1)
149
+ railties (7.0.4.2)
150
+ actionpack (= 7.0.4.2)
151
+ activesupport (= 7.0.4.2)
152
+ method_source
153
+ rake (>= 12.2)
154
+ thor (~> 1.0)
155
+ zeitwerk (~> 2.5)
156
+ rainbow (3.1.1)
157
+ rake (13.0.6)
158
+ regexp_parser (2.6.2)
159
+ rexml (3.2.5)
160
+ rspec (3.12.0)
161
+ rspec-core (~> 3.12.0)
162
+ rspec-expectations (~> 3.12.0)
163
+ rspec-mocks (~> 3.12.0)
164
+ rspec-core (3.12.0)
165
+ rspec-support (~> 3.12.0)
166
+ rspec-expectations (3.12.2)
167
+ diff-lcs (>= 1.2.0, < 2.0)
168
+ rspec-support (~> 3.12.0)
169
+ rspec-mocks (3.12.3)
170
+ diff-lcs (>= 1.2.0, < 2.0)
171
+ rspec-support (~> 3.12.0)
172
+ rspec-support (3.12.0)
173
+ rubocop (1.44.1)
174
+ json (~> 2.3)
175
+ parallel (~> 1.10)
176
+ parser (>= 3.2.0.0)
177
+ rainbow (>= 2.2.2, < 4.0)
178
+ regexp_parser (>= 1.8, < 3.0)
179
+ rexml (>= 3.2.5, < 4.0)
180
+ rubocop-ast (>= 1.24.1, < 2.0)
181
+ ruby-progressbar (~> 1.7)
182
+ unicode-display_width (>= 2.4.0, < 3.0)
183
+ rubocop-ast (1.24.1)
184
+ parser (>= 3.1.1.0)
185
+ rubocop-capybara (2.17.0)
186
+ rubocop (~> 1.41)
187
+ rubocop-performance (1.15.2)
188
+ rubocop (>= 1.7.0, < 2.0)
189
+ rubocop-ast (>= 0.4.0)
190
+ rubocop-rails (2.17.4)
191
+ activesupport (>= 4.2.0)
192
+ rack (>= 1.1)
193
+ rubocop (>= 1.33.0, < 2.0)
194
+ rubocop-rspec (2.18.1)
195
+ rubocop (~> 1.33)
196
+ rubocop-capybara (~> 2.17)
197
+ ruby-progressbar (1.11.0)
198
+ simplecov (0.21.2)
199
+ docile (~> 1.1)
200
+ simplecov-html (~> 0.11)
201
+ simplecov_json_formatter (~> 0.1)
202
+ simplecov-html (0.12.3)
203
+ simplecov_json_formatter (0.1.4)
204
+ thor (1.2.1)
205
+ timeout (0.3.1)
206
+ tzinfo (2.0.6)
207
+ concurrent-ruby (~> 1.0)
208
+ unicode-display_width (2.4.2)
209
+ websocket-driver (0.7.5)
210
+ websocket-extensions (>= 0.1.0)
211
+ websocket-extensions (0.1.5)
212
+ zeitwerk (2.6.6)
213
+
214
+ PLATFORMS
215
+ ruby
216
+
217
+ DEPENDENCIES
218
+ bundler (~> 2.0)
219
+ codecov
220
+ pry-byebug
221
+ rails (~> 7.0.0)
222
+ rake (~> 13.0)
223
+ resource_policy!
224
+ rspec (~> 3.0)
225
+ rubocop
226
+ rubocop-performance
227
+ rubocop-rails
228
+ rubocop-rspec
229
+ simplecov
230
+
231
+ BUNDLED WITH
232
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Povilas Jurcys
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "resource_policy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/docs/.nojekyll ADDED
File without changes
data/docs/README.md ADDED
@@ -0,0 +1,163 @@
1
+ # ResourcePolicy
2
+
3
+ [![Build Status](https://travis-ci.org/samesystem/resource_policy.svg?branch=master)](https://travis-ci.org/samesystem/resource_policy)
4
+ [![codecov](https://codecov.io/gh/samesystem/resource_policy/branch/master/graph/badge.svg)](https://codecov.io/gh/samesystem/resource_policy)
5
+ [![Documentation](https://readthedocs.org/projects/ansicolortags/badge/?version=latest)](https://samesystem.github.io/resource_policy)
6
+
7
+ Gem which allows to protect your resources and their methods with policy rules.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your Ruby on Rails application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'resource_policy', require 'resource_policy/rails'
15
+ ```
16
+
17
+ Or add this for any other ruby app:
18
+
19
+ ```ruby
20
+ gem 'resource_policy'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```sh
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+
31
+ ```sh
32
+ $ gem install resource_policy
33
+ ```
34
+
35
+ ## Documentation
36
+
37
+ All details about gem usage can be found here: https://samesystem.github.io/resource_policy
38
+
39
+ ## Usage
40
+
41
+ Policy should be a single point of truth where you can check what kind of actions current user (or anything else) can do to some resource. Later you will see example of `UserPolicy`.
42
+
43
+ ### Actions policy
44
+
45
+ Action policy defines what kind of actions can be done on resource. In the folulowing example `UserPolicy` defines what kind of actions `current_user` can do with other `user`.
46
+
47
+ #### Define action policy
48
+
49
+ ```ruby
50
+ class UserPolicy
51
+ include ResourcePolicy::Policy
52
+
53
+ policy do |c|
54
+ c.action(:read).allowed # current_user can always see user
55
+ c.action(:write).allowed(if: :admin?) # only admin current_user can update user
56
+ end
57
+
58
+ def initialize(user, current_user:)
59
+ @user = user
60
+ @current_user = current_user
61
+ end
62
+
63
+ private
64
+
65
+ def admin?
66
+ @current_user.admin?
67
+ end
68
+ end
69
+ ```
70
+
71
+ #### Using action policy
72
+
73
+ ```ruby
74
+ policy = UserPolicy.new(user, current_user: current_user)
75
+ policy.action(:read).allowed? # => true
76
+ policy.action(:write).allowed? # ... depends on `admin?` result
77
+ ```
78
+
79
+ ### Attributes policy
80
+
81
+ Similar as with actions policy, you can define each field which should be visible or writable by other user
82
+
83
+ #### Define attributes policy
84
+
85
+ ```ruby
86
+ class UserPolicy
87
+ include ResourcePolicy::Policy
88
+
89
+ policy do |c|
90
+ c.attribute(:email)
91
+ .allowed(:read) # current_user can always view user.email
92
+ .allowed(:write, if: :admin?) # only admin current_user can change email
93
+ end
94
+
95
+ def initialize(user, current_user:)
96
+ @user = user
97
+ @current_user = current_user
98
+ end
99
+
100
+ private
101
+
102
+ def admin?
103
+ @current_user.admin?
104
+ end
105
+ end
106
+ ```
107
+
108
+ #### Using attributes policy
109
+
110
+ ```ruby
111
+ policy = UserPolicy.new(user, current_user: current_user)
112
+ policy.attribute(:email).readable? # => true
113
+ policy.action(:email).writable? # ... depends on `admin?` result
114
+ ```
115
+
116
+ #### Using protector
117
+
118
+ You can use `Policy` to hide some fields. Here is how:
119
+
120
+ ```ruby
121
+ class UserPolicy
122
+ include ResourcePolicy::Policy
123
+
124
+ policy do |c|
125
+ c.attribute(:id).allowed(:read)
126
+ c.attribute(:salary).allowed(:read, if: :admin?)
127
+ end
128
+
129
+ ...
130
+ end
131
+ ```
132
+
133
+ Now you can protect `user` like this:
134
+
135
+ ```ruby
136
+ current_user.admin? #=> false
137
+
138
+ user = User.find(1337)
139
+ user.id #=> 1337
140
+ user.email #=> "john.doe@example.com"
141
+
142
+ protected_user = UserPolicy.attributes_policy.protect(user, current_user: current_user)
143
+ protected_user.id #=> 1337
144
+ protected_user.email # nil
145
+ ```
146
+
147
+ ## Development
148
+
149
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
150
+
151
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
152
+
153
+ ## Contributing
154
+
155
+ Bug reports and pull requests are welcome on GitHub at https://github.com/samesystem/resource_policy. 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.
156
+
157
+ ## License
158
+
159
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
160
+
161
+ ## Code of Conduct
162
+
163
+ Everyone interacting in the ResourcePolicy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/samesystem/resource_policy/blob/master/CODE_OF_CONDUCT.md).
data/docs/_sidebar.md ADDED
@@ -0,0 +1,6 @@
1
+ * [Home](README)
2
+ * Components
3
+ * [Policy](components/policy)
4
+ * [ActionsPolicy](components/actions_policy)
5
+ * [ActionsPolicy](components/actions_validator)
6
+ * [AttributesPolicy](components/attributes_policy)