-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathUserBase.php
More file actions
461 lines (398 loc) · 11.6 KB
/
UserBase.php
File metadata and controls
461 lines (398 loc) · 11.6 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
455
456
457
458
459
460
461
<?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\Helper\Hash;
use OCA\Polls\Model\Group\ContactGroup;
use OCA\Polls\Model\Group\Group;
use OCA\Polls\Model\Group\Team;
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\IGroupManager;
use OCP\IL10N;
use OCP\Share\IShare;
/**
* @psalm-import-type PollsUser from \OCA\Polls\ResponseDefinitions
*/
class UserBase implements JsonSerializable {
/** @var string */
public const TYPE_GENERIC = 'generic';
/** @var string */
public const TYPE = self::TYPE_GENERIC;
/** @var string */
public const TYPE_PUBLIC = 'public';
/** @var string */
public const TYPE_EXTERNAL = 'external';
/** @var string */
public const TYPE_EMPTY = 'empty';
/** @var string */
public const TYPE_GUEST = 'guest';
/** @var string */
public const TYPE_TEAM = Team::TYPE;
/** @var string */
public const TYPE_CONTACT = Contact::TYPE;
/** @var string */
public const TYPE_CONTACTGROUP = ContactGroup::TYPE;
/** @var string */
public const TYPE_EMAIL = Email::TYPE;
/** @var string */
public const TYPE_GHOST = Ghost::TYPE;
/** @var string */
public const TYPE_GROUP = Group::TYPE;
/** @var string */
public const TYPE_USER = User::TYPE;
/** @var string */
public const TYPE_ADMIN = Admin::TYPE;
/** @var string */
public const TYPE_CRON = Cron::TYPE;
/** @var string[] */
protected array $categories = [];
protected string $description = '';
protected string $richObjectType = self::TYPE_USER;
protected string $organisation = '';
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->userSession = Container::queryClass(UserSession::class);
$this->appSettings = Container::queryClass(AppSettings::class);
}
public function getId(): string {
return $this->id;
}
public function getInternalUserId(): ?string {
return null;
}
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 {
return Hash::getUserIdHash($this->id);
}
/**
* Returns the users' type
*
* Returned Type will be one of
* UserBase::TYPE_CONTACT (Share),
* UserBase::TYPE_EMAIL (Share),
* UserBase::TYPE_GHOST (deleted user),
* UserBase::TYPE_USER (NC User),
* UserBase::TYPE_ADMIN (NC user with admin rights),
* UserBase::TYPE_GROUP (NC Group),
* UserBase::TYPE_TEAM (Share),
* UserBase::TYPE_CONTACTGROUP (Share)
* UserBase::TYPE_ANON (Anonymized User)
*
* @return string
**/
public function getType(): string {
return $this->type;
}
public function getIsGuest(): bool {
return !in_array($this->type, [self::TYPE_USER, self::TYPE_ADMIN]);
}
/**
* used for telling internal from guest users
*/
public function getSimpleType(): string {
if (in_array($this->type, [self::TYPE_USER, self::TYPE_ADMIN])) {
return self::TYPE_USER;
}
return self::TYPE_GUEST;
}
public function getLanguageCode(): string {
return $this->languageCode;
}
public function getLanguageCodeIntl(): string {
return str_replace('_', '-', $this->getLanguageCode());
}
public function getLocaleCode(): string {
if (!$this->localeCode) {
return $this->languageCode;
}
return $this->localeCode;
}
public function getLocaleCodeIntl(): string {
return str_replace('_', '-', $this->getLocaleCode());
}
public function getTimeZone(): DateTimeZone {
return new DateTimeZone($this->timeZoneName);
}
public function getTimeZoneName(): string {
if ($this->timeZoneName) {
return $this->timeZoneName;
}
return date_default_timezone_get();
}
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 list<string> */
public function getCategories(): array {
return array_values($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 (Team::isEnabled()) {
$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 (Team::isEnabled()) {
foreach (($result['circles'] ?? []) as $item) {
$items[] = new Team($item['value']['shareWith']);
}
foreach (($result['exact']['circles'] ?? []) as $item) {
$items[] = new Team($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 */
/** @return PollsUser */
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 PollsUser
*/
public function getRichUserArray(): array {
return [
'array' => 'richArray',
'categories' => $this->getCategories(),
'desc' => $this->getDescription(),
'displayName' => $this->getDisplayName(),
'emailAddress' => $this->getEmailAddress(),
'id' => $this->getId(),
'user' => $this->getInternalUserId(),
'isAdmin' => $this->getIsAdmin(),
'isGuest' => $this->getIsGuest(),
'isNoUser' => $this->getIsNoUser(),
'isUnrestrictedOwner' => $this->getIsUnrestrictedPollOwner(),
'languageCode' => $this->getLanguageCode(),
'languageCodeIntl' => $this->getLanguageCodeIntl(),
'localeCode' => $this->getLocaleCode(),
'localeCodeIntl' => $this->getLocaleCodeIntl(),
'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 (null|bool|null|string)[]
*/
/** @return PollsUser */
protected function getSimpleUserArray(): array {
return [
'array' => 'simpleArray',
'categories' => [],
'desc' => '',
'displayName' => $this->getDisplayName(),
'emailAddress' => $this->getSafeEmailAddress(),
'id' => $this->getSafeId(),
'user' => null,
'isAdmin' => false,
'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();
}
// 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();
}
}