Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pundit authorisation using service objects

The Tools

  1. Pundit Authorisation; experimenting with this pull request linked to from the official Pundit README;
  2. ActiveInteraction domain service objects ("DSOs");
  3. RSpec 2.99.1

** 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

  • the PostsController#new action and its spec;
  • the Pundit ApplicationPolicy class; and
  • the PostDataPolicy 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.

like image 340
Jeff Dickey Avatar asked Jul 22 '14 07:07

Jeff Dickey


People also ask

How does pundit gem work?

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.

What does scope refer to within a pundit policy?

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 .


1 Answers

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 :\

like image 164
Nondv Avatar answered Oct 19 '22 09:10

Nondv