37

我正在使用设计并创建了一个名为 :active 的用户字段,该字段为真或假。在允许用户登录之前,我必须手动使用户处于活动状态(true)。至少这是意图。我试过这个...

class SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    "resource/signin CREATE"
    self.resource = warden.authenticate!(auth_options)
    unless resource.active?
      sign_out
      redirect_to :sorry_not_active_url
      return
    end
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end  
end

但是,这并不能捕获用户可以登录的所有地方,例如,当用户更改密码时,网站会在之后自动将其自动登录。但是,如果用户不活跃,我不希望他们被允许登录,而是被重定向到 sorry_not_active_url。

如果用户不活跃,防止用户登录的最佳方法是什么?

谢谢你。

4

2 回答 2

96

将这两种方法添加到您的用户模型中,设计应该自动选择它们 - 您不需要扩展Devise::SessionsController

def active_for_authentication?
  super && self.your_method_for_checking_active # i.e. super && self.is_active
end

def inactive_message
  "Sorry, this account has been deactivated."
end
于 2013-04-07T18:55:39.323 回答
7

设计(如果你有设计 3.2+)现在支持block(会话)创建参数

# assuming this is your session controller

class SessionsController < Devise::SessionsController

def create
  super do |resource|
     unless resource.active?
      sign_out
      # you can set flash message as well.
      redirect_to :sorry_not_active_url
      return
    end
  end
end
于 2016-10-09T15:41:01.163 回答