我正在使用 Rails 4、设计、角色模型和 CanCanCan。
是否可以在ability.rb 中定义许多角色共有的能力?
例如,每个登录用户都可以 CRUD 自己的个人资料页面吗?然后角色在通用能力之上还有特定的能力?
这是如何运作的?我是否需要在角色模型中为通用能力创建一个角色,然后允许每个用户拥有多个角色,以便他们获得通用能力以及角色特定能力?
例如,在我的能力.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)
所以,我希望学生能够阅读客人可以阅读的相同内容。有没有办法说学生可以做新用户和登录用户可以做的所有事情(以及学生的特定能力)?