pundit 2.4.0 → 2.5.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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +92 -57
  3. data/.rubocop.yml +18 -8
  4. data/.rubocop_ignore_git.yml +7 -0
  5. data/.yardopts +1 -1
  6. data/CHANGELOG.md +61 -42
  7. data/Gemfile +22 -2
  8. data/README.md +30 -0
  9. data/Rakefile +1 -0
  10. data/lib/generators/pundit/install/install_generator.rb +3 -1
  11. data/lib/generators/pundit/policy/policy_generator.rb +3 -1
  12. data/lib/generators/rspec/policy_generator.rb +4 -1
  13. data/lib/generators/test_unit/policy_generator.rb +4 -1
  14. data/lib/pundit/authorization.rb +152 -77
  15. data/lib/pundit/cache_store/legacy_store.rb +7 -0
  16. data/lib/pundit/cache_store/null_store.rb +9 -0
  17. data/lib/pundit/cache_store.rb +22 -0
  18. data/lib/pundit/context.rb +76 -26
  19. data/lib/pundit/policy_finder.rb +22 -1
  20. data/lib/pundit/railtie.rb +19 -0
  21. data/lib/pundit/rspec.rb +67 -6
  22. data/lib/pundit/version.rb +2 -1
  23. data/lib/pundit.rb +39 -14
  24. data/pundit.gemspec +8 -12
  25. data/spec/authorization_spec.rb +60 -3
  26. data/spec/policy_finder_spec.rb +5 -1
  27. data/spec/pundit/helper_spec.rb +18 -0
  28. data/spec/pundit_spec.rb +37 -11
  29. data/spec/rspec_dsl_spec.rb +81 -0
  30. data/spec/simple_cov_check_action_formatter.rb +79 -0
  31. data/spec/spec_helper.rb +22 -339
  32. data/spec/support/lib/controller.rb +38 -0
  33. data/spec/support/lib/custom_cache.rb +19 -0
  34. data/spec/support/lib/instance_tracking.rb +20 -0
  35. data/spec/support/models/article.rb +4 -0
  36. data/spec/support/models/article_tag.rb +7 -0
  37. data/spec/support/models/artificial_blog.rb +7 -0
  38. data/spec/support/models/blog.rb +4 -0
  39. data/spec/support/models/comment.rb +5 -0
  40. data/spec/support/models/comment_four_five_six.rb +5 -0
  41. data/spec/support/models/comment_scope.rb +13 -0
  42. data/spec/support/models/comments_relation.rb +15 -0
  43. data/spec/support/models/customer/post.rb +11 -0
  44. data/spec/support/models/default_scope_contains_error.rb +5 -0
  45. data/spec/support/models/dummy_current_user.rb +7 -0
  46. data/spec/support/models/foo.rb +4 -0
  47. data/spec/support/models/post.rb +25 -0
  48. data/spec/support/models/post_four_five_six.rb +9 -0
  49. data/spec/support/models/project_one_two_three/avatar_four_five_six.rb +7 -0
  50. data/spec/support/models/project_one_two_three/tag_four_five_six.rb +11 -0
  51. data/spec/support/models/wiki.rb +4 -0
  52. data/spec/support/policies/article_tag_other_name_policy.rb +13 -0
  53. data/spec/support/policies/base_policy.rb +23 -0
  54. data/spec/support/policies/blog_policy.rb +5 -0
  55. data/spec/support/policies/comment_policy.rb +11 -0
  56. data/spec/support/policies/criteria_policy.rb +5 -0
  57. data/spec/support/policies/default_scope_contains_error_policy.rb +10 -0
  58. data/spec/support/policies/denier_policy.rb +7 -0
  59. data/spec/support/policies/dummy_current_user_policy.rb +9 -0
  60. data/spec/support/policies/nil_class_policy.rb +17 -0
  61. data/spec/support/policies/post_policy.rb +36 -0
  62. data/spec/support/policies/project/admin/comment_policy.rb +15 -0
  63. data/spec/support/policies/project/comment_policy.rb +17 -0
  64. data/spec/support/policies/project/criteria_policy.rb +7 -0
  65. data/spec/support/policies/project/post_policy.rb +13 -0
  66. data/spec/support/policies/project_one_two_three/avatar_four_five_six_policy.rb +6 -0
  67. data/spec/support/policies/project_one_two_three/comment_four_five_six_policy.rb +6 -0
  68. data/spec/support/policies/project_one_two_three/criteria_four_five_six_policy.rb +6 -0
  69. data/spec/support/policies/project_one_two_three/post_four_five_six_policy.rb +6 -0
  70. data/spec/support/policies/project_one_two_three/tag_four_five_six_policy.rb +6 -0
  71. data/spec/support/policies/publication_policy.rb +13 -0
  72. data/spec/support/policies/wiki_policy.rb +8 -0
  73. metadata +62 -158
  74. data/spec/dsl_spec.rb +0 -30
  75. /data/lib/generators/pundit/install/templates/{application_policy.rb → application_policy.rb.tt} +0 -0
  76. /data/lib/generators/pundit/policy/templates/{policy.rb → policy.rb.tt} +0 -0
  77. /data/lib/generators/rspec/templates/{policy_spec.rb → policy_spec.rb.tt} +0 -0
  78. /data/lib/generators/test_unit/templates/{policy_test.rb → policy_test.rb.tt} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1cc7a931867875af2c1a7cd5c4225da689b33e101f76bb7a471afb967323e615
