2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
class UserAuthenticator
|
2014-11-17 19:04:29 +08:00
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
def initialize(user, session, authenticator_finder = Users::OmniauthCallbacksController)
|
|
|
|
@user = user
|
|
|
|
@session = session[:authentication]
|
|
|
|
@authenticator_finder = authenticator_finder
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
if authenticated?
|
|
|
|
@user.active = true
|
|
|
|
else
|
|
|
|
@user.password_required!
|
|
|
|
end
|
2016-09-08 02:05:46 +08:00
|
|
|
|
|
|
|
@user.skip_email_validation = true if @session && @session[:skip_email_validation].present?
|
2013-11-13 06:37:38 +08:00
|
|
|
end
|
|
|
|
|
2014-03-20 11:49:25 +08:00
|
|
|
def has_authenticator?
|
|
|
|
!!authenticator
|
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
def finish
|
2018-05-23 07:26:07 +08:00
|
|
|
if authenticator
|
|
|
|
authenticator.after_create_account(@user, @session)
|
|
|
|
confirm_email
|
|
|
|
end
|
2013-11-13 06:37:38 +08:00
|
|
|
@session = nil
|
|
|
|
end
|
|
|
|
|
2017-10-17 01:51:35 +08:00
|
|
|
def email_valid?
|
|
|
|
@session && @session[:email_valid]
|
|
|
|
end
|
2013-11-13 06:37:38 +08:00
|
|
|
|
|
|
|
def authenticated?
|
2018-05-23 07:26:07 +08:00
|
|
|
@session && @session[:email]&.downcase == @user.email.downcase && @session[:email_valid].to_s == "true"
|
2013-11-13 06:37:38 +08:00
|
|
|
end
|
|
|
|
|
2017-10-17 01:51:35 +08:00
|
|
|
private
|
|
|
|
|
2018-05-23 07:26:07 +08:00
|
|
|
def confirm_email
|
|
|
|
if authenticated?
|
|
|
|
EmailToken.confirm(@user.email_tokens.first.token)
|
|
|
|
@user.set_automatic_groups
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
def authenticator
|
|
|
|
if authenticator_name
|
|
|
|
@authenticator ||= @authenticator_finder.find_authenticator(authenticator_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticator_name
|
|
|
|
@session && @session[:authenticator_name]
|
|
|
|
end
|
2014-11-17 19:04:29 +08:00
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
end
|