【发布时间】:2018-10-05 15:23:40
【问题描述】:
我有一个模型 Report 和 ReportsController。 有几十个动作。
我需要为每个角色设置每个控制器操作的权限。 如何实现?
【问题讨论】:
-
查看 can can can 文档中的定义能力部分。挺好的!
标签: ruby-on-rails ruby cancancan
我有一个模型 Report 和 ReportsController。 有几十个动作。
我需要为每个角色设置每个控制器操作的权限。 如何实现?
【问题讨论】:
标签: ruby-on-rails ruby cancancan
在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) 范围
【讨论】: