-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathRegisterBehavior.php
More file actions
228 lines (203 loc) · 6.99 KB
/
RegisterBehavior.php
File metadata and controls
228 lines (203 loc) · 6.99 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2026, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2026, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Model\Behavior;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Mailer\MailerAwareTrait;
use Cake\Validation\Validator;
use CakeDC\Users\Exception\TokenExpiredException;
use CakeDC\Users\Exception\UserAlreadyActiveException;
use CakeDC\Users\Exception\UserNotFoundException;
/**
* Covers the user registration
*/
class RegisterBehavior extends BaseTokenBehavior
{
use MailerAwareTrait;
/**
* @var bool
*/
protected bool $validateEmail = true;
/**
* @var bool
*/
protected bool $useTos = true;
/**
* Constructor hook method.
*
* @param array $config The configuration settings provided to this behavior.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->validateEmail = (bool)Configure::read('Users.Email.validate');
$this->useTos = (bool)Configure::read('Users.Tos.required');
}
/**
* Registers an user.
*
* @param \Cake\Datasource\EntityInterface $user User information
* @param array $data User information
* @param array $options ['tokenExpiration]
* @return \Cake\Datasource\EntityInterface|bool
*/
public function register($user, $data, $options)
{
$validateEmail = (bool)($options['validate_email'] ?? null);
$tokenExpiration = $options['token_expiration'] ?? null;
$validator = $options['validator'] ?? null;
if (is_string($validator)) {
$validate = $validator;
} else {
$this->_table->setValidator('current', $validator ?: $this->getRegisterValidators($options));
$validate = 'current';
}
$user = $this->_table->patchEntity(
$user,
$data,
['validate' => $validate],
);
$user['role'] = Configure::read('Users.Registration.defaultRole') ?: 'user';
$user->validated = false;
//@todo move updateActive to afterSave?
$user = $this->_updateActive($user, $validateEmail, $tokenExpiration);
$this->_table->isValidateEmail = $validateEmail;
$userSaved = $this->_table->save($user);
if ($userSaved && $validateEmail) {
$this->_sendValidationEmail($user);
}
return $userSaved;
}
/**
* Validates token and return user
*
* @param string $token toke to be validated.
* @param null $callback function that will be returned.
* @throws \CakeDC\Users\Exception\TokenExpiredException when token has expired.
* @throws \CakeDC\Users\Exception\UserNotFoundException when user isn't found.
* @return \Cake\Datasource\EntityInterface $user
*/
public function validate($token, $callback = null)
{
$user = $token ? $this->_table->find()
->select(['token_expires', 'id', 'active', 'token'])
->where(['token' => $token])
->first() : null;
if (empty($user)) {
throw new UserNotFoundException(__d('cake_d_c/users', 'User not found for the given token and email.'));
}
if ($user->tokenExpired()) {
throw new TokenExpiredException(__d('cake_d_c/users', 'Token has already expired user with no token'));
}
if (!method_exists($this, (string)$callback)) {
return $user;
}
return $this->_table->getBehavior('Register')->{$callback}($user);
}
/**
* Activates an user
*
* @param \Cake\Datasource\EntityInterface $user user object.
* @return mixed User entity or bool false if the user could not be activated
* @throws \CakeDC\Users\Exception\UserAlreadyActiveException
*/
public function activateUser(EntityInterface $user)
{
if ($user->active) {
throw new UserAlreadyActiveException(__d('cake_d_c/users', 'User account already validated'));
}
$user->set('token', null);
$user->set('token_expires', null);
$user->set('activation_date', new \DateTime());
$user->set('active', true);
return $this->_table->save($user);
}
/**
* buildValidator
*
* @param \Cake\Event\Event $event event
* @param \Cake\Validation\Validator $validator validator
* @param string $name name
* @return void
*/
public function buildValidator(\Cake\Event\EventInterface $event, Validator $validator, $name): void
{
if ($name === 'default') {
$event->setResult($this->_emailValidator($validator, $this->validateEmail));
return;
}
$event->setResult($validator);
}
/**
* Email validator
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @param bool $validateEmail true when email needs to be required
* @return \Cake\Validation\Validator
*/
protected function _emailValidator(Validator $validator, $validateEmail)
{
$this->validateEmail = (bool)$validateEmail;
$validator
->add('email', 'valid_email', ['rule' => 'email'])
->notBlank('email', __d('cake_d_c/users', 'This field is required'), function () {
return $this->validateEmail;
});
return $validator;
}
/**
* Tos validator
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
protected function _tosValidator(Validator $validator)
{
$validator
->requirePresence('tos', 'create')
->notBlank('tos');
return $validator;
}
/**
* Returns the list of validators
*
* @param array $options Array of options ['validate_email' => true/false, 'use_tos' => true/false]
* @return \Cake\Validation\Validator
*/
public function getRegisterValidators($options)
{
$validateEmail = $options['validate_email'] ?? null;
$useTos = $options['use_tos'] ?? null;
$validator = $this->_table->validationRegister(new Validator());
if ($useTos) {
$validator = $this->_tosValidator($validator);
}
if ($validateEmail) {
$validator = $this->_emailValidator($validator, $validateEmail);
}
return $validator;
}
/**
* Wrapper for mailer
*
* @param \Cake\Datasource\EntityInterface $user user
* @return void
*/
protected function _sendValidationEmail($user)
{
$mailer = Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users';
$this
->getMailer($mailer)
->send('validation', [$user]);
}
}