【问题标题】:CanCanCan: How to set permissions on each methodCanCanCan:如何设置每种方法的权限
【发布时间】:2018-10-05 15:23:40
【问题描述】:

我有一个模型 Report 和 ReportsController。 有几十个动作。

我需要为每个角色设置每个控制器操作的权限。 如何实现?

【问题讨论】:

  • 查看 can can can 文档中的定义能力部分。挺好的!

标签: ruby-on-rails ruby cancancan


【解决方案1】:

ability.rb你可以拥有

if user.has_role?(:foo)
  can :some_custom_action, Report
end

if user.has_role?(:bar)
  can([:some_other_custom_action, :even_more_action], Report)
end

authorize_resource 将对此进行检查,或者为了获得更多控制权,您可以在 before_action 中调用 authorize!(action_name.to_sym, @report || Report)

还将操作传递给accessible_by(current_ability, action_name.to_sym) 范围

【讨论】:

  • 动作也可以作为数组传递
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多