mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
Merge branch 'skip-authorization' of https://github.com/nickrivadeneira/pundit into nickrivadeneira-skip-authorization
This commit is contained in:
commit
1aa2837c67
3 changed files with 27 additions and 0 deletions
16
README.md
16
README.md
|
@ -177,6 +177,22 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
```
|
||||
|
||||
If you're using `verify_authorized` in your controllers but need to
|
||||
conditionally bypass verification, you can use `skip_authorization`. This is
|
||||
useful in circumstances where you don't want to disable verification for the
|
||||
entire action, but have some cases where you intend to not authorize.
|
||||
|
||||
```ruby
|
||||
def show
|
||||
record = Record.find_by(attribute: "value")
|
||||
if record.present?
|
||||
authorize record
|
||||
else
|
||||
skip_authorization
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Scopes
|
||||
|
||||
Often, you will want to have some kind of view listing records which a
|
||||
|
|
|
@ -92,6 +92,10 @@ module Pundit
|
|||
true
|
||||
end
|
||||
|
||||
def skip_authorization
|
||||
@_pundit_policy_authorized = true
|
||||
end
|
||||
|
||||
def policy_scope(scope)
|
||||
@_pundit_policy_scoped = true
|
||||
policy_scopes[scope] ||= Pundit.policy_scope!(pundit_user, scope)
|
||||
|
|
|
@ -205,6 +205,13 @@ describe Pundit do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#skip_authorization" do
|
||||
it "disables authorization verification" do
|
||||
controller.skip_authorization
|
||||
expect { controller.verify_authorized }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "#pundit_user" do
|
||||
it 'returns the same thing as current_user' do
|
||||
expect(controller.pundit_user).to eq controller.current_user
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue