10

根据所有文档,该:read操作被别名为:index:show

alias_action :index, show, :to => :read

但是,请考虑以下具有嵌套资源的场景:

resources :posts
  resources :comments
end

如果我这样定义能力:

# ability.rb
can :read, Post
can :show, Comment

# comments_controller.rb
load_and_authorize_resource :organization, :find_by => :permalink
load_and_authorize_resource :membership, :through => :organization

事情按预期工作。但是,如果我将:read操作更改为 [:index, :show]:

# ability.rb
can [:index, :show], Post
can :show, Comment

# comments_controller.rb
load_and_authorize_resource :organization, :find_by => :permalink
load_and_authorize_resource :membership, :through => :organization

我未经授权访问/posts/:post_id/comments,/posts/:post_id/comments/:id等。但是,我仍然可以同时访问:index:showposts_controller

如果它们的行为不同,这些动作怎么可能被“混淆”?

在我的摆弄中,我还遇到了以下情况。更改load_and_authorize_resource为以下允许的访问权限:

# ability.rb
can [:index, :show], Post
can :show, Comment

# comments_controller.rb
load__resource :organization, :find_by => :permalink
load_and_authorize_resource :membership, :through => :organization

有人可以解释这里发生了什么吗?

4

1 回答 1

16

我将此作为一个问题发布在 GitHub 上。瑞恩回应如下:

:index和动作都:show指向:read动作。但是,当 CanCan 授权父资源时,它:read直接使用该操作,这就是您看到此行为的原因。

我认为这之前引起了混乱,所以我将内部行为更改为从不:read 直接使用该动作。我将更改为使用而不是 :parent资源:showaccessible_by默认情况下我将使用 :index而不是:read. 感谢您引起我的注意。

https://github.com/ryanb/cancan/issues/302#comment_863142

于 2011-03-12T06:59:46.097 回答