Ruby on Rails:编辑会话控制器创建操作以检查用户是否处于活动状态
Ruby on Rails: Edit Sessions controller create action to check if user is active
我需要更改会话控制器创建方法以首先检查用户是否为 deactivation_date
.
填写了一个列
如果该列有值,则用户已停用并且登录身份验证应该以与用户不存在相同的方式失败。
class Users::SessionsController < Devise::SessionsController
def create
logger.debug('create')
self.resource = warden.authenticate!(auth_options)
if self.resource.deactivation_date?
logger.debug('user has been deactivated')
flash.now[:notice] = "Sorry, this account has been deactivated."
else
set_flash_message!(:notice, :signed_in)
sign_in(resource_name, resource)
respond_with resource, location: after_sign_in_path_for(resource)
end
end
end
我已经尝试覆盖会话控制器以及创建方法。
我上面的代码只是防止重定向。刷新页面后,无论如何都会对用户进行身份验证。
有人可以帮我指明正确的方向吗?
最简单的方法就是添加一个自定义 valid_for_authentication?正在使用资源设计中的方法,因此在 Users.rb 中:
def valid_for_authentication?
super && !deactivation_date?
end
感谢这个问题:Check if user is active before allowing user to sign in with devise (rails)
我能够简单地向我的用户模型添加一个方法并设计自动获取它
在我的用户模型中:
def active_for_authentication?
super && !self.deactivation_date
end
起初我不知道这个方法名是专门设计的。必须正好是 active_for_authentication?
我需要更改会话控制器创建方法以首先检查用户是否为 deactivation_date
.
如果该列有值,则用户已停用并且登录身份验证应该以与用户不存在相同的方式失败。
class Users::SessionsController < Devise::SessionsController
def create
logger.debug('create')
self.resource = warden.authenticate!(auth_options)
if self.resource.deactivation_date?
logger.debug('user has been deactivated')
flash.now[:notice] = "Sorry, this account has been deactivated."
else
set_flash_message!(:notice, :signed_in)
sign_in(resource_name, resource)
respond_with resource, location: after_sign_in_path_for(resource)
end
end
end
我已经尝试覆盖会话控制器以及创建方法。
我上面的代码只是防止重定向。刷新页面后,无论如何都会对用户进行身份验证。
有人可以帮我指明正确的方向吗?
最简单的方法就是添加一个自定义 valid_for_authentication?正在使用资源设计中的方法,因此在 Users.rb 中:
def valid_for_authentication?
super && !deactivation_date?
end
感谢这个问题:Check if user is active before allowing user to sign in with devise (rails)
我能够简单地向我的用户模型添加一个方法并设计自动获取它
在我的用户模型中:
def active_for_authentication?
super && !self.deactivation_date
end
起初我不知道这个方法名是专门设计的。必须正好是 active_for_authentication?