简体   繁体   English

Cancancan嵌套资源授权

[英]Cancancan nested resource authorization

I have a working ability defined as below: 我的工作能力定义如下:

routes.rb 的routes.rb

resources :projects do
  resources :tasks
end

ability.rb ability.rb

can [:manage], Project, invites: {supplier: {:user_id => user.id}}
can [:new, :create], Task
can [:update, :show, :destroy, :edit], Task, user_id: user.id

Tasks Controller: 任务控制器:

load_and_authorize_resource :project
load_and_authorize_resource :task, :through => :project

This properly lets the user create a task, if they are invited to a project. 如果邀请他们加入项目,这可以让用户正确创建任务。

However, I don't want the user to be able to :manage the project. 但是,我不希望用户能够:管理项目。 I just need the user to be able to index the project as below. 我只需要用户能够为项目编制索引,如下所示。

ability.rb ability.rb

can [:index], Project, invites: {supplier: {:user_id => user.id}} ## breaks when changing :manage to :index here
can [:new, :create], Task
can [:update, :show, :destroy, :edit, :index], Task, user_id: user.id

When I put in the the abilities above, the user can no longer access or perform any actions on the task. 当我输入上述功能时,用户将无法再访问或执行任务上的任何操作。 How do I create a task ability through the project, with only giving an :index ability to the project? 如何通过项目创建任务能力,只为项目提供:index能力?

You should probably define that a user can read a Project when invited. 您可能应该定义用户在被邀请时可以read项目。 Therefore: 因此:

a user can read a project if is invited ( I guess the condition invites: { supplier: { user: user} } defines an invited user ) 如果被邀请,用户可以阅读项目(我猜条件invites: { supplier: { user: user} }定义受邀用户)

can :read, Project, invites: { supplier: { user: user} }

then you have to say that a user can create Tasks for projects to which he got invited, then: 那么你必须说用户可以为他被邀请的项目创建任务,然后:

can [:new, :create], Task, project: { invites: { supplier: { user: user} } }

in the end you define that the user can manage his own tasks on projects he his invited to: 最后,您定义用户可以在他受邀的项目上管理自己的任务:

can :manage, Task, user: user, project: { invites: { supplier: { user: user} } }

You can also simplify the conditions by extracting 您还可以通过提取来简化条件

invited_to = { invites: { supplier: { user: user} } }
can :read, Project, invited_to
can [:new, :create], Task, project: invited_to
can :manage, Task, user: user, project: invited_to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM