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

Is it possible to connect CanCanCan with Trestle? #266

Open
rcholody opened this issue Dec 6, 2019 · 2 comments
Open

Is it possible to connect CanCanCan with Trestle? #266

rcholody opened this issue Dec 6, 2019 · 2 comments

Comments

@rcholody
Copy link

rcholody commented Dec 6, 2019

I have used CanCanCan to limit specific action on admin dashboard based on role. I would like to limit some specific CRUD operations on specific model. Is it possible to also render different content on specific role? I have specified ability.rb with for example:

user can :read, Company, id: user.companies.pluck(:id)

and connect load_and_authorized_resource in controller.rb.
When I have used command
Company.accessible_by(ability) in IRB it give me correct feedback. But how can I connect this functionality to Trestle?

@dmitry
Copy link

dmitry commented Feb 3, 2020

Might help you:

TrestleAdmin/trestle-auth#21
#45

No real support at the moment, but it's planned.

@RuanAyram
Copy link

Is not the correct way, but in development works.

# app/admin/users_admin.rb

Trestle.resource(:users) do
  menu do
    item 'Users', icon: 'fa fa-user', group: :agenda
  end

  scope :all, default: true
  
  controller do
    def show
      @user = User.find(params[:id])
      authorize! :read, @user
    end
  end
# app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)

    can :create, User
    can %i[read update destroy], User, id: user.id
    can :create, Annotation
    can %i[read update show destroy], Annotation, user_id: user.id
  end
end

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

3 participants