Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role in trestle #11

Open
qkhuyit opened this issue Jul 9, 2018 · 11 comments
Open

Role in trestle #11

qkhuyit opened this issue Jul 9, 2018 · 11 comments

Comments

@qkhuyit
Copy link

qkhuyit commented Jul 9, 2018

How to custom trestle-auth by Role, Role Group . ..v..v..
Thks

@spohlenz
Copy link
Member

This isn't currently possible but should hopefully be very soon (I'm planning on working on it within the next couple of weeks).

@IanMitchell
Copy link

Would love to see this too! Been trying to have content editors / 'true' administrators, and some things are a little tricky to do

@wanxsb
Copy link

wanxsb commented Mar 1, 2019

looking forward to this

@McRipper
Copy link
Contributor

Could be nice to have an idea on how to start so we can help

@brunitob
Copy link

This is important!

@brandondrew
Copy link

Any word on this?

@stydav
Copy link

stydav commented Nov 23, 2019

Is this possible yet?

@hdbreaker
Copy link

Hi guys! There is any new about this issue? I will love to add some roles to my panel in order to show or hide menus in relation with user roles.

@McRipper
Copy link
Contributor

I found a way, and it's also pretty straightforward. It's just an idea but I tested it and it works.

Let's use the Pundit gem, remember that roles must be managed separately.

inside an admin resource, take Customer for example:

Trestle.resource(:customers) do

  ...

  controller do
    
    include Pundit

    after_action :verify_authorized

    def index
      authorize Customer
      super
    end

    def show
      authorize instance
      super
    end

  end

end

Remember to create a policy for the customer, an easy one would be:

class CustomerPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.all
    end
  end

  def index?
    true
  end

  def show?
    false
  end

end

Adding some config to the controller could help, instead of opening all the methods of the controller, maybe @spohlenz could give us some ideas when he sees this solution, it seems that everything is inherited from "Trestle::AdminController" and "Trestle::ResourceController"

Hope this help!

@Protoplaste
Copy link

Hi, just wanted to ask if there is any news on this or is the pundit still the best way?

@uaru
Copy link

uaru commented Sep 27, 2022

I needed to have this role based support, and using @McRipper suggestion is easy enough.

The only difficulty I found was how to apply Scope policies when the resolve method is more complicated than

def resolve
    scope.all
end

It can be applied in the index, as usual, however, it is important to know that the @collection is what we need to modify.

Trestle.resource(:customers) do
  ...
  
  controller do
    include Pundit::Authorization

    after_action :verify_authorized, except: :index
    after_action :verify_policy_scoped, only: :index
   
    def index
      @collection = policy_scope @collection
    end
    ...

If we want to totally cut off some classes of users from seeing the list of resources, we could

Trestle.resource(:customers) do
  ...
  
  controller do
    include Pundit::Authorization

    after_action :verify_authorized, except: :index
   
    def index
      authorize Customer
    end

By using both authorize and policy_scope we can prevent any access to some users, and limit what the other can see. Very flexible.

This way of using pundit is the easiest very straightforward, but it has its drawbacks. When the controller's index method is called, the collection is already loaded and prepared. E.g. authorize will prevent from displaying the collection data to the user, but it will not prevent reading it all from the database. So from the purely performance point of view it could be better to call the authorize and policy_scope at the beginning of Trestle::Resource::Collection.prepare method, or even better, search adapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests