【发布时间】:2015-11-25 03:26:13
【问题描述】:
我正在使用 Rails 4、设计、角色模型和 CanCanCan。
是否可以在ability.rb 中定义多个角色共有的能力?
例如,每个登录用户都可以 CRUD 自己的个人资料页面吗?然后角色除了通用能力之外还有特定的能力?
它是如何工作的?我是否需要在角色模型中为通用能力创建一个角色,然后允许每个用户拥有多个角色,以便他们获得通用能力以及特定于角色的能力?
例如,在我的ability.rb中,我有:
class Ability
include CanCan::Ability
def initialize(user)
alias_action :create, :read, :update, :destroy, :to => :crud
# Define abilities for the passed in user here. For example:
#
user ||= User.new # guest user (not logged in)
#users who are not signed in can create registration or login
# can read publicly available projects, programs and proposals
can :read, Project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }
# {:active => true, :closed => false && :Project.sweep.disclosure.allusers => true}
# if user role is student
if user_signed_in?
can :crud, Profile, :user_id => user.id #[for themselves]
elsif user.try(:profile).present? && user.profile.has_role?(:student)
所以,我希望学生能够阅读客人可以阅读的相同内容。有没有办法说学生可以做新用户和登录用户可以做的所有事情(以及学生的特定能力)?
【问题讨论】:
标签: ruby-on-rails cancan user-roles cancancan