1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed under The GPL-3.0 License |
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
6
|
|
|
* |
7
|
|
|
* @since 2.0.0 |
8
|
|
|
* @author Christopher Castro <[email protected]> |
9
|
|
|
* @link http://www.quickappscms.org |
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
11
|
|
|
*/ |
12
|
|
|
namespace User\Model\Entity; |
13
|
|
|
|
14
|
|
|
use Cake\Auth\DefaultPasswordHasher; |
15
|
|
|
use Cake\Error\FatalErrorException; |
16
|
|
|
use Cake\ORM\Entity; |
17
|
|
|
use Cake\ORM\TableRegistry; |
18
|
|
|
use Cake\Utility\Security; |
19
|
|
|
use CMS\Core\StaticCacheTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Represents single "user" in "users" database table. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class User extends Entity |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
use StaticCacheTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Updates this user's Token value. |
32
|
|
|
* |
33
|
|
|
* The new token is persisted in DB and in this entity property. |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
* @throws \Cake\Error\FatalErrorException When an invalid user entity was given |
37
|
|
|
* @see \User\Model\Table\UsersTable::updateToken() |
38
|
|
|
*/ |
39
|
|
|
public function updateToken() |
40
|
|
|
{ |
41
|
|
|
return TableRegistry::get('User.Users')->updateToken($this); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whether this use belongs to the administrator role. |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isAdmin() |
50
|
|
|
{ |
51
|
|
|
$roles = $this->_getRoleIds(); |
52
|
|
|
|
53
|
|
|
return in_array(ROLE_ID_ADMINISTRATOR, $roles); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Verifies this user is allowed access the given ACO. |
58
|
|
|
* |
59
|
|
|
* ### Usage: |
60
|
|
|
* |
61
|
|
|
* ```php |
62
|
|
|
* // Checks if current user is allowed to edit created contents: |
63
|
|
|
* user()->isAllowed('Content/Admin/Manage/edit'); |
64
|
|
|
* ``` |
65
|
|
|
* |
66
|
|
|
* @param string $aco An ACO path. e.g. `Plugin/Prefix/Controller/action` |
67
|
|
|
* @return bool True if user can access ACO, false otherwise |
68
|
|
|
*/ |
69
|
|
|
public function isAllowed($aco) |
70
|
|
|
{ |
71
|
|
|
$cacheKey = 'isAllowed(' . $this->get('id') . ", {$aco})"; |
72
|
|
|
$cache = static::cache($cacheKey); |
73
|
|
|
if ($cache === null) { |
74
|
|
|
$cache = TableRegistry::get('User.Permissions')->check($this, $aco); |
75
|
|
|
static::cache($cacheKey, $cache); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $cache; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets user default-avatar image's URL. |
83
|
|
|
* |
84
|
|
|
* Powered by Gravatar, it uses user's email to get avatar image URL from |
85
|
|
|
* Gravatar service. |
86
|
|
|
* |
87
|
|
|
* @return string URL to user's avatar |
88
|
|
|
* @link http://www.gravatar.com |
89
|
|
|
*/ |
90
|
|
|
protected function _getAvatar() |
91
|
|
|
{ |
92
|
|
|
return $this->avatar(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets user's real name. |
97
|
|
|
* |
98
|
|
|
* @return string Name |
99
|
|
|
*/ |
100
|
|
|
protected function _getName() |
101
|
|
|
{ |
102
|
|
|
$name = isset($this->_properties['name']) ? $this->_properties['name'] : ''; |
103
|
|
|
|
104
|
|
|
return h($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gets user avatar image's URL. |
109
|
|
|
* |
110
|
|
|
* Powered by Gravatar, it uses user's email to get avatar image URL from |
111
|
|
|
* Gravatar service. |
112
|
|
|
* |
113
|
|
|
* Use this method instead of `avatar` property when you need to customize |
114
|
|
|
* avatar's parameters such as `size`, etc. |
115
|
|
|
* |
116
|
|
|
* ```php |
117
|
|
|
* $user->avatar(['s' => 150]); // instead of: $user->avatar; |
118
|
|
|
* ``` |
119
|
|
|
* |
120
|
|
|
* @param array $options Array of options for Gravatar API |
121
|
|
|
* @return string URL to user's avatar |
122
|
|
|
* @link http://www.gravatar.com |
123
|
|
|
*/ |
124
|
|
|
public function avatar($options = []) |
125
|
|
|
{ |
126
|
|
|
$options = (array)$options; |
127
|
|
|
$options += [ |
128
|
|
|
's' => 80, |
129
|
|
|
'd' => 'mm', |
130
|
|
|
'r' => 'g' |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$url = 'http://www.gravatar.com/avatar/'; |
134
|
|
|
$url .= md5(strtolower(trim($this->get('email')))); |
135
|
|
|
$url .= "?s={$options['s']}&d={$options['d']}&r={$options['r']}"; |
136
|
|
|
|
137
|
|
|
return $url; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Hashes the password if not empty. |
142
|
|
|
* |
143
|
|
|
* @param string $password The RAW password |
144
|
|
|
* @return string Encrypted password |
145
|
|
|
*/ |
146
|
|
|
protected function _setPassword($password) |
147
|
|
|
{ |
148
|
|
|
if (!empty($password)) { |
149
|
|
|
return (new DefaultPasswordHasher)->hash($password); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $password; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Gets an array list of role IDs this user belongs to. |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
protected function _getRoleIds() |
161
|
|
|
{ |
162
|
|
|
$ids = []; |
163
|
|
|
if (!$this->has('roles')) { |
164
|
|
|
return $ids; |
165
|
|
|
} |
166
|
|
|
foreach ($this->roles as $k => $role) { |
167
|
|
|
$ids[] = $role->id; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $ids; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Gets an array list of role NAMES this user belongs to. |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
protected function _getRoleNames() |
179
|
|
|
{ |
180
|
|
|
$names = []; |
181
|
|
|
if (!$this->has('roles')) { |
182
|
|
|
return $names; |
183
|
|
|
} |
184
|
|
|
foreach ($this->roles as $k => $role) { |
185
|
|
|
$names[] = $role->name; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $names; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets an array list of role NAMES this user belongs to. |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
protected function _getRoleSlugs() |
197
|
|
|
{ |
198
|
|
|
$slugs = []; |
199
|
|
|
if (!$this->has('roles')) { |
200
|
|
|
return $slugs; |
201
|
|
|
} |
202
|
|
|
foreach ($this->roles as $role) { |
203
|
|
|
$slugs[] = $role->slug; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $slugs; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Generates cancel code for this user. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
* @throws \Cake\Error\FatalErrorException When code cannot be created |
214
|
|
|
*/ |
215
|
|
|
protected function _getCancelCode() |
216
|
|
|
{ |
217
|
|
|
if (!$this->has('password') && !$this->has('id')) { |
218
|
|
|
throw new FatalErrorException(__d('user', 'Cannot generated cancel code for this user: unknown user ID.')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (!$this->has('password')) { |
222
|
|
|
$password = TableRegistry::get('User.Users') |
223
|
|
|
->get($this->id, ['fields' => ['password']]) |
224
|
|
|
->get('password'); |
225
|
|
|
} else { |
226
|
|
|
$password = $this->password; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return Security::hash($password, 'md5', true); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|