Cancan 可用于限制对单个模型的特定值的访问吗?

发布于 2024-09-26 12:39:16 字数 112 浏览 3 评论 0原文

我有一个包含类别的 Rails 3 应用程序。类别可以由具有类别所有者角色的人员管理。但类别所有者应该只能访问他拥有的类别,而不能访问其他类别。我可以使用 CanCan 锁定管理功能,但我需要限制特定类别本身。

I have a Rails 3 application that has Categories. A category can be administered by somebody with the Category Owner role. But the Category Owner should only be able to access Categories that he owns, not others. I can lock down the admin functions using CanCan, but I need to restrict the specific categories themselves.

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

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

发布评论

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

评论(1

东走西顾 2024-10-03 12:39:16

您可以通过以下两种方式之一来完成此操作。

您可以指定属性哈希来限制 Ability 类中的访问。

can :manage, Category, :user_id => user.id

或者您可以使用块:

can :manage, Category do |c|
  c && c.user_id == user.id
end

它们都会检查您正在检查的类别的 user_id 属性是否与您正在检查的用户匹配。

这些内容在使用哈希定义能力用块定义能力

You can do it in one of two ways.

You can either specify a hash of attributes to restrict access in your Ability class.

can :manage, Category, :user_id => user.id

Or you can use a block:

can :manage, Category do |c|
  c && c.user_id == user.id
end

These both check whether the user_id attribute on the category you are checking against matches the user you are checking for.

These are described under Defining Abilities with Hashes and Defining Abilities with Blocks respectively in the CanCan documentation.

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