-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathUserComponent.php
More file actions
274 lines (244 loc) · 7.41 KB
/
UserComponent.php
File metadata and controls
274 lines (244 loc) · 7.41 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.0.0
*/
namespace App\Controller\Component;
use App\Model\Entity\Role;
use App\Utility\ExtendedUserAccessControl;
use App\Utility\UserAccessControl;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\ORM\TableRegistry;
use donatj\UserAgent\UserAgentParser;
use Exception;
/**
* @property \Authentication\Controller\Component\AuthenticationComponent $Authentication
*/
class UserComponent extends Component
{
public array $components = ['Authentication.Authentication'];
/**
* @var string|null cache for role uuid
*/
protected ?string $_roleId = null;
/**
* User agent cache to avoid parsing multiple times per request
*
* @var array|null
*/
protected ?array $_userAgent = null;
/**
* Return the current user id if the user is identified
*
* @return string|null
*/
public function id(): ?string
{
return $this->getAuthenticatedUserProperty('id');
}
/**
* Return the current username if the user is identified
*
* @return string|null
*/
public function username(): ?string
{
return $this->getAuthenticatedUserProperty('username');
}
/**
* @return string client ip
*/
public function ip(): string
{
return $this->getController()->getRequest()->clientIp();
}
/**
* @return string user agent
*/
public function userAgent(): string
{
return $this->getController()->getRequest()->getEnv('HTTP_USER_AGENT') ?? 'undefined';
}
/**
* Return the current user role or GUEST if the user is not identified
*
* @return string|null
*/
public function role(): ?string
{
return $this->getAuthenticatedUserProperty('role.name', Role::GUEST);
}
/**
* @return \App\Model\Entity\Role
*/
public function getRoleEntity(): Role
{
$role = $this->Authentication->getIdentity()->getOriginalData()['role'] ?? null;
if (is_null($role)) {
/** @var \App\Model\Table\RolesTable $Roles */
$Roles = TableRegistry::getTableLocator()->get('Roles');
return $Roles->find()->where(['name' => Role::GUEST])->first();
}
return $role;
}
/**
* Return the role id if set or the id of Guest role
* Return null if database role guest entry is not present (aka fresh)
*
* @return string|null
*/
public function roleId(): ?string
{
$roleId = $this->getAuthenticatedUserProperty('role_id') ?? null;
if ($roleId === null) {
/** @var \App\Model\Table\RolesTable $Roles */
$Roles = TableRegistry::getTableLocator()->get('Roles');
return $Roles->getIdByName(Role::GUEST);
}
return $roleId;
}
/**
* Return true if the current user is an administrator
*
* @return bool
*/
public function isAdmin(): bool
{
return $this->role() == Role::ADMIN;
}
/**
* Return true if the current user is a guest
*
* @return bool
*/
public function isGuest(): bool
{
return $this->role() == Role::GUEST;
}
/**
* @return \App\Utility\UserAccessControl
*/
public function getAccessControl(): UserAccessControl
{
return new UserAccessControl($this->role(), $this->id(), $this->username());
}
/**
* @return \App\Utility\ExtendedUserAccessControl
*/
public function getExtendAccessControl(): ExtendedUserAccessControl
{
return new ExtendedUserAccessControl(
$this->role(),
$this->id(),
$this->username(),
$this->ip(),
$this->userAgent()
);
}
/**
* Get a given property of the authenticated user.
*
* @param string $property Property name (e.g. username, or id, or role.name)
* @param string|null $default Default value if not found
* @return string|null
*/
protected function getAuthenticatedUserProperty(string $property, ?string $default = null): ?string
{
if ($this->Authentication->getIdentity() === null) {
return $default;
}
return $this->Authentication->getIdentityData($property) ?? $default;
}
/**
* Get user agent details from name defined in environment variable
*
* @return array
* @throws \Exception
*/
public function agent()
{
if (!isset($this->_userAgent)) {
// Parse the user agent string.
try {
/** @var string|null $agent */
$agent = env('HTTP_USER_AGENT');
if ($agent === null) {
throw new Exception(__('undefined user agent'));
}
$provider = new UserAgentParser();
$parser = $provider->parse($agent);
$this->_userAgent['Browser']['name'] = $parser->browser();
$this->_userAgent['Browser']['version'] = $parser->browserVersion();
} catch (Exception $e) {
// Failure is not an option
$this->_userAgent['Browser']['name'] = 'invalid';
$this->_userAgent['Browser']['version'] = 'invalid';
}
}
return $this->_userAgent;
}
/**
* Get the user theme
*
* @return string|null
*/
public function theme()
{
if (Configure::read('passbolt.plugins.accountSettings')) {
/** @var \Passbolt\AccountSettings\Model\Table\AccountSettingsTable $AccountSettings */
$AccountSettings = TableRegistry::getTableLocator()->get('Passbolt/AccountSettings.AccountSettings');
$theme = $AccountSettings->getTheme($this->id());
if ($theme) {
return $theme;
}
}
return null;
}
/**
* Allow admins only.
*
* @param string|null $msg Optional message
* @throws \Cake\Http\Exception\ForbiddenException
* @return void
*/
public function assertIsAdmin(?string $msg = null): void
{
if (!$this->isAdmin()) {
throw new ForbiddenException($msg ?? __('Access restricted to administrators.'));
}
}
/**
* Allow guests only.
*
* @throws \Cake\Http\Exception\ForbiddenException
* @return void
*/
public function assertIsGuest(): void
{
if (!$this->isGuest()) {
throw new ForbiddenException(__('Access restricted to unauthenticated users.'));
}
}
/**
* @throws \Cake\Http\Exception\BadRequestException if user is not guest
* @return void
*/
public function assertNotLoggedIn(): void
{
if ($this->role() !== Role::GUEST) {
throw new ForbiddenException(__('The user should not be logged in.'));
}
}
}