forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_activator.rb
78 lines (62 loc) · 1.45 KB
/
user_activator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
class UserActivator
attr_reader :user, :request, :session, :cookies, :message
def initialize(user, request, session, cookies)
@user = user
@session = session
@cookies = cookies
@request = request
@message = nil
end
def start
end
def finish
@message = activator.activate
end
def success_message
activator.success_message
end
private
def activator
factory.new(user, request, session, cookies)
end
def factory
invite = Invite.find_by(email: Email.downcase(@user.email))
if !user.active?
EmailActivator
elsif SiteSetting.must_approve_users? && !(invite.present? && invite.redeemable?)
ApprovalActivator
else
LoginActivator
end
end
end
class ApprovalActivator < UserActivator
def activate
success_message
end
def success_message
I18n.t("login.wait_approval")
end
end
class EmailActivator < UserActivator
def activate
email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:signup])
EmailToken.enqueue_signup_email(email_token)
success_message
end
def success_message
I18n.t("login.activate_email", email: Rack::Utils.escape_html(user.email))
end
end
class LoginActivator < UserActivator
include CurrentUser
def activate
log_on_user(user)
user.enqueue_welcome_message("welcome_user")
success_message
end
def success_message
I18n.t("login.active")
end
end