ruby-on-rails - 限制管理员使用 cancan 销毁自己的帐户

标签 ruby-on-rails cancan restrict

这是我能力等级的代码片段

if user.admin?
      can :manage, :all
      can :destroy, :all if != current_user

我相信您可以弄清楚我在这里要做什么。我意识到 destroy 包含在 manage 中,我在那里重复一遍。有什么建议吗?

编辑 Yjerem 的回答是正确的,我只是更改它以适合我的代码。这是它的样子。

 if user.admin?
      can :manage, :all
      cannot :destroy, User, :id => user.id

正如 Yjerem 还所说,在 cancan 中,能力优先级表明定义较低的能力胜过它们之上的能力,因此管理员可以使用上面的代码管理除在其下定义的能力之外的所有能力。

最佳答案

阅读Ability Precedence ,这里有一个专门为您准备的示例!

基本上你想要的是 cannot 方法:

if user.admin?
      can :manage, :all
      cannot :destroy, User, :id => current_user.id

因为 cannot 规则低于更一般的规则,所以它会覆盖它。

关于ruby-on-rails - 限制管理员使用 cancan 销毁自己的帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5956333/

相关文章:

ruby-on-rails - 如何生成模型之间的关联

ruby-on-rails - 乘客/Nginx错误 "unexpected end of file detected."

ruby-on-rails - Rails rake 测试返回错误消息

ruby-on-rails - 使用 CanCan 授权基于多对多关联的资源

c++ - 为什么 clang 会忽略 __restrict__?

c++ - nvcc、gcc、clang 和 msvc "respect"在结构中使用 __restrict__ 关键字吗?

ruby-on-rails - 联合两个mongo结果

ruby-on-rails - devise/cancan模拟帐户

ruby-on-rails - 非 RESTful Controller 中的 Cancan 授权

MySql 脚本临时将外键约束更改为级联然后返回