-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathRegisterController.php
More file actions
180 lines (146 loc) · 5.04 KB
/
RegisterController.php
File metadata and controls
180 lines (146 loc) · 5.04 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Exceptions\ValidationException;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Traits\Viewable;
use CodeIgniter\Shield\Validation\ValidationRules;
use Psr\Log\LoggerInterface;
/**
* Class RegisterController
*
* Handles displaying registration form,
* and handling actual registration flow.
*/
class RegisterController extends BaseController
{
use Viewable;
public function initController(
RequestInterface $request,
ResponseInterface $response,
LoggerInterface $logger
): void {
parent::initController(
$request,
$response,
$logger
);
}
/**
* Displays the registration form.
*
* @return RedirectResponse|string
*/
public function registerView()
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->registerRedirect());
}
// Check if registration is allowed
if (! setting('Auth.allowRegistration')) {
return redirect()->back()->withInput()
->with('error', lang('Auth.registerDisabled'));
}
/** @var Session $authenticator */
$authenticator = auth('session')->getAuthenticator();
// If an action has been defined, start it up.
if ($authenticator->hasAction()) {
return redirect()->route('auth-action-show');
}
return $this->view(setting('Auth.views')['register']);
}
/**
* Attempts to register the user.
*/
public function registerAction(): RedirectResponse
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->registerRedirect());
}
// Check if registration is allowed
if (! setting('Auth.allowRegistration')) {
return redirect()->back()->withInput()
->with('error', lang('Auth.registerDisabled'));
}
$users = $this->getUserProvider();
// Validate here first, since some things,
// like the password, can only be validated properly here.
$rules = $this->getValidationRules();
if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
// Save the user
$allowedPostFields = array_keys($rules);
$user = $this->getUserEntity();
$user->fill($this->request->getPost($allowedPostFields));
// Workaround for email only registration/login
if ($user->username === null) {
$user->username = null;
}
try {
$users->save($user);
} catch (ValidationException $e) {
return redirect()->back()->withInput()->with('errors', $users->errors());
}
// To get the complete user object with ID, we need to get from the database
$user = $users->findById($users->getInsertID());
// Add to default group
$users->addToDefaultGroup($user);
Events::trigger('register', $user);
/** @var Session $authenticator */
$authenticator = auth('session')->getAuthenticator();
$authenticator->startLogin($user);
// If an action has been defined for register, start it up.
$hasAction = $authenticator->startUpAction('register', $user);
if ($hasAction) {
return redirect()->route('auth-action-show');
}
// Set the user active
$user->activate();
$authenticator->completeLogin($user);
// Success!
return redirect()->to(config('Auth')->registerRedirect())
->with('message', lang('Auth.registerSuccess'));
}
/**
* Returns the User provider
*/
protected function getUserProvider(): UserModel
{
$provider = model(setting('Auth.userProvider'));
assert($provider instanceof UserModel, 'Config Auth.userProvider is not a valid UserProvider.');
return $provider;
}
/**
* Returns the Entity class that should be used
*/
protected function getUserEntity(): User
{
return new User();
}
/**
* Returns the rules that should be used for validation.
*
* @return array<string, array<string, list<string>|string>>
*/
protected function getValidationRules(): array
{
$rules = new ValidationRules();
return $rules->getRegistrationRules();
}
}