forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
163 lines (143 loc) · 3.82 KB
/
Copy pathUser.php
File metadata and controls
163 lines (143 loc) · 3.82 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
<?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\Model\Entity;
use Cake\Core\Configure;
use Cake\I18n\Time;
use Cake\ORM\Entity;
use Cake\Utility\Security;
/**
* User Entity.
*/
class User extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'*' => true,
'id' => false,
'is_superuser' => false,
'role' => false,
];
/**
* Fields that are excluded from JSON an array versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password',
'token',
'token_expires',
'api_token',
];
/**
* @param string $password password that will be set.
* @return bool|string
*/
protected function _setPassword($password)
{
return $this->hashPassword($password);
}
/**
* @param string $password password that will be confirm.
* @return bool|string
*/
protected function _setConfirmPassword($password)
{
return $this->hashPassword($password);
}
/**
* @param string $tos tos option. It will be set the tos_date
* @return bool
*/
protected function _setTos($tos)
{
if ((bool)$tos === true) {
$this->set('tos_date', Time::now());
}
return $tos;
}
/**
* Hash a password using the configured password hasher,
* use DefaultPasswordHasher if no one was configured
*
* @param string $password password to be hashed
* @return mixed
*/
public function hashPassword($password)
{
$PasswordHasher = $this->getPasswordHasher();
return $PasswordHasher->hash($password);
}
/**
* Return the configured Password Hasher
*
* @return mixed
*/
public function getPasswordHasher()
{
$passwordHasher = Configure::read('Users.passwordHasher');
if (!class_exists($passwordHasher)) {
$passwordHasher = '\Cake\Auth\DefaultPasswordHasher';
}
return new $passwordHasher;
}
/**
* Checks if a password is correctly hashed
*
* @param string $password password that will be check.
* @param string $hashedPassword hash used to check password.
* @return bool
*/
public function checkPassword($password, $hashedPassword)
{
$PasswordHasher = $this->getPasswordHasher();
return $PasswordHasher->check($password, $hashedPassword);
}
/**
* Returns if the token has already expired
*
* @return bool
*/
public function tokenExpired()
{
if (empty($this->token_expires)) {
return true;
}
return new Time($this->token_expires) < Time::now();
}
/**
* Getter for user avatar
*
* @return string|null avatar
*/
protected function _getAvatar()
{
$avatar = null;
if (!empty($this->_properties['social_accounts'][0])) {
$avatar = $this->_properties['social_accounts'][0]['avatar'];
}
return $avatar;
}
/**
* Generate token_expires and token in a user
* @param int $tokenExpiration seconds to expire the token from Now
* @return void
*/
public function updateToken($tokenExpiration = 0)
{
$expiration = new Time('now');
$this->token_expires = $expiration->addSeconds($tokenExpiration);
$this->token = bin2hex(Security::randomBytes(16));
}
}