这是我能力等级的代码片段
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/