-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathEmailActivator.php
183 lines (150 loc) · 5.4 KB
/
EmailActivator.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Authentication\Actions;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Response;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Entities\UserIdentity;
use CodeIgniter\Shield\Exceptions\LogicException;
use CodeIgniter\Shield\Exceptions\RuntimeException;
use CodeIgniter\Shield\Models\UserIdentityModel;
use CodeIgniter\Shield\Traits\Viewable;
class EmailActivator implements ActionInterface
{
use Viewable;
private string $type = Session::ID_TYPE_EMAIL_ACTIVATE;
/**
* Shows the initial screen to the user telling them
* that an email was just sent to them with a link
* to confirm their email address.
*/
public function show(): string
{
/** @var Session $authenticator */
$authenticator = auth('session')->getAuthenticator();
$user = $authenticator->getPendingUser();
if ($user === null) {
throw new RuntimeException('Cannot get the pending login User.');
}
$userEmail = $user->email;
if ($userEmail === null) {
throw new LogicException(
'Email Activation needs user email address. user_id: ' . $user->id
);
}
$code = $this->createIdentity($user);
/** @var IncomingRequest $request */
$request = service('request');
$ipAddress = $request->getIPAddress();
$userAgent = (string) $request->getUserAgent();
$date = Time::now()->toDateTimeString();
// Send the email
helper('email');
$email = emailer(['mailType' => 'html'])
->setFrom(shieldSetting('Email.fromEmail'), shieldSetting('Email.fromName') ?? '');
$email->setTo($userEmail);
$email->setSubject(lang('Auth.emailActivateSubject'));
$email->setMessage($this->view(
shieldSetting('Auth.views')['action_email_activate_email'],
['code' => $code, 'user' => $user, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date],
['debug' => false]
));
if ($email->send(false) === false) {
throw new RuntimeException('Cannot send email for user: ' . $user->email . "\n" . $email->printDebugger(['headers']));
}
// Clear the email
$email->clear();
// Display the info page
return $this->view(shieldSetting('Auth.views')['action_email_activate_show'], ['user' => $user]);
}
/**
* This method is unused.
*
* @return Response|string
*/
public function handle(IncomingRequest $request)
{
throw new PageNotFoundException();
}
/**
* Verifies the email address and code matches an
* identity we have for that user.
*
* @return RedirectResponse|string
*/
public function verify(IncomingRequest $request)
{
/** @var Session $authenticator */
$authenticator = auth('session')->getAuthenticator();
$postedToken = $request->getVar('token');
$user = $authenticator->getPendingUser();
if ($user === null) {
throw new RuntimeException('Cannot get the pending login User.');
}
$identity = $this->getIdentity($user);
// No match - let them try again.
if (! $authenticator->checkAction($identity, $postedToken)) {
session()->setFlashdata('error', lang('Auth.invalidActivateToken'));
return $this->view(shieldSetting('Auth.views')['action_email_activate_show']);
}
$user = $authenticator->getUser();
// Set the user active now
$user->activate();
// Success!
return redirect()->to(config('Auth')->registerRedirect())
->with('message', lang('Auth.registerSuccess'));
}
/**
* Creates an identity for the action of the user.
*
* @return string secret
*/
public function createIdentity(User $user): string
{
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
// Delete any previous identities for action
$identityModel->deleteIdentitiesByType($user, $this->type);
$generator = static fn (): string => random_string('nozero', 6);
return $identityModel->createCodeIdentity(
$user,
[
'type' => $this->type,
'name' => 'register',
'extra' => lang('Auth.needVerification'),
],
$generator
);
}
/**
* Returns an identity for the action of the user.
*/
private function getIdentity(User $user): ?UserIdentity
{
/** @var UserIdentityModel $identityModel */
$identityModel = model(UserIdentityModel::class);
return $identityModel->getIdentityByType(
$user,
$this->type
);
}
/**
* Returns the string type of the action class.
*/
public function getType(): string
{
return $this->type;
}
}