我对 rails (3) 比较陌生,并且正在使用 CanCan 构建一个应用程序,其中有 3 层用户。
- 访客 - 未注册访客用户
- 注册并登录访客
- 管理员 - 使用管理员标志注册并登录的访客
我现在的能力很糟糕,从 cancan 文档中复制而来,基本上定义了访客角色和管理员角色
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # Guest user
if user.is_admin?
can :manage, :all
else
can :read, [Asana,Image,User,Video,Sequence]
end
end
end
我正在寻找添加用户角色。由于我正在创建那个一次性用户模型,我考虑过使用 new_record?确定用户是否登录。就像是:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # Guest user
if !user.new_record? and user.is_admin?
can :manage, :all
elsif !user.new_record? and !user.is_admin?
can {registered user-y permissions}
else
can :read, [Asana,Image,User,Video,Sequence]
end
end
end
但是,就是感觉不对。似乎有点与实际登录无关,并且担心它是否真的安全。
寻找有关更优雅的方法的建议。
谢谢!