2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-18 10:37:38 -04:00
|
|
|
class UserActivator
|
2013-11-12 17:37:38 -05:00
|
|
|
attr_reader :user, :request, :session, :cookies, :message
|
2013-10-09 00:10:37 -04:00
|
|
|
|
|
|
|
def initialize(user, request, session, cookies)
|
2013-08-18 10:37:38 -04:00
|
|
|
@user = user
|
|
|
|
@session = session
|
|
|
|
@cookies = cookies
|
2013-10-09 00:10:37 -04:00
|
|
|
@request = request
|
2013-11-12 17:37:38 -05:00
|
|
|
@message = nil
|
2013-08-18 10:37:38 -04:00
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
def start
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
|
|
|
@message = activator.activate
|
2013-08-18 10:37:38 -04:00
|
|
|
end
|
|
|
|
|
2017-10-03 14:08:37 -04:00
|
|
|
def success_message
|
|
|
|
activator.success_message
|
|
|
|
end
|
|
|
|
|
2013-08-18 10:37:38 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def activator
|
2013-10-09 00:10:37 -04:00
|
|
|
factory.new(user, request, session, cookies)
|
2013-08-18 10:37:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def factory
|
2015-06-25 15:51:26 -04:00
|
|
|
invite = Invite.find_by(email: Email.downcase(@user.email))
|
2017-09-12 15:04:39 -04:00
|
|
|
|
|
|
|
if !user.active?
|
2013-08-18 10:37:38 -04:00
|
|
|
EmailActivator
|
2017-09-12 15:04:39 -04:00
|
|
|
elsif SiteSetting.must_approve_users? && !(invite.present? && !invite.expired? && !invite.destroyed? && invite.link_valid?)
|
|
|
|
ApprovalActivator
|
2013-08-18 10:37:38 -04:00
|
|
|
else
|
|
|
|
LoginActivator
|
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
2013-08-18 10:37:38 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class ApprovalActivator < UserActivator
|
|
|
|
def activate
|
2017-10-03 14:08:37 -04:00
|
|
|
success_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def success_message
|
2013-08-18 10:37:38 -04:00
|
|
|
I18n.t("login.wait_approval")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EmailActivator < UserActivator
|
|
|
|
def activate
|
2015-08-24 02:21:34 -04:00
|
|
|
email_token = user.email_tokens.unconfirmed.active.first
|
|
|
|
email_token = user.email_tokens.create(email: user.email) if email_token.nil?
|
|
|
|
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.enqueue(:critical_user_email,
|
2013-08-18 10:37:38 -04:00
|
|
|
type: :signup,
|
|
|
|
user_id: user.id,
|
2015-08-24 02:21:34 -04:00
|
|
|
email_token: email_token.token
|
2013-08-18 10:37:38 -04:00
|
|
|
)
|
2017-10-03 14:08:37 -04:00
|
|
|
|
|
|
|
success_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def success_message
|
2016-08-05 12:01:16 -04:00
|
|
|
I18n.t("login.activate_email", email: Rack::Utils.escape_html(user.email))
|
2013-08-18 10:37:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class LoginActivator < UserActivator
|
|
|
|
include CurrentUser
|
|
|
|
|
|
|
|
def activate
|
|
|
|
log_on_user(user)
|
|
|
|
user.enqueue_welcome_message('welcome_user')
|
2017-10-03 14:08:37 -04:00
|
|
|
success_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def success_message
|
2013-08-18 10:37:38 -04:00
|
|
|
I18n.t("login.active")
|
|
|
|
end
|
|
|
|
end
|