The Tools
** The Project**
The project repo is on Github; what's under scrutiny here is on the pundit-1
branch.
I've followed the Pundit tutorial and gotten authorisation working using "traditional" chubby controllers; see
PostsController#new
action and its spec;ApplicationPolicy
class; andPostDataPolicy
governing authorisation for the Rails model instances of Posts.All well and good so far. And then we come to the SessionController
, whose #new
and #destroy
actions respectively govern logging in and out..
The current code and spec Work Just Fine using traditional-ish controller logic (see how #new
calls private methods that use Pundit to authorise the active (current) user using a SessionDataPolicy
.
Then I try to encapsulate that logic within an ActiveInteraction DSO (see the commented-out code in SessionsController#new
), and all hell breaks loose.
More specifically, the version of the SessionsController
spec in this Gist raises a Pundit::AuthorizationNotPerformedError
at the point where the spec calls the #new
method.
Whiskey. Tango. FOX?!?!?
Has anybody been able to get similar code working without Pundit living directly inside the controller code, ideally using @billychan's pull request (a better alternate will be gratefully entertained)?
This has me completely flummoxed.
Pundit is a Ruby gem that handles authorization via a very simple API. Remember that authorization is different from authentication — authentication is verifying that you are who you say you are, and authorization is verifying that you have permission to perform an action.
Policy Scope For these cases, Pundit uses the notion of authorized scopes to provide the granularity you need. An authorized scope is just an authorization-based limitation to the default scope of a particular model. Authorized scopes are defined as a class with a single instance method: #resolve .
Pundit::AuthorizationNotPerformedError
is raised because of #verify_authorized
method that is basically checking for @_pundit_policy_authorized
variable.
You can just turn off verification for controllers that don't need it.
UPD. and again necroposting. I need to pay more attention to dates :\
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With