4
- data.tar.gz: 8ca35ba01f65b52b1b8bbb2061858bdc61cd0034b01818b07dbbba4b7ddd3a69
3
+ metadata.gz: 2e34d4263a4c386c0078ddfed804532e292357926fefb505b62bcea9c6e3d08d
4
+ data.tar.gz: 67c3471d5354cba97b650185770f81bdcc79699f8cbc4d8e60c99b57639a6cee
5
5
  SHA512:
6
- metadata.gz: 0f495747f61c744c04dffa7811d3a86fc818812807a971591d71542d798d5a7aa4438333534082e755bbead592b4b1b5465e23030e535b03420c643e088bcaf1
7
- data.tar.gz: 951ec8a8c02c081bc6b412bb0b5d1d6ffcc33543fa71f66fef9c4f4a6f391ea53a057e20b94bdef5faf4c8f2ef0deffd09357c9580ef6a739575c94a70d9d950
6
+ metadata.gz: e67f07116623c8fd505ed254a165136be512ea36f7635ca2e6062fd59bf73a23eb1a4bf5790a390ff6b4e014e3baf0f7f8e7b649e6e50a8985fcff2e6c27cecd
7
+ data.tar.gz: 65f7d1132b00f9bdcb8b717e08c402a5f6a9a90de5fa07e017b831cbb3ac7b9c11f8869466e3112fea4acddd1699dc495fe30b3234fab06c213ea65dd459c1fd
@@ -2,9 +2,8 @@ name: Main
2
2
 
3
3
  on:
4
4
  push:
5
- branches: [ "main" ]
5
+ branches: ["main"]
6
6
  pull_request:
7
- branches: [ "main" ]
8
7
  workflow_dispatch:
9
8
 
10
9
  permissions:
@@ -28,74 +27,109 @@ jobs:
28
27
  fail-fast: false
29
28
  matrix:
30
29
  ruby-version:
31
- - '3.1'
32
- - '3.2'
33
- - '3.3'
34
- - 'jruby-9.3.10' # oldest supported jruby
35
- - 'jruby'
30
+ - "3.1"
31
+ - "3.2"
32
+ - "3.3"
33
+ - "jruby-9.3.15"
34
+ - "jruby"
36
35
  include: # HEAD-versions
37
- - ruby-version: 'head'
36
+ - ruby-version: "head"
38
37
  allow-failure: true
39
- - ruby-version: 'jruby-head'
38
+ - ruby-version: "jruby-head"
40
39
  allow-failure: true
41
- - ruby-version: 'truffleruby-head'
40
+ - ruby-version: "truffleruby-head"
42
41
  allow-failure: true
43
42
 
44
43
  steps:
45
- - uses: actions/checkout@v4
46
- - name: Set up Ruby
47
- uses: ruby/setup-ruby@v1
48
- with:
49
- rubygems: latest
50
- ruby-version: ${{ matrix.ruby-version }}
51
- bundler-cache: ${{ !startsWith(matrix.ruby-version, 'jruby') }}
52
- - name: Bundler install (JRuby workaround)
53
- if: ${{ startsWith(matrix.ruby-version, 'jruby') }}
54
- run: |
55
- gem install psych
56
- bundle install
57
- - name: Run tests
58
- run: bundle exec rspec
44
+ - uses: actions/checkout@v4
45
+ - name: Set up Ruby
46
+ uses: ruby/setup-ruby@v1
47
+ with:
48
+ rubygems: latest
49
+ ruby-version: ${{ matrix.ruby-version }}
50
+ bundler-cache: true
51
+ - name: Run tests
52
+ run: bundle exec rspec
59
53
 
60
54
  test:
61
55
  runs-on: ubuntu-latest
62
56
  steps:
63
- - uses: actions/checkout@v4
64
- - name: Set up Ruby
65
- uses: ruby/setup-ruby@v1
66
- with:
67
- rubygems: latest
68
- ruby-version: 'ruby'
69
- bundler-cache: true
70
- - name: "Download cc-test-reporter from codeclimate.com"
71
- run: |
72
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
73
- chmod +x ./cc-test-reporter
74
- - name: "Report to Code Climate that we will send a coverage report."
75
- run: ./cc-test-reporter before-build
76
- - name: Run tests
77
- run: bundle exec rspec
78
- env:
79
- COVERAGE: 1
80
- - name: Upload code coverage to Code Climate
81
- run: |
82
- ./cc-test-reporter after-build \
83
- --coverage-input-type simplecov \
84
- ./coverage/.resultset.json
57
+ - uses: actions/checkout@v4
58
+ - name: Set up Ruby
59
+ uses: ruby/setup-ruby@v1
60
+ with:
61
+ rubygems: latest
62
+ ruby-version: "ruby"
63
+ bundler-cache: true
64
+ - name: "Download cc-test-reporter from codeclimate.com"
65
+ run: |
66
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
67
+ chmod +x ./cc-test-reporter
68
+ - name: "Report to Code Climate that we will send a coverage report."
69
+ run: ./cc-test-reporter before-build
70
+ - name: Run tests
71
+ run: bundle exec rspec
72
+ env:
73
+ COVERAGE: 1
74
+ - name: Upload coverage results
75
+ uses: actions/upload-artifact@v4
76
+ with:
77
+ include-hidden-files: true
78
+ name: coverage-results
79
+ path: coverage
80
+ retention-days: 1
81
+ - name: Upload code coverage to Code Climate
82
+ run: |
83
+ ./cc-test-reporter after-build \
84
+ --coverage-input-type simplecov \
85
+ ./coverage/.resultset.json
86
+
87
+ coverage-check:
88
+ permissions:
89
+ contents: read
90
+ checks: write
91
+ needs: test
92
+ runs-on: ubuntu-latest
93
+ steps:
94
+ - uses: actions/checkout@v4
95
+ - name: Download coverage results
96
+ uses: actions/download-artifact@v4
97
+ with:
98
+ name: coverage-results
99
+ path: coverage
100
+ - uses: joshmfrankel/simplecov-check-action@be89e11889202cc59efb14aab2a7091622fa9aad
101
+ with:
102
+ github_token: ${{ secrets.GITHUB_TOKEN }}
103
+ minimum_suite_coverage: 100
104
+ minimum_file_coverage: 100
105
+ coverage_json_path: coverage/simplecov-check-action.json
85
106
 
86
107
  rubocop:
87
108
  runs-on: ubuntu-latest
88
109
  steps:
89
- - uses: actions/checkout@v4
90
- - name: Set up Ruby
91
- uses: ruby/setup-ruby@v1
92
- with:
93
- rubygems: default
94
- ruby-version: 'ruby'
95
- bundler-cache: false
96
- - run: bundle install
97
- - name: Run RuboCop
98
- run: bundle exec rubocop
110
+ - uses: actions/checkout@v4
111
+ - name: Set up Ruby
112
+ uses: ruby/setup-ruby@v1
113
+ with:
114
+ rubygems: default
115
+ ruby-version: "ruby"
116
+ bundler-cache: false
117
+ - run: bundle install
118
+ - name: Run RuboCop
119
+ run: bundle exec rubocop
120
+
121
+ docs:
122
+ runs-on: ubuntu-latest
123
+ steps:
124
+ - uses: actions/checkout@v4
125
+ - name: Set up Ruby
126
+ uses: ruby/setup-ruby@v1
127
+ with:
128
+ rubygems: default
129
+ ruby-version: "ruby"
130
+ bundler-cache: false
131
+ - run: bundle install
132
+ - run: rake yard
99
133
 
100
134
  required-checks:
101
135
  runs-on: ubuntu-latest
@@ -103,10 +137,11 @@ jobs:
103
137
  needs:
104
138
  - test
105
139
  - matrix-test
140
+ - docs
106
141
  - rubocop
107
142
  steps:
108
143
  - name: failure
109
144
  if: ${{ failure() || contains(needs.*.result, 'failure') }}
110
145
  run: exit 1
111
146
  - name: success
112
- run: exit 0
147
+ run: exit 0
data/.rubocop.yml CHANGED
@@ -1,16 +1,20 @@
1
+ inherit_from: .rubocop_ignore_git.yml
2
+
1
3
  AllCops:
2
4
  TargetRubyVersion: 3.1
3
- Exclude:
4
- - "lib/generators/**/templates/**/*"
5
- <% `git status --ignored --porcelain`.lines.grep(/^!! /).each do |path| %>
6
- - <%= path.sub(/^!! /, '').sub(/\/$/, '/**/*') %>
7
- <% end %>
8
5
  SuggestExtensions: false
9
6
  NewCops: disable
10
7
 
8
+ Gemspec/DeprecatedAttributeAssignment:
9
+ Enabled: true
10
+
11
+ Gemspec/DevelopmentDependencies:
12
+ Enabled: true
13
+
11
14
  Metrics/BlockLength:
12
15
  Exclude:
13
16
  - "**/*_spec.rb"
17
+ - pundit.gemspec
14
18
 
15
19
  Metrics/MethodLength:
16
20
  Max: 40
@@ -24,7 +28,7 @@ Layout/LineLength:
24
28
  Max: 120
25
29
 
26
30
  Gemspec/RequiredRubyVersion:
27
- Enabled: false
31
+ Enabled: false
28
32
 
29
33
  Layout/ParameterAlignment:
30
34
  EnforcedStyle: with_fixed_indentation
@@ -36,13 +40,19 @@ Layout/CaseIndentation:
36
40
  - end
37
41
  IndentOneStep: true
38
42
 
43
+ Layout/FirstArrayElementIndentation:
44
+ EnforcedStyle: consistent
45
+
46
+ Layout/FirstHashElementIndentation:
47
+ EnforcedStyle: consistent
48
+
39
49
  Layout/EndAlignment:
40
50
  EnforcedStyleAlignWith: variable
41
51
 
42
52
  Style/PercentLiteralDelimiters:
43
53
  PreferredDelimiters:
44
- '%w': "[]"
45
- '%W': "[]"
54
+ "%w": "[]"
55
+ "%W": "[]"
46
56
 
47
57
  Style/StringLiterals:
48
58
  EnforcedStyle: double_quotes
@@ -0,0 +1,7 @@
1
+ # This is here so we can keep YAML syntax highlight in the main file.
2
+ AllCops:
3
+ Exclude:
4
+ - "lib/generators/**/templates/**/*"
5
+ <% `git status --ignored --porcelain`.lines.grep(/^!! /).each do |path| %>
6
+ - <%= path.sub(/^!! /, '').sub(/\/$/, '/**/*') %>
7
+ <% end %>
data/.yardopts CHANGED
@@ -1 +1 @@
1
- --api public --hide-void-return --markup markdown
1
+ --no-private --private --protected --hide-void-return --markup markdown --fail-on-warning
data/CHANGELOG.md CHANGED
@@ -2,43 +2,62 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 2.5.0 (2025-03-03)
6
+
7
+ ### Added
8
+
9
+ - Add `Pundit::Authorization#pundit_reset!` hook to reset the policy and policy scope cache. [#830](https://github.com/varvet/pundit/issues/830)
10
+ - Add links to gemspec. [#845](https://github.com/varvet/pundit/issues/845)
11
+ - Register policies directories for Rails 8 code statistics [#833](https://github.com/varvet/pundit/issues/833)
12
+ - Added an example for how to use pundit with Rails 8 authentication generator [#850](https://github.com/varvet/pundit/issues/850)
13
+
14
+ ### Changed
15
+
16
+ - Deprecated `Pundit::SUFFIX`, moved it to `Pundit::PolicyFinder::SUFFIX` [#835](https://github.com/varvet/pundit/issues/835)
17
+ - Explicitly require less of `active_support` [#837](https://github.com/varvet/pundit/issues/837)
18
+ - Using `permit` matcher without a surrouding `permissions` block now raises a useful error. [#836](https://github.com/varvet/pundit/issues/836)
19
+
20
+ ### Fixed
21
+
22
+ - Using a hash as custom cache in `Pundit.authorize` now works as documented. [#838](https://github.com/varvet/pundit/issues/838)
23
+
5
24
  ## 2.4.0 (2024-08-26)
6
25
 
7
- ## Changed
26
+ ### Changed
8
27
 
9
28
  - Improve the `NotAuthorizedError` message to include the policy class.
10
- Furthermore, in the case where the record passed is a class instead of an instance, the class name is given. (#812)
29
+ Furthermore, in the case where the record passed is a class instead of an instance, the class name is given. [#812](https://github.com/varvet/pundit/issues/812)
11
30
 
12
- ## Added
31
+ ### Added
13
32
 
14
- - Add customizable permit matcher description (#806)
15
- - Add support for filter_run_when_matching :focus with permissions helper. (#820)
33
+ - Add customizable permit matcher description [#806](https://github.com/varvet/pundit/issues/806)
34
+ - Add support for filter_run_when_matching :focus with permissions helper. [#820](https://github.com/varvet/pundit/issues/820)
16
35
 
17
36
  ## 2.3.2 (2024-05-08)
18
37
 
19
- - Refactor: First pass of Pundit::Context (#797)
38
+ - Refactor: First pass of Pundit::Context [#797](https://github.com/varvet/pundit/issues/797)
20
39
 
21
- ## Changed
40
+ ### Changed
22
41
 
23
- - Update `ApplicationPolicy` generator to qualify the `Scope` class name (#792)
24
- - Policy generator uses `NoMethodError` to indicate `#resolve` is not implemented (#776)
42
+ - Update `ApplicationPolicy` generator to qualify the `Scope` class name [#792](https://github.com/varvet/pundit/issues/792)
43
+ - Policy generator uses `NoMethodError` to indicate `#resolve` is not implemented [#776](https://github.com/varvet/pundit/issues/776)
25
44
 
26
45
  ## Deprecated
27
46
 
28
- - Dropped support for Ruby 3.0 (#796)
47
+ - Dropped support for Ruby 3.0 [#796](https://github.com/varvet/pundit/issues/796)
29
48
 
30
49
  ## 2.3.1 (2023-07-17)
31
50
 
32
51
  ### Fixed
33
52
 
34
- - Use `Kernel.warn` instead of `ActiveSupport::Deprecation.warn` for deprecations (#764)
35
- - Policy generator now works on Ruby 3.2 (#754)
53
+ - Use `Kernel.warn` instead of `ActiveSupport::Deprecation.warn` for deprecations [#764](https://github.com/varvet/pundit/issues/764)
54
+ - Policy generator now works on Ruby 3.2 [#754](https://github.com/varvet/pundit/issues/754)
36
55
 
37
56
  ## 2.3.0 (2022-12-19)
38
57
 
39
58
  ### Added
40
59
 
41
- - add support for rubocop-rspec syntax extensions (#745)
60
+ - add support for rubocop-rspec syntax extensions [#745](https://github.com/varvet/pundit/issues/745)
42
61
 
43
62
  ## 2.2.0 (2022-02-11)
44
63
 
@@ -52,41 +71,41 @@
52
71
 
53
72
  ### Deprecated
54
73
 
55
- - Deprecate `include Pundit` in favor of `include Pundit::Authorization` (#621)
74
+ - Deprecate `include Pundit` in favor of `include Pundit::Authorization` [#621](https://github.com/varvet/pundit/issues/621)
56
75
 
57
76
  ## 2.1.1 (2021-08-13)
58
77
 
59
78
  Friday 13th-release!
60
79
 
61
- Careful! The bugfix below (#626) could break existing code. If you rely on the
80
+ Careful! The bugfix below [#626](https://github.com/varvet/pundit/issues/626) could break existing code. If you rely on the
62
81
  return value for `authorize` and namespaced policies you might need to do some
63
82
  changes.
64
83
 
65
84
  ### Fixed
66
85
 
67
86
  - `.authorize` and `#authorize` return the instance, even for namespaced
68
- policies (#626)
87
+ policies [#626](https://github.com/varvet/pundit/issues/626)
69
88
 
70
89
  ### Changed
71
90
 
72
- - Generate application scope with `protected` attr_readers. (#616)
91
+ - Generate application scope with `protected` attr_readers. [#616](https://github.com/varvet/pundit/issues/616)
73
92
 
74
93
  ### Removed
75
94
 
76
- - Dropped support for Ruby end-of-life versions: 2.1 and 2.2. (#604)
77
- - Dropped support for Ruby end-of-life versions: 2.3 (#633)
78
- - Dropped support for Ruby end-of-life versions: 2.4, 2.5 and JRuby 9.1 (#676)
79
- - Dropped support for RSpec 2 (#615)
95
+ - Dropped support for Ruby end-of-life versions: 2.1 and 2.2. [#604](https://github.com/varvet/pundit/issues/604)
96
+ - Dropped support for Ruby end-of-life versions: 2.3 [#633](https://github.com/varvet/pundit/issues/633)
97
+ - Dropped support for Ruby end-of-life versions: 2.4, 2.5 and JRuby 9.1 [#676](https://github.com/varvet/pundit/issues/676)
98
+ - Dropped support for RSpec 2 [#615](https://github.com/varvet/pundit/issues/615)
80
99
 
81
100
  ## 2.1.0 (2019-08-14)
82
101
 
83
102
  ### Fixed
84
103
 
85
- - Avoid name clashes with the Error class. (#590)
104
+ - Avoid name clashes with the Error class. [#590](https://github.com/varvet/pundit/issues/590)
86
105
 
87
106
  ### Changed
88
107
 
89
- - Return a safer default NotAuthorizedError message. (#583)
108
+ - Return a safer default NotAuthorizedError message. [#583](https://github.com/varvet/pundit/issues/583)
90
109
 
91
110
  ## 2.0.1 (2019-01-18)
92
111
 
@@ -96,8 +115,8 @@ None
96
115
 
97
116
  ### Other changes
98
117
 
99
- - Improve exception handling for `#policy_scope` and `#policy_scope!`. (#550)
100
- - Add `:policy` metadata to RSpec template. (#566)
118
+ - Improve exception handling for `#policy_scope` and `#policy_scope!`. [#550](https://github.com/varvet/pundit/issues/550)
119
+ - Add `:policy` metadata to RSpec template. [#566](https://github.com/varvet/pundit/issues/566)
101
120
 
102
121
  ## 2.0.0 (2018-07-21)
103
122
 
@@ -107,20 +126,20 @@ No changes since beta1
107
126
 
108
127
  ### Breaking changes
109
128
 
110
- - Only pass last element of "namespace array" to policy and scope. (#529)
111
- - Raise `InvalidConstructorError` if a policy or policy scope with an invalid constructor is called. (#462)
112
- - Return passed object from `#authorize` method to make chaining possible. (#385)
129
+ - Only pass last element of "namespace array" to policy and scope. [#529](https://github.com/varvet/pundit/issues/529)
130
+ - Raise `InvalidConstructorError` if a policy or policy scope with an invalid constructor is called. [#462](https://github.com/varvet/pundit/issues/462)
131
+ - Return passed object from `#authorize` method to make chaining possible. [#385](https://github.com/varvet/pundit/issues/385)
113
132
 
114
133
  ### Other changes
115
134
 
116
- - Add `policy_class` option to `authorize` to be able to override the policy. (#441)
117
- - Add `policy_scope_class` option to `authorize` to be able to override the policy scope. (#441)
118
- - Fix `param_key` issue when passed an array. (#529)
119
- - Allow specification of a `NilClassPolicy`. (#525)
120
- - Make sure `policy_class` override is called when passed an array. (#475)
135
+ - Add `policy_class` option to `authorize` to be able to override the policy. [#441](https://github.com/varvet/pundit/issues/441)
136
+ - Add `policy_scope_class` option to `authorize` to be able to override the policy scope. [#441](https://github.com/varvet/pundit/issues/441)
137
+ - Fix `param_key` issue when passed an array. [#529](https://github.com/varvet/pundit/issues/529)
138
+ - Allow specification of a `NilClassPolicy`. [#525](https://github.com/varvet/pundit/issues/525)
139
+ - Make sure `policy_class` override is called when passed an array. [#475](https://github.com/varvet/pundit/issues/475)
121
140
 
122
- - Use `action_name` instead of `params[:action]`. (#419)
123
- - Add `pundit_params_for` method to make it easy to customize params fetching. (#502)
141
+ - Use `action_name` instead of `params[:action]`. [#419](https://github.com/varvet/pundit/issues/419)
142
+ - Add `pundit_params_for` method to make it easy to customize params fetching. [#502](https://github.com/varvet/pundit/issues/502)
124
143
 
125
144
  ## 1.1.0 (2016-01-14)
126
145
 
@@ -152,16 +171,16 @@ No changes since beta1
152
171
 
153
172
  ## 0.3.0 (2014-08-22)
154
173
 
155
- - Extend the default `ApplicationPolicy` with an `ApplicationPolicy::Scope` (#120)
156
- - Fix RSpec 3 deprecation warnings for built-in matchers (#162)
157
- - Generate blank policy spec/test files for Rspec/MiniTest/Test::Unit in Rails (#138)
174
+ - Extend the default `ApplicationPolicy` with an `ApplicationPolicy::Scope` [#120](https://github.com/varvet/pundit/issues/120)
175
+ - Fix RSpec 3 deprecation warnings for built-in matchers [#162](https://github.com/varvet/pundit/issues/162)
176
+ - Generate blank policy spec/test files for Rspec/MiniTest/Test::Unit in Rails [#138](https://github.com/varvet/pundit/issues/138)
158
177
 
159
178
  ## 0.2.3 (2014-04-06)
160
179
 
161
- - Customizable error messages: `#query`, `#record` and `#policy` methods on `Pundit::NotAuthorizedError` (#114)
162
- - Raise a different `Pundit::AuthorizationNotPerformedError` when `authorize` call is expected in controller action but missing (#109)
163
- - Update Rspec matchers for Rspec 3 (#124)
180
+ - Customizable error messages: `#query`, `#record` and `#policy` methods on `Pundit::NotAuthorizedError` [#114](https://github.com/varvet/pundit/issues/114)
181
+ - Raise a different `Pundit::AuthorizationNotPerformedError` when `authorize` call is expected in controller action but missing [#109](https://github.com/varvet/pundit/issues/109)
182
+ - Update Rspec matchers for Rspec 3 [#124](https://github.com/varvet/pundit/issues/124)
164
183
 
165
184
  ## 0.2.2 (2014-02-07)
166
185
 
167
- - Customize the user to be passed into policies: `pundit_user` (#42)
186
+ - Customize the user to be passed into policies: `pundit_user` [#42](https://github.com/varvet/pundit/issues/42)
data/Gemfile CHANGED
@@ -4,5 +4,25 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
- # https://github.com/ruby/psych/issues/655
8
- gem "psych", "!= 5.1.1", platforms: %i[jruby]
7
+ # Rails-related - for testing purposes
8
+ gem "actionpack", ">= 3.0.0" # Used to test strong parameters
9
+ gem "activemodel", ">= 3.0.0" # Used to test ActiveModel::Naming
10
+ gem "railties", ">= 3.0.0" # Used to test generators
11
+
12
+ # Testing
13
+ gem "rspec", ">= 3.0.0"
14
+ gem "simplecov", ">= 0.17.0"
15
+
16
+ # Development tools
17
+ gem "bundler"
18
+ gem "rake"
19
+ gem "rubocop"
20
+ gem "rubocop-performance"
21
+ gem "rubocop-rspec"
22
+ gem "yard"
23
+ gem "zeitwerk"
24
+
25
+ # Affects us on JRuby 9.3.15.
26
+ #
27
+ # @see https://github.com/rails/rails/issues/54260
28
+ gem "logger"
data/README.md CHANGED
@@ -583,6 +583,36 @@ def pundit_user
583
583
  end
584
584
  ```
585
585
 
586
+ For instance, Rails 8 includes a built-in [authentication generator](https://github.com/rails/rails/tree/8-0-stable/railties/lib/rails/generators/rails/authentication). If you choose to use it, the currently logged-in user is accessed via `Current.user` instead of `current_user`.
587
+
588
+ To ensure compatibility with Pundit, define a `pundit_user` method in `application_controller.rb` (or another suitable location) as follows:
589
+
590
+ ```ruby
591
+ def pundit_user
592
+ Current.user
593
+ end
594
+ ```
595
+
596
+ ### Handling User Switching in Pundit
597
+
598
+ When switching users in your application, it's important to reset the Pundit user context to ensure that authorization policies are applied correctly for the new user. Pundit caches the user context, so failing to reset it could result in incorrect permissions being applied.
599
+
600
+ To handle user switching, you can use the following pattern in your controller:
601
+
602
+ ```ruby
603
+ class ApplicationController
604
+ include Pundit::Authorization
605
+
606
+ def switch_user_to(user)
607
+ terminate_session if authenticated?
608
+ start_new_session_for user
609
+ pundit_reset!
610
+ end
611
+ end
612
+ ```
613
+
614
+ Make sure to invoke `pundit_reset!` whenever changing the user. This ensures the cached authorization context is reset, preventing any incorrect permissions from being applied.
615
+
586
616
  ## Policy Namespacing
587
617
  In some cases it might be helpful to have multiple policies that serve different contexts for a
588
618
  resource. A prime example of this is the case where User policies differ from Admin policies. To
data/Rakefile CHANGED
@@ -15,6 +15,7 @@ end
15
15
 
16
16
  YARD::Rake::YardocTask.new do |t|
17
17
  t.files = ["lib/**/*.rb"]
18
+ t.stats_options = ["--list-undoc"]
18
19
  end
19
20
 
20
21
  task default: :spec
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pundit
4
+ # @private
4
5
  module Generators
6
+ # @private
5
7
  class InstallGenerator < ::Rails::Generators::Base
6
8
  source_root File.expand_path("templates", __dir__)
7
9
 
8
10
  def copy_application_policy
9
- template "application_policy.rb", "app/policies/application_policy.rb"
11
+ template "application_policy.rb.tt", "app/policies/application_policy.rb"
10
12
  end
11
13
  end
12
14
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pundit
4
+ # @private
4
5
  module Generators
6
+ # @private
5
7
  class PolicyGenerator < ::Rails::Generators::NamedBase
6
8
  source_root File.expand_path("templates", __dir__)
7
9
 
8
10
  def create_policy
9
- template "policy.rb", File.join("app/policies", class_path, "#{file_name}_policy.rb")
11
+ template "policy.rb.tt", File.join("app/policies", class_path, "#{file_name}_policy.rb")
10
12
  end
11
13
 
12
14
  hook_for :test_framework
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # @private
3
4
  module Rspec
5
+ # @private
4
6
  module Generators
7
+ # @private
5
8
  class PolicyGenerator < ::Rails::Generators::NamedBase
6
9
  source_root File.expand_path("templates", __dir__)
7
10
 
8
11
  def create_policy_spec
9
- template "policy_spec.rb", File.join("spec/policies", class_path, "#{file_name}_policy_spec.rb")
12
+ template "policy_spec.rb.tt", File.join("spec/policies", class_path, "#{file_name}_policy_spec.rb")
10
13
  end
11
14
  end
12
15
  end
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # @private
3
4
  module TestUnit
5
+ # @private
4
6
  module Generators
7
+ # @private
5
8
  class PolicyGenerator < ::Rails::Generators::NamedBase
6
9
  source_root File.expand_path("templates", __dir__)
7
10
 
8
11
  def create_policy_test
9
- template "policy_test.rb", File.join("test/policies", class_path, "#{file_name}_policy_test.rb")
12
+ template "policy_test.rb.tt", File.join("test/policies", class_path, "#{file_name}_policy_test.rb")
10
13
  end
11
14
  end
12
15
  end