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
157 lines (144 loc) · 4.02 KB
/
Copy pathUser.php
File metadata and controls
157 lines (144 loc) · 4.02 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
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Core\Configure;
use Cake\ORM\Entity;
use Cake\Utility\Text;
use DateTime;
/**
* User Entity.
*/
class User extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'username' => true,
'email' => true,
'password' => true,
'confirm_password' => true,
'first_name' => true,
'last_name' => true,
'token' => true,
'token_expires' => true,
'api_token' => true,
'activation_date' => true,
//tos is a boolean, coming from the "accept the terms of service" checkbox but it is not stored onto database
'tos' => true,
'tos_date' => true,
'active' => true,
'social_accounts' => true,
'current_password' => true
];
/**
* @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', new DateTime());
}
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()
{
return empty($this->token_expires) || strtotime($this->token_expires) < strtotime("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 string $tokenExpiration new token_expires user.
*
* @return void
*/
public function updateToken($tokenExpiration)
{
$expires = new DateTime();
$expires->modify("+ $tokenExpiration secs");
$this->token_expires = $expires;
$this->token = str_replace('-', '', Text::uuid());
}
}