ruby-on-rails - 如何编写 CanCanCan 用户只能读取其数据的能力?
问题描述
您如何限制用户访问以使用户只能读取自己的记录?
我试过了:
def initialize(user)
can :read, User, :id => user.id
和这个:
def initialize(user)
can :read, user
但我仍然可以访问索引和显示中的每个用户。我在 UsersController 中有 authorize_resource。
相关文档供参考: https ://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
解决方案
:read != :show
:read == [:show, :index]
不幸的是,我没有设置来测试它,所以它是在黑暗中拍摄的。
can :show, User, :id => user.id
推荐阅读
- c# - 无法连接到 Asp.Net Core 上的 mysql 容器
- android - 使用 Bazel 运行 Android 应用程序时构建失败
- oracle - 根据特定逻辑从表中检索配置文件
- reactjs - 如何使用 React-testing-library 和 Jest 测试文档中是否包含动态文本内容?
- python - pyinstaller制作exe文件后程序不起作用
- excel - 使用 excel 求解器构建 VRP 并列出路径
- javascript - React 中的多选需要使用什么事件
- python - 按钮按下只工作一次
- python - 如何将数据框 value_count 每个值映射到新列
- xml - 如何在 .bat 脚本中编辑 XML 中的值