-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathUserBase.php
More file actions
454 lines (393 loc) · 11.4 KB
/
UserBase.php
File metadata and controls
454 lines (393 loc) · 11.4 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Model;
use DateTimeZone;
use JsonSerializable;
use OCA\Polls\Helper\Container;
use OCA\Polls\Model\Group\Circle;
use OCA\Polls\Model\Group\ContactGroup;
use OCA\Polls\Model\Group\Group;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Model\User\Admin;
use OCA\Polls\Model\User\Contact;
use OCA\Polls\Model\User\Cron;
use OCA\Polls\Model\User\Email;
use OCA\Polls\Model\User\Ghost;
use OCA\Polls\Model\User\User;
use OCA\Polls\UserSession;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\Share\IShare;
class UserBase implements JsonSerializable {
/** @var string */
public const TYPE = 'generic';
/** @var string */
public const TYPE_PUBLIC = 'public';
/** @var string */
public const TYPE_EXTERNAL = 'external';
public const TYPE_EMPTY = 'empty';
public const TYPE_GUEST = 'guest';
public const TYPE_CIRCLE = Circle::TYPE;
public const TYPE_CONTACT = Contact::TYPE;
public const TYPE_CONTACTGROUP = ContactGroup::TYPE;
public const TYPE_EMAIL = Email::TYPE;
public const TYPE_GHOST = Ghost::TYPE;
public const TYPE_GROUP = Group::TYPE;
public const TYPE_USER = User::TYPE;
public const TYPE_ADMIN = Admin::TYPE;
public const TYPE_CRON = Cron::TYPE;
/** @var string[] */
protected array $categories = [];
protected string $description = '';
protected string $richObjectType = self::TYPE_USER;
protected string $organisation = '';
protected IDateTimeZone $timeZone;
protected IGroupManager $groupManager;
protected IL10N $l10n;
protected UserSession $userSession;
protected AppSettings $appSettings;
public function __construct(
protected string $id,
protected string $type,
protected string $displayName = '',
protected string $emailAddress = '',
protected string $languageCode = '',
protected string $localeCode = '',
protected string $timeZoneName = '',
) {
$this->l10n = Container::getL10N();
$this->groupManager = Container::queryClass(IGroupManager::class);
$this->timeZone = Container::queryClass(IDateTimeZone::class);
$this->userSession = Container::queryClass(UserSession::class);
$this->appSettings = Container::queryClass(AppSettings::class);
}
public function getId(): string {
return $this->id;
}
public function getShareUserId(): string {
return $this->getId();
}
/**
* for later use
*/
public function getPrincipalUri(): string {
return '';
}
/**
* hash the real userId to obfuscate the real userId
*/
public function getHashedUserId(): string {
// TODO: add a session salt
return hash('md5', $this->id);
}
/**
* Returns the users' type
*
* Returned Type will be one of
* Contact::TYPE (Share),
* Email::TYPE (Share),
* Ghost::TYPE (deleted user),
* User::TYPE (NC User),
* Admin::TYPE (NC user with admin rights),
* Group::TYPE (NC Group),
* Circle::TYPE (Share),
* ContactGroup::TYPE (Share)
* Anon::TYPE (Anonymized User)
*
* @return string
**/
public function getType(): string {
return $this->type;
}
/**
* Returns the users' type used in shares
*
* Returned Type will be one of
* Email::TYPE (Share),
* Ghost::TYPE (deleted user),
* User::TYPE (NC User),
* Admin::TYPE (NC user with admin rights),
* Group::TYPE (NC Group),
* Circle::TYPE (Share),
* ContactGroup::TYPE (Share)
* Anon::TYPE (Anonymized User)
*
* @return string
**/
public function getShareType(): string {
return $this->type;
}
public function getIsGuest(): bool {
return !in_array($this->type, [User::TYPE, Admin::TYPE]);
}
/**
* used for telling internal from guest users
*/
public function getSimpleType(): string {
return in_array($this->type, [User::TYPE, Admin::TYPE]) ? self::TYPE_USER : self::TYPE_GUEST;
}
public function getLanguageCode(): string {
return $this->languageCode;
}
public function getLocaleCode(): string {
if (!$this->localeCode) {
return $this->languageCode;
}
return $this->localeCode;
}
public function getTimeZone(): DateTimeZone {
if ($this->timeZoneName) {
return new DateTimeZone($this->timeZoneName);
}
return new DateTimeZone($this->timeZone->getTimeZone()->getName());
}
public function getTimeZoneName(): string {
return $this->timeZoneName;
}
public function getDisplayName(): string {
return $this->displayName;
}
public function getDescription(): string {
return $this->description;
}
public function getSubName(): string {
return $this->getDescription();
}
public function getEmailAddress(): string {
return $this->emailAddress;
}
public function getEmailAndDisplayName(): string {
return $this->getDisplayName() . ' <' . $this->getEmailAddress() . '>';
}
public function getHasEmail(): bool {
return boolVal($this->getEmailAddress());
}
/**
* return true, if the $checkname is equal to the userid or displayName (case insensitive)
*/
public function hasName(string $checkName): bool {
return in_array(strtolower($checkName), [
strtolower($this->getDisplayName()),
strtolower($this->getId()),
]);
}
/**
* @return string[]
*
* @psalm-return array<array-key, string>
*/
public function getCategories(): array {
return $this->categories;
}
public function getIsNoUser(): bool {
return $this->getSimpleType() !== self::TYPE_USER;
}
public function getRichObjectString() : array {
return [
'type' => $this->richObjectType,
'id' => $this->getId(),
'name' => $this->getDisplayName(),
];
}
/**
* search all possible sharees - use ISearch to respect autocomplete restrictions
*/
public static function search(string $query = ''): array {
$items = [];
$types = [
IShare::TYPE_USER,
IShare::TYPE_GROUP,
IShare::TYPE_EMAIL
];
if (Circle::isEnabled() && class_exists('\OCA\Circles\ShareByCircleProvider')) {
$types[] = IShare::TYPE_CIRCLE;
}
[$result] = Container::queryClass(ISearch::class)->search($query, $types, false, 200, 0);
foreach (($result['users'] ?? []) as $item) {
$items[] = new User($item['value']['shareWith']);
}
foreach (($result['exact']['users'] ?? []) as $item) {
$items[] = new User($item['value']['shareWith']);
}
foreach (($result['groups'] ?? []) as $item) {
$items[] = new Group($item['value']['shareWith']);
}
foreach (($result['exact']['groups'] ?? []) as $item) {
$items[] = new Group($item['value']['shareWith']);
}
$items = array_merge($items, Contact::search($query));
$items = array_merge($items, ContactGroup::search($query));
if (Circle::isEnabled()) {
foreach (($result['circles'] ?? []) as $item) {
$items[] = new Circle($item['value']['shareWith']);
}
foreach (($result['exact']['circles'] ?? []) as $item) {
$items[] = new Circle($item['value']['shareWith']);
}
}
return $items;
}
/**
* Default is an array with self as single element
* @return array
*/
public function getMembers(): array {
return [$this];
}
/** @psalm-suppress PossiblyUnusedMethod */
public function jsonSerialize(): array {
if ($this->getIsCurrentUser()) {
return $this->getRichUserArray();
}
return $this->getSimpleUserArray();
}
/**
* Full user array for poll owners, delegated poll admins and the current user himself
* without obfuscating/anonymizing
*
* @return (bool|string|string[])[]
*/
public function getRichUserArray(): array {
return [
'array' => 'richArray',
'categories' => $this->getCategories(),
'desc' => $this->getDescription(),
'displayName' => $this->getDisplayName(),
'emailAddress' => $this->getEmailAddress(),
'id' => $this->getId(),
'isGuest' => $this->getIsGuest(),
'isNoUser' => $this->getIsNoUser(),
'isUnrestrictedOwner' => $this->getIsUnrestrictedPollOwner(),
'languageCode' => $this->getLanguageCode(),
'localeCode' => $this->getLocaleCode(),
'organisation' => $this->getOrganisation(),
'subname' => $this->getSubName(),
'subtitle' => $this->getDescription(),
'timeZone' => $this->getTimeZoneName(),
'type' => $this->getType(),
'userId' => $this->getId(),
];
}
/**
* privacy and anonymizing section
*/
/**
* Simply user array returning safe attributes
*
* @return (bool|null|string)[]
*/
protected function getSimpleUserArray(): array {
return [
'array' => 'simpleArray',
'categories' => '',
'desc' => '',
'displayName' => $this->getSafeDisplayName(),
'emailAddress' => $this->getSafeEmailAddress(),
'id' => $this->getSafeId(),
'isNoUser' => $this->getIsNoUser(),
'isGuest' => $this->getIsGuest(),
'isUnrestrictedOwner' => false,
'languageCode' => '',
'localeCode' => '',
'organisation' => '',
'subname' => '',
'subtitle' => '',
'timeZone' => '',
'type' => $this->getSafeType(),
'userId' => $this->getSafeId(),
];
}
/**
* returns the safe id to avoid leaking the userId
*/
public function getSafeId(): string {
// always return real userId for the current user
if ($this->getIsCurrentUser()) {
return $this->getId();
}
// hash the userId, if user is not logged in
if (!$this->userSession->getIsLoggedIn()) {
return $this->getHashedUserId();
}
// otherwise return the obfuscated userId
return $this->getId();
}
/**
* anonymize the displayname in case of anonymous settings
*/
public function getSafeDisplayName(): string {
return $this->displayName;
}
// Function for obfuscating mail adresses; Default return the email address
public function getSafeEmailAddress(): string {
// always return real email address if this user object is the current user
if ($this->getIsCurrentUser()) {
return $this->getEmailAddress();
}
// hide email address, if user is denied to see email addresses (App setting)
// hide email address, if user is not logged in
if (!$this->appSettings->getAllowSeeMailAddresses() || !$this->userSession->getIsLoggedIn()) {
return '';
}
return $this->getEmailAddress();
}
public function getOrganisation(): string {
return $this->organisation;
}
public function getIsCurrentUser(): bool {
return $this->getId() === $this->userSession->getCurrentUserId();
}
/**
* returns true, if the user is an unrestricted owner
* Only valid for User
*/
public function getIsUnrestrictedPollOwner(): bool {
return false;
}
/**
* returns true, if the user is an admin
* Only valid for User, false for other user types
*/
public function getIsAdmin(): bool {
return false;
}
// TODO: reactivate this function later
/** @psalm-suppress PossiblyUnusedMethod */
public function getIsSystemUser(): bool {
return $this->groupManager->isAdmin($this->getId());
}
/**
* returns true, if the user is member of the requested group
* Only valid for User, false for other user types
*/
public function getIsInGroup(string $groupName): bool {
return false;
}
/**
* returns true, if the user is member of one of the requested groups
* Only valid for User, false for other user types
*/
public function getIsInGroupArray(array $groupNames): bool {
return false;
}
/**
* returns the safe id to avoid leaking the real user type
*/
public function getSafeType(): string {
// always return real type for the current user
if ($this->getIsCurrentUser()) {
return $this->getType();
}
// return simple type, if user is not logged in
if (!$this->userSession->getIsLoggedIn()) {
return $this->getSimpleType();
}
return $this->getType();
}
}