use*_*677 33 ruby ruby-on-rails devise
我正在使用设计并创建了一个名为:active的用户字段,该字段为true或false.在允许用户登录之前,我必须手动使用户处于活动状态(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
Run Code Online (Sandbox Code Playgroud)
但是,这并没有捕获用户可以登录的所有位置,例如,当用户更改其密码时,该站点会自动将其自动登录.但是,如果用户不活动,我不希望他们被允许登录,而是被重定向到sorry_not_active_url.
如果用户不活动,阻止用户登录的最佳方法是什么?
谢谢.
hou*_*se9 81
将这两种方法添加到您的用户模型中,设计应该自动选择它们 - 您不需要扩展 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
Run Code Online (Sandbox Code Playgroud)
设计(如果您有设计 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
Run Code Online (Sandbox Code Playgroud)