CanCan :read 和 [:index, :show] 之间的区别吗?

发布于 2024-10-21 17:38:47 字数 1387 浏览 2 评论 0原文

根据所有文档, :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 等。但是,我仍然可以访问 posts_controller:index:show代码>.

如果这些行为的行为不同,它们怎么可能是“别名”呢?

在我的摆弄中,我还遇到了以下情况。将 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

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

According to all documentation, the :read action is aliased to both :index and :show:

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

However, consider the following scenario with nested resources:

resources :posts
  resources :comments
end

If I define abilities like this:

# 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

things work as expected. However, if I change the :read action to [: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

I am unauthorized to access /posts/:post_id/comments, /posts/:post_id/comments/:id, etc. I still, however, can access both :index and :show for the posts_controller.

How is possible that these actions are "aliased", if they behave differently?

In my fiddling, I also came across the following. Changing load_and_authorize_resource to the following allowed access:

# 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

Can someone explain what's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-10-28 17:38:47

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

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

我认为这引起了混乱
之前,所以我会改变内部
永远不要使用 :read 的行为
直接行动。而不是一个
:parent 资源我将其更改为
使用 :show
我将使用默认的 accessible_by
:index 而不是 :read。谢谢
让我注意到这一点。

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

I posted this as an issue on GitHub. Ryan responded with the following:

Both the :index and :show actions
point to the :read action. But when
CanCan authorizes a parent resource it
uses the :read action directly which
is why you're seeing this behavior.

I think this has caused confusion
before, so I will change the internal
behavior to never use the :read
action directly. Instead of a
:parent resource I'll change it to
use :show and for the
accessible_by default I will use
:index instead of :read. Thanks
for bringing this to my attention.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文