0

我使用 devise 的 rails 生成器创建了三个不同的角色(用户、管理员、经理),它们存储在不同的表和模型中......

如何禁止某人在同一会话期间登录两个不同的角色?

4

1 回答 1

0

当用户尝试登录时,您可以验证他没有以其他角色登录。为此,您必须重写设计 SessionsController。这里对 RegistrationsController进行了解释,但同样可以使用 SessionsController 完成。接下来在新的 SessionsController 中添加一个前置过滤器:

before_filter :require_not_authenticated_in_other_scopes, :only => [:new, :create]

然后只需在控制器中实现过滤器:

def require_not_authenticated_in_other_scopes
  other_types = [:user, :admin, :manager] - [resource_name]
  other_types.each do |type|
    if self.send("#{type}_signed_in?") 
      resource = warden.user(type)
      redirect_to after_sign_in_path_for(resource)
    end
  end
end

我已经从 Devise 的 SessionsController 本身中获取了部分实现,你可以在他们的GitHub 存储库中找到它。

于 2012-10-31T14:22:06.260 回答