Code Monkey home page Code Monkey logo

Comments (4)

bryanrite avatar bryanrite commented on May 8, 2024

These types of inquires are fine for when you are checking abilities.. ie. in the controller or view: can? :manage, @entity will properly check whatever class @entity is, but when you're defining abilities, you have to use the class then any restrictions.

Something like:

can :manage, [TypeOneEntity, TypeTwoEntity], id: @entity.id or
can :manage, ParentEntity, id: @entity.id or
can :manage, Entity, id: @entity.id, type: %w(TypeOneEntity TypeTwoEntity)

I've done this for subclassed User types where @entity in your example is current_user. If @entity isn't your authorizing resource, would you mind explaining what your use case is?

from cancancan.

Crystark avatar Crystark commented on May 8, 2024

In my case @entity isn't the User but an entity linked to the user.
can :manage, ParentEntity, id: @entity.id
Is actually what i did so that accessible_by works.
Thanks for your answer.

from cancancan.

Silex avatar Silex commented on May 8, 2024

This should be better documented.

I have permissions in database, like so:

create_table(:permissions) do |t|
  t.references :user, index: true
  t.references :subject, polymorphic: true, index: true
  t.string     :action
  t.timestamps
end

I had to change code from:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    user.permissions.each do |permission|
      can permission.action.to_sym, permission.subject
    end
  end
end

to:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    user.permissions.each do |permission|
      can permission.action.to_sym, permission.subject_type.constantize, id: permission.subject_id
    end
  end
end

...for it to work. Very unintuitive imho.

from cancancan.

Silex avatar Silex commented on May 8, 2024

I realised it is actually described this way in https://github.com/CanCanCommunity/cancancan/wiki/Abilities-in-Database

I should have read better, my bad.

from cancancan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.