Cancan 可用于限制对单个模型的特定值的访问吗?
我有一个包含类别的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下两种方式之一来完成此操作。
您可以指定属性哈希来限制
Ability
类中的访问。或者您可以使用块:
它们都会检查您正在检查的类别的
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.Or you can use a block:
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.