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
123 lines (116 loc) · 4.79 KB
/
Copy pathUserValidationTrait.php
File metadata and controls
123 lines (116 loc) · 4.79 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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use CakeDC\Users\Exception\TokenExpiredException;
use CakeDC\Users\Exception\UserAlreadyActiveException;
use CakeDC\Users\Exception\UserNotFoundException;
use Cake\Core\Configure;
use Cake\Http\Response;
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 Response
*/
public function validate($type = null, $token = null)
{
try {
switch ($type) {
case 'email':
try {
$result = $this->getUsersTable()->validate($token, 'activateUser');
if ($result) {
$this->Flash->success(__d('CakeDC/Users', 'User account validated successfully'));
} else {
$this->Flash->error(__d('CakeDC/Users', 'User account could not be validated'));
}
} catch (UserAlreadyActiveException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'User already active'));
}
break;
case 'password':
$result = $this->getUsersTable()->validate($token);
if (!empty($result)) {
$this->Flash->success(__d('CakeDC/Users', 'Reset password token was validated successfully'));
$this->request->getSession()->write(
Configure::read('Users.Key.Session.resetPasswordUserId'),
$result->id
);
return $this->redirect(['action' => 'changePassword']);
} else {
$this->Flash->error(__d('CakeDC/Users', 'Reset password token could not be validated'));
}
break;
default:
$this->Flash->error(__d('CakeDC/Users', 'Invalid validation type'));
}
} catch (UserNotFoundException $ex) {
$this->Flash->error(__d('CakeDC/Users', 'Invalid token or user account already validated'));
} catch (TokenExpiredException $ex) {
$event = $this->dispatchEvent(UsersAuthComponent::EVENT_ON_EXPIRED_TOKEN, ['type' => $type]);
if (!empty($event) && is_array($event->result)) {
return $this->redirect($event->result);
}
$this->Flash->error(__d('CakeDC/Users', 'Token already expired'));
}
return $this->redirect(['action' => 'login']);
}
/**
* Resend Token validation
*
* @return mixed
*/
public function resendTokenValidation()
{
$this->set('user', $this->getUsersTable()->newEntity());
$this->set('_serialize', ['user']);
if (!$this->request->is('post')) {
return;
}
$reference = $this->request->getData('reference');
try {
if ($this->getUsersTable()->resetToken($reference, [
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => true,
'sendEmail' => true,
'type' => 'email'
])) {
$event = $this->dispatchEvent(UsersAuthComponent::EVENT_AFTER_RESEND_TOKEN_VALIDATION);
if (!empty($event) && is_array($event->result)) {
return $this->redirect($event->result);
}
$this->Flash->success(__d(
'CakeDC/Users',
'Token has been reset successfully. Please check your email.'
));
} else {
$this->Flash->error(__d('CakeDC/Users', 'Token could not be reset'));
}
return $this->redirect(['action' => 'login']);
} catch (UserNotFoundException $ex) {
$this->Flash->error(__d('CakeDC/Users', 'User {0} was not found', $reference));
} catch (UserAlreadyActiveException $ex) {
$this->Flash->error(__d('CakeDC/Users', 'User {0} is already active', $reference));
} catch (Exception $ex) {
$this->Flash->error(__d('CakeDC/Users', 'Token could not be reset'));
}
}
}