设计:如何禁止同时在同一会话中记录不同的角色

时间:2012-10-31 14:05:30

标签: ruby-on-rails session authentication login devise

我使用devise的rails生成器创建了三个不同的角色(User,Admin,Manager),它们存储在不同的表和模型中......

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

1 个答案:

答案 0 :(得分:0)

当用户尝试登录时,您可以验证他是否未以其他角色身份登录。 要做到这一点,你必须覆盖设计SessionsController。它解释了here for 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 repository中找到它。

相关问题