forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserValidationTrait.php
More file actions
131 lines (123 loc) · 5.2 KB
/
Copy pathUserValidationTrait.php
File metadata and controls
131 lines (123 loc) · 5.2 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
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use Cake\Core\Configure;
use CakeDC\Users\Exception\TokenExpiredException;
use CakeDC\Users\Exception\UserAlreadyActiveException;
use CakeDC\Users\Exception\UserNotFoundException;
use CakeDC\Users\Plugin;
use Exception;
/**
* Covers the user validation
*
* @property \Cake\Http\ServerRequest $request
*/
trait UserValidationTrait
{
/**
* Validates email
*
* @param string $type 'email' or 'password' to validate the user
* @param string $token token
* @return \Cake\Http\Response
*/
public function validate($type = null, $token = null)
{
try {
switch ($type) {
case 'email':
try {
$result = $this->getUsersTable()->validate($token, 'activateUser');
if ($result) {
$event = $this->dispatchEvent(Plugin::EVENT_AFTER_EMAIL_TOKEN_VALIDATION, ['user' => $result]);
if (!empty($event) && is_array($event->getResult())) {
return $this->redirect($event->getResult());
}
$this->Flash->success(__d('cake_d_c/users', 'User account validated successfully'));
} else {
$this->Flash->error(__d('cake_d_c/users', 'User account could not be validated'));
}
} catch (UserAlreadyActiveException $exception) {
$this->Flash->error(__d('cake_d_c/users', 'User already active'));
}
break;
case 'password':
$result = $this->getUsersTable()->validate($token);
if (!empty($result)) {
$this->Flash->success(__d('cake_d_c/users', 'Reset password token was validated successfully'));
$this->getRequest()->getSession()->write(
Configure::read('Users.Key.Session.resetPasswordUserId'),
$result->id
);
return $this->redirect(['action' => 'changePassword']);
} else {
$this->Flash->error(__d('cake_d_c/users', 'Reset password token could not be validated'));
}
break;
default:
$this->Flash->error(__d('cake_d_c/users', 'Invalid validation type'));
}
} catch (UserNotFoundException $ex) {
$this->Flash->error(__d('cake_d_c/users', 'Invalid token or user account already validated'));
} catch (TokenExpiredException $ex) {
$event = $this->dispatchEvent(Plugin::EVENT_ON_EXPIRED_TOKEN, ['type' => $type]);
if (!empty($event) && is_array($event->getResult())) {
return $this->redirect($event->getResult());
}
$this->Flash->error(__d('cake_d_c/users', 'Token already expired'));
}
return $this->redirect(['action' => 'login']);
}
/**
* Resend Token validation
*
* @return mixed
*/
public function resendTokenValidation()
{
$this->set('user', $this->getUsersTable()->newEntity([], ['validate' => false]));
$this->set('_serialize', ['user']);
if (!$this->getRequest()->is('post')) {
return;
}
$reference = $this->getRequest()->getData('reference');
try {
if (
$this->getUsersTable()->resetToken($reference, [
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => true,
'sendEmail' => true,
'type' => 'email',
])
) {
$event = $this->dispatchEvent(Plugin::EVENT_AFTER_RESEND_TOKEN_VALIDATION);
$result = $event->getResult();
if (!empty($event) && is_array($result)) {
return $this->redirect($result);
}
$this->Flash->success(__d(
'cake_d_c/users',
'Token has been reset successfully. Please check your email.'
));
} else {
$this->Flash->error(__d('cake_d_c/users', 'Token could not be reset'));
}
return $this->redirect(['action' => 'login']);
} catch (UserNotFoundException $ex) {
$this->Flash->error(__d('cake_d_c/users', 'User {0} was not found', $reference));
} catch (UserAlreadyActiveException $ex) {
$this->Flash->error(__d('cake_d_c/users', 'User {0} is already active', $reference));
} catch (Exception $ex) {
$this->Flash->error(__d('cake_d_c/users', 'Token could not be reset'));
}
}
}