CanCan 嵌套资源控制器授权

发布于 2024-10-30 09:46:10 字数 926 浏览 1 评论 0原文

在我的应用程序中,允许用户指定另一个用户作为他们的“客户经理”,并且客户经理将被允许修改所有用户信息。我定义了以下能力:

can :manage, User do |user|
    user == current_user or user.account_manager == current_user
end

用户还拥有一些嵌套资源(例如:出版物)。我定义了以下功能:

can :manage, Publication do |publication, user|
    publication.user == current_user or user == current_user or user.account_manager == current_user
end

在视图中我使用以下内容进行检查:
可以吗? :更新,@publication,@user_we_are_accessing
可以吗? :创建,Publication.new,@user_we_are_accessing

到目前为止一切都很好。我的问题出在控制器上。在我的 PublicationsController 中我添加了:
load_and_authorize_resource:用户
load_and_authorize_resource :publication, :through =>; :user

但是,这总是抛出 AccessDenied,因为发布检查没有将用户对象传递给能力(尝试检查能力中的用户对象显示 nil)。

我有什么想法可以实施这个吗?

tl;dr:使用 CanCan 授权对资源的访问。用户可以指定其他用户作为客户经理。用户有嵌套资源。问题:客户经理无法访问嵌套资源。

In my application, a user is allowed to assign another user as their "account manager" and the account manager would be allowed to modify all user info. I defined the following ability:

can :manage, User do |user|
    user == current_user or user.account_manager == current_user
end

A user also has some nested resources (e.g: publications). I defined the following ability:

can :manage, Publication do |publication, user|
    publication.user == current_user or user == current_user or user.account_manager == current_user
end

In the views I check using the following:
can? :update, @publication, @user_we_are_accessing
can? :create, Publication.new, @user_we_are_accessing.

Everything works just fine so far. My problem is with the controller. In my PublicationsController I added:
load_and_authorize_resource :user
load_and_authorize_resource :publication, :through => :user

However this always throws AccessDenied, because the check for publication is not passing the user object to the ability (trying to inspect the user object in the ability shows nil).

Any ideas how I can go about implementing this?

tl;dr: Using CanCan to authorize access to resources. User can assign another user as account manager. User has nested resources. Problem: nested resource is not accessible by account manager.

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

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

发布评论

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

评论(1

不如归去 2024-11-06 09:46:10

我通过执行以下操作解决了这个问题:

1) 将 load_and_authorize_resource :publication 替换为仅加载资源 load_resource :publication
2)在 load_resource 调用之后添加了一个 before_filter :authorize ,并使用以下实现:

def authorize
    raise CanCan::AccessDenied unless can? :manage, @publication, @user
end

这可行,但我希望有一种方法可以以设计方式解决这个问题,如果有这样的事情。感谢您的想法和反馈。

I solved this by doing the following:

1) replaced load_and_authorize_resource :publication with just loading the resource load_resource :publication
2) Added a before_filter :authorize after the load_resource call with the following implementaiton:

def authorize
    raise CanCan::AccessDenied unless can? :manage, @publication, @user
end

This works, but I was hoping for a way that would solve this in the devise way, if there is such a thing. Thoughts and feedback are appreciated.

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