@@ -57,515 +57,515 @@ |
||
57 | 57 | use Symfony\Component\EventDispatcher\GenericEvent; |
58 | 58 | |
59 | 59 | class User implements IUser { |
60 | - /** @var IAccountManager */ |
|
61 | - protected $accountManager; |
|
62 | - /** @var string */ |
|
63 | - private $uid; |
|
64 | - |
|
65 | - /** @var string|null */ |
|
66 | - private $displayName; |
|
67 | - |
|
68 | - /** @var UserInterface|null */ |
|
69 | - private $backend; |
|
70 | - /** @var EventDispatcherInterface */ |
|
71 | - private $legacyDispatcher; |
|
72 | - |
|
73 | - /** @var IEventDispatcher */ |
|
74 | - private $dispatcher; |
|
75 | - |
|
76 | - /** @var bool|null */ |
|
77 | - private $enabled; |
|
78 | - |
|
79 | - /** @var Emitter|Manager */ |
|
80 | - private $emitter; |
|
81 | - |
|
82 | - /** @var string */ |
|
83 | - private $home; |
|
84 | - |
|
85 | - /** @var int|null */ |
|
86 | - private $lastLogin; |
|
87 | - |
|
88 | - /** @var \OCP\IConfig */ |
|
89 | - private $config; |
|
90 | - |
|
91 | - /** @var IAvatarManager */ |
|
92 | - private $avatarManager; |
|
93 | - |
|
94 | - /** @var IURLGenerator */ |
|
95 | - private $urlGenerator; |
|
96 | - |
|
97 | - public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
98 | - $this->uid = $uid; |
|
99 | - $this->backend = $backend; |
|
100 | - $this->legacyDispatcher = $dispatcher; |
|
101 | - $this->emitter = $emitter; |
|
102 | - if (is_null($config)) { |
|
103 | - $config = \OC::$server->getConfig(); |
|
104 | - } |
|
105 | - $this->config = $config; |
|
106 | - $this->urlGenerator = $urlGenerator; |
|
107 | - if (is_null($this->urlGenerator)) { |
|
108 | - $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
109 | - } |
|
110 | - // TODO: inject |
|
111 | - $this->dispatcher = \OC::$server->query(IEventDispatcher::class); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * get the user id |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function getUID() { |
|
120 | - return $this->uid; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
125 | - * |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - public function getDisplayName() { |
|
129 | - if ($this->displayName === null) { |
|
130 | - $displayName = ''; |
|
131 | - if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
132 | - // get display name and strip whitespace from the beginning and end of it |
|
133 | - $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
134 | - if (is_string($backendDisplayName)) { |
|
135 | - $displayName = trim($backendDisplayName); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - if (!empty($displayName)) { |
|
140 | - $this->displayName = $displayName; |
|
141 | - } else { |
|
142 | - $this->displayName = $this->uid; |
|
143 | - } |
|
144 | - } |
|
145 | - return $this->displayName; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * set the displayname for the user |
|
150 | - * |
|
151 | - * @param string $displayName |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - public function setDisplayName($displayName) { |
|
155 | - $displayName = trim($displayName); |
|
156 | - $oldDisplayName = $this->getDisplayName(); |
|
157 | - if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) { |
|
158 | - $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
159 | - if ($result) { |
|
160 | - $this->displayName = $displayName; |
|
161 | - $this->triggerChange('displayName', $displayName, $oldDisplayName); |
|
162 | - } |
|
163 | - return $result !== false; |
|
164 | - } |
|
165 | - return false; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @inheritDoc |
|
170 | - */ |
|
171 | - public function setEMailAddress($mailAddress) { |
|
172 | - $this->setSystemEMailAddress($mailAddress); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @inheritDoc |
|
177 | - */ |
|
178 | - public function setSystemEMailAddress(string $mailAddress): void { |
|
179 | - $oldMailAddress = $this->getSystemEMailAddress(); |
|
180 | - |
|
181 | - if ($mailAddress === '') { |
|
182 | - $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
183 | - } else { |
|
184 | - $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
185 | - } |
|
186 | - |
|
187 | - $primaryAddress = $this->getPrimaryEMailAddress(); |
|
188 | - if ($primaryAddress === $mailAddress) { |
|
189 | - // on match no dedicated primary settings is necessary |
|
190 | - $this->setPrimaryEMailAddress(''); |
|
191 | - } |
|
192 | - |
|
193 | - if ($oldMailAddress !== $mailAddress) { |
|
194 | - $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * @inheritDoc |
|
200 | - */ |
|
201 | - public function setPrimaryEMailAddress(string $mailAddress): void { |
|
202 | - if ($mailAddress === '') { |
|
203 | - $this->config->deleteUserValue($this->uid, 'settings', 'primary_email'); |
|
204 | - return; |
|
205 | - } |
|
206 | - |
|
207 | - $this->ensureAccountManager(); |
|
208 | - $account = $this->accountManager->getAccount($this); |
|
209 | - $property = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL) |
|
210 | - ->getPropertyByValue($mailAddress); |
|
211 | - |
|
212 | - if ($property === null || $property->getLocallyVerified() !== IAccountManager::VERIFIED) { |
|
213 | - throw new InvalidArgumentException('Only verified emails can be set as primary'); |
|
214 | - } |
|
215 | - $this->config->setUserValue($this->uid, 'settings', 'primary_email', $mailAddress); |
|
216 | - } |
|
217 | - |
|
218 | - private function ensureAccountManager() { |
|
219 | - if (!$this->accountManager instanceof IAccountManager) { |
|
220 | - $this->accountManager = \OC::$server->get(IAccountManager::class); |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * returns the timestamp of the user's last login or 0 if the user did never |
|
226 | - * login |
|
227 | - * |
|
228 | - * @return int |
|
229 | - */ |
|
230 | - public function getLastLogin() { |
|
231 | - if ($this->lastLogin === null) { |
|
232 | - $this->lastLogin = (int) $this->config->getUserValue($this->uid, 'login', 'lastLogin', 0); |
|
233 | - } |
|
234 | - return (int) $this->lastLogin; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * updates the timestamp of the most recent login of this user |
|
239 | - */ |
|
240 | - public function updateLastLoginTimestamp() { |
|
241 | - $firstTimeLogin = ($this->getLastLogin() === 0); |
|
242 | - $this->lastLogin = time(); |
|
243 | - $this->config->setUserValue( |
|
244 | - $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
245 | - |
|
246 | - return $firstTimeLogin; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Delete the user |
|
251 | - * |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function delete() { |
|
255 | - /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
256 | - $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
257 | - if ($this->emitter) { |
|
258 | - /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
259 | - $this->emitter->emit('\OC\User', 'preDelete', [$this]); |
|
260 | - } |
|
261 | - $this->dispatcher->dispatchTyped(new BeforeUserDeletedEvent($this)); |
|
262 | - $result = $this->backend->deleteUser($this->uid); |
|
263 | - if ($result) { |
|
264 | - |
|
265 | - // FIXME: Feels like an hack - suggestions? |
|
266 | - |
|
267 | - $groupManager = \OC::$server->getGroupManager(); |
|
268 | - // We have to delete the user from all groups |
|
269 | - foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
270 | - $group = $groupManager->get($groupId); |
|
271 | - if ($group) { |
|
272 | - $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this)); |
|
273 | - $group->removeUser($this); |
|
274 | - $this->dispatcher->dispatchTyped(new UserRemovedEvent($group, $this)); |
|
275 | - } |
|
276 | - } |
|
277 | - // Delete the user's keys in preferences |
|
278 | - \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
279 | - |
|
280 | - \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
281 | - \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
282 | - |
|
283 | - /** @var IAvatarManager $avatarManager */ |
|
284 | - $avatarManager = \OC::$server->query(AvatarManager::class); |
|
285 | - $avatarManager->deleteUserAvatar($this->uid); |
|
286 | - |
|
287 | - $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
288 | - $notification->setUser($this->uid); |
|
289 | - \OC::$server->getNotificationManager()->markProcessed($notification); |
|
290 | - |
|
291 | - /** @var AccountManager $accountManager */ |
|
292 | - $accountManager = \OC::$server->query(AccountManager::class); |
|
293 | - $accountManager->deleteUser($this); |
|
294 | - |
|
295 | - /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
296 | - $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
297 | - if ($this->emitter) { |
|
298 | - /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
299 | - $this->emitter->emit('\OC\User', 'postDelete', [$this]); |
|
300 | - } |
|
301 | - $this->dispatcher->dispatchTyped(new UserDeletedEvent($this)); |
|
302 | - } |
|
303 | - return !($result === false); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Set the password of the user |
|
308 | - * |
|
309 | - * @param string $password |
|
310 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - public function setPassword($password, $recoveryPassword = null) { |
|
314 | - $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
315 | - 'password' => $password, |
|
316 | - 'recoveryPassword' => $recoveryPassword, |
|
317 | - ])); |
|
318 | - if ($this->emitter) { |
|
319 | - $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]); |
|
320 | - } |
|
321 | - if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
322 | - $result = $this->backend->setPassword($this->uid, $password); |
|
323 | - |
|
324 | - if ($result !== false) { |
|
325 | - $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
326 | - 'password' => $password, |
|
327 | - 'recoveryPassword' => $recoveryPassword, |
|
328 | - ])); |
|
329 | - if ($this->emitter) { |
|
330 | - $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - return !($result === false); |
|
335 | - } else { |
|
336 | - return false; |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * get the users home folder to mount |
|
342 | - * |
|
343 | - * @return string |
|
344 | - */ |
|
345 | - public function getHome() { |
|
346 | - if (!$this->home) { |
|
347 | - if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
348 | - $this->home = $home; |
|
349 | - } elseif ($this->config) { |
|
350 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
351 | - } else { |
|
352 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
353 | - } |
|
354 | - } |
|
355 | - return $this->home; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Get the name of the backend class the user is connected with |
|
360 | - * |
|
361 | - * @return string |
|
362 | - */ |
|
363 | - public function getBackendClassName() { |
|
364 | - if ($this->backend instanceof IUserBackend) { |
|
365 | - return $this->backend->getBackendName(); |
|
366 | - } |
|
367 | - return get_class($this->backend); |
|
368 | - } |
|
369 | - |
|
370 | - public function getBackend() { |
|
371 | - return $this->backend; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * check if the backend allows the user to change his avatar on Personal page |
|
376 | - * |
|
377 | - * @return bool |
|
378 | - */ |
|
379 | - public function canChangeAvatar() { |
|
380 | - if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
381 | - return $this->backend->canChangeAvatar($this->uid); |
|
382 | - } |
|
383 | - return true; |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * check if the backend supports changing passwords |
|
388 | - * |
|
389 | - * @return bool |
|
390 | - */ |
|
391 | - public function canChangePassword() { |
|
392 | - return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * check if the backend supports changing display names |
|
397 | - * |
|
398 | - * @return bool |
|
399 | - */ |
|
400 | - public function canChangeDisplayName() { |
|
401 | - if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
402 | - return false; |
|
403 | - } |
|
404 | - return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * check if the user is enabled |
|
409 | - * |
|
410 | - * @return bool |
|
411 | - */ |
|
412 | - public function isEnabled() { |
|
413 | - if ($this->enabled === null) { |
|
414 | - $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); |
|
415 | - $this->enabled = $enabled === 'true'; |
|
416 | - } |
|
417 | - return (bool) $this->enabled; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * set the enabled status for the user |
|
422 | - * |
|
423 | - * @param bool $enabled |
|
424 | - */ |
|
425 | - public function setEnabled(bool $enabled = true) { |
|
426 | - $oldStatus = $this->isEnabled(); |
|
427 | - $this->enabled = $enabled; |
|
428 | - if ($oldStatus !== $this->enabled) { |
|
429 | - // TODO: First change the value, then trigger the event as done for all other properties. |
|
430 | - $this->triggerChange('enabled', $enabled, $oldStatus); |
|
431 | - $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * get the users email address |
|
437 | - * |
|
438 | - * @return string|null |
|
439 | - * @since 9.0.0 |
|
440 | - */ |
|
441 | - public function getEMailAddress() { |
|
442 | - return $this->getPrimaryEMailAddress() ?? $this->getSystemEMailAddress(); |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * @inheritDoc |
|
447 | - */ |
|
448 | - public function getSystemEMailAddress(): ?string { |
|
449 | - return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * @inheritDoc |
|
454 | - */ |
|
455 | - public function getPrimaryEMailAddress(): ?string { |
|
456 | - return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null); |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * get the users' quota |
|
461 | - * |
|
462 | - * @return string |
|
463 | - * @since 9.0.0 |
|
464 | - */ |
|
465 | - public function getQuota() { |
|
466 | - // allow apps to modify the user quota by hooking into the event |
|
467 | - $event = new GetQuotaEvent($this); |
|
468 | - $this->dispatcher->dispatchTyped($event); |
|
469 | - $overwriteQuota = $event->getQuota(); |
|
470 | - if ($overwriteQuota) { |
|
471 | - $quota = $overwriteQuota; |
|
472 | - } else { |
|
473 | - $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
474 | - } |
|
475 | - if ($quota === 'default') { |
|
476 | - $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
477 | - |
|
478 | - // if unlimited quota is not allowed => avoid getting 'unlimited' as default_quota fallback value |
|
479 | - // use the first preset instead |
|
480 | - $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1'; |
|
481 | - if (!$allowUnlimitedQuota) { |
|
482 | - $presets = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
483 | - $presets = array_filter(array_map('trim', explode(',', $presets))); |
|
484 | - $quotaPreset = array_values(array_diff($presets, ['default', 'none'])); |
|
485 | - if (count($quotaPreset) > 0) { |
|
486 | - $quota = $this->config->getAppValue('files', 'default_quota', $quotaPreset[0]); |
|
487 | - } |
|
488 | - } |
|
489 | - } |
|
490 | - return $quota; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * set the users' quota |
|
495 | - * |
|
496 | - * @param string $quota |
|
497 | - * @return void |
|
498 | - * @since 9.0.0 |
|
499 | - */ |
|
500 | - public function setQuota($quota) { |
|
501 | - $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
502 | - if ($quota !== 'none' and $quota !== 'default') { |
|
503 | - $quota = OC_Helper::computerFileSize($quota); |
|
504 | - $quota = OC_Helper::humanFileSize($quota); |
|
505 | - } |
|
506 | - if ($quota !== $oldQuota) { |
|
507 | - $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
508 | - $this->triggerChange('quota', $quota, $oldQuota); |
|
509 | - } |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * get the avatar image if it exists |
|
514 | - * |
|
515 | - * @param int $size |
|
516 | - * @return IImage|null |
|
517 | - * @since 9.0.0 |
|
518 | - */ |
|
519 | - public function getAvatarImage($size) { |
|
520 | - // delay the initialization |
|
521 | - if (is_null($this->avatarManager)) { |
|
522 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
523 | - } |
|
524 | - |
|
525 | - $avatar = $this->avatarManager->getAvatar($this->uid); |
|
526 | - $image = $avatar->get(-1); |
|
527 | - if ($image) { |
|
528 | - return $image; |
|
529 | - } |
|
530 | - |
|
531 | - return null; |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * get the federation cloud id |
|
536 | - * |
|
537 | - * @return string |
|
538 | - * @since 9.0.0 |
|
539 | - */ |
|
540 | - public function getCloudId() { |
|
541 | - $uid = $this->getUID(); |
|
542 | - $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
543 | - $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
|
544 | - return $uid . '@' . $server; |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * @param string $url |
|
549 | - * @return string |
|
550 | - */ |
|
551 | - private function removeProtocolFromUrl($url) { |
|
552 | - if (strpos($url, 'https://') === 0) { |
|
553 | - return substr($url, strlen('https://')); |
|
554 | - } elseif (strpos($url, 'http://') === 0) { |
|
555 | - return substr($url, strlen('http://')); |
|
556 | - } |
|
557 | - |
|
558 | - return $url; |
|
559 | - } |
|
560 | - |
|
561 | - public function triggerChange($feature, $value = null, $oldValue = null) { |
|
562 | - $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
563 | - 'feature' => $feature, |
|
564 | - 'value' => $value, |
|
565 | - 'oldValue' => $oldValue, |
|
566 | - ])); |
|
567 | - if ($this->emitter) { |
|
568 | - $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); |
|
569 | - } |
|
570 | - } |
|
60 | + /** @var IAccountManager */ |
|
61 | + protected $accountManager; |
|
62 | + /** @var string */ |
|
63 | + private $uid; |
|
64 | + |
|
65 | + /** @var string|null */ |
|
66 | + private $displayName; |
|
67 | + |
|
68 | + /** @var UserInterface|null */ |
|
69 | + private $backend; |
|
70 | + /** @var EventDispatcherInterface */ |
|
71 | + private $legacyDispatcher; |
|
72 | + |
|
73 | + /** @var IEventDispatcher */ |
|
74 | + private $dispatcher; |
|
75 | + |
|
76 | + /** @var bool|null */ |
|
77 | + private $enabled; |
|
78 | + |
|
79 | + /** @var Emitter|Manager */ |
|
80 | + private $emitter; |
|
81 | + |
|
82 | + /** @var string */ |
|
83 | + private $home; |
|
84 | + |
|
85 | + /** @var int|null */ |
|
86 | + private $lastLogin; |
|
87 | + |
|
88 | + /** @var \OCP\IConfig */ |
|
89 | + private $config; |
|
90 | + |
|
91 | + /** @var IAvatarManager */ |
|
92 | + private $avatarManager; |
|
93 | + |
|
94 | + /** @var IURLGenerator */ |
|
95 | + private $urlGenerator; |
|
96 | + |
|
97 | + public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
98 | + $this->uid = $uid; |
|
99 | + $this->backend = $backend; |
|
100 | + $this->legacyDispatcher = $dispatcher; |
|
101 | + $this->emitter = $emitter; |
|
102 | + if (is_null($config)) { |
|
103 | + $config = \OC::$server->getConfig(); |
|
104 | + } |
|
105 | + $this->config = $config; |
|
106 | + $this->urlGenerator = $urlGenerator; |
|
107 | + if (is_null($this->urlGenerator)) { |
|
108 | + $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
109 | + } |
|
110 | + // TODO: inject |
|
111 | + $this->dispatcher = \OC::$server->query(IEventDispatcher::class); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * get the user id |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function getUID() { |
|
120 | + return $this->uid; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
125 | + * |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + public function getDisplayName() { |
|
129 | + if ($this->displayName === null) { |
|
130 | + $displayName = ''; |
|
131 | + if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
132 | + // get display name and strip whitespace from the beginning and end of it |
|
133 | + $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
134 | + if (is_string($backendDisplayName)) { |
|
135 | + $displayName = trim($backendDisplayName); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + if (!empty($displayName)) { |
|
140 | + $this->displayName = $displayName; |
|
141 | + } else { |
|
142 | + $this->displayName = $this->uid; |
|
143 | + } |
|
144 | + } |
|
145 | + return $this->displayName; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * set the displayname for the user |
|
150 | + * |
|
151 | + * @param string $displayName |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + public function setDisplayName($displayName) { |
|
155 | + $displayName = trim($displayName); |
|
156 | + $oldDisplayName = $this->getDisplayName(); |
|
157 | + if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) { |
|
158 | + $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
159 | + if ($result) { |
|
160 | + $this->displayName = $displayName; |
|
161 | + $this->triggerChange('displayName', $displayName, $oldDisplayName); |
|
162 | + } |
|
163 | + return $result !== false; |
|
164 | + } |
|
165 | + return false; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @inheritDoc |
|
170 | + */ |
|
171 | + public function setEMailAddress($mailAddress) { |
|
172 | + $this->setSystemEMailAddress($mailAddress); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @inheritDoc |
|
177 | + */ |
|
178 | + public function setSystemEMailAddress(string $mailAddress): void { |
|
179 | + $oldMailAddress = $this->getSystemEMailAddress(); |
|
180 | + |
|
181 | + if ($mailAddress === '') { |
|
182 | + $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
183 | + } else { |
|
184 | + $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
185 | + } |
|
186 | + |
|
187 | + $primaryAddress = $this->getPrimaryEMailAddress(); |
|
188 | + if ($primaryAddress === $mailAddress) { |
|
189 | + // on match no dedicated primary settings is necessary |
|
190 | + $this->setPrimaryEMailAddress(''); |
|
191 | + } |
|
192 | + |
|
193 | + if ($oldMailAddress !== $mailAddress) { |
|
194 | + $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * @inheritDoc |
|
200 | + */ |
|
201 | + public function setPrimaryEMailAddress(string $mailAddress): void { |
|
202 | + if ($mailAddress === '') { |
|
203 | + $this->config->deleteUserValue($this->uid, 'settings', 'primary_email'); |
|
204 | + return; |
|
205 | + } |
|
206 | + |
|
207 | + $this->ensureAccountManager(); |
|
208 | + $account = $this->accountManager->getAccount($this); |
|
209 | + $property = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL) |
|
210 | + ->getPropertyByValue($mailAddress); |
|
211 | + |
|
212 | + if ($property === null || $property->getLocallyVerified() !== IAccountManager::VERIFIED) { |
|
213 | + throw new InvalidArgumentException('Only verified emails can be set as primary'); |
|
214 | + } |
|
215 | + $this->config->setUserValue($this->uid, 'settings', 'primary_email', $mailAddress); |
|
216 | + } |
|
217 | + |
|
218 | + private function ensureAccountManager() { |
|
219 | + if (!$this->accountManager instanceof IAccountManager) { |
|
220 | + $this->accountManager = \OC::$server->get(IAccountManager::class); |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * returns the timestamp of the user's last login or 0 if the user did never |
|
226 | + * login |
|
227 | + * |
|
228 | + * @return int |
|
229 | + */ |
|
230 | + public function getLastLogin() { |
|
231 | + if ($this->lastLogin === null) { |
|
232 | + $this->lastLogin = (int) $this->config->getUserValue($this->uid, 'login', 'lastLogin', 0); |
|
233 | + } |
|
234 | + return (int) $this->lastLogin; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * updates the timestamp of the most recent login of this user |
|
239 | + */ |
|
240 | + public function updateLastLoginTimestamp() { |
|
241 | + $firstTimeLogin = ($this->getLastLogin() === 0); |
|
242 | + $this->lastLogin = time(); |
|
243 | + $this->config->setUserValue( |
|
244 | + $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
245 | + |
|
246 | + return $firstTimeLogin; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Delete the user |
|
251 | + * |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function delete() { |
|
255 | + /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
256 | + $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
257 | + if ($this->emitter) { |
|
258 | + /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
259 | + $this->emitter->emit('\OC\User', 'preDelete', [$this]); |
|
260 | + } |
|
261 | + $this->dispatcher->dispatchTyped(new BeforeUserDeletedEvent($this)); |
|
262 | + $result = $this->backend->deleteUser($this->uid); |
|
263 | + if ($result) { |
|
264 | + |
|
265 | + // FIXME: Feels like an hack - suggestions? |
|
266 | + |
|
267 | + $groupManager = \OC::$server->getGroupManager(); |
|
268 | + // We have to delete the user from all groups |
|
269 | + foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
270 | + $group = $groupManager->get($groupId); |
|
271 | + if ($group) { |
|
272 | + $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this)); |
|
273 | + $group->removeUser($this); |
|
274 | + $this->dispatcher->dispatchTyped(new UserRemovedEvent($group, $this)); |
|
275 | + } |
|
276 | + } |
|
277 | + // Delete the user's keys in preferences |
|
278 | + \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
279 | + |
|
280 | + \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
281 | + \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
282 | + |
|
283 | + /** @var IAvatarManager $avatarManager */ |
|
284 | + $avatarManager = \OC::$server->query(AvatarManager::class); |
|
285 | + $avatarManager->deleteUserAvatar($this->uid); |
|
286 | + |
|
287 | + $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
288 | + $notification->setUser($this->uid); |
|
289 | + \OC::$server->getNotificationManager()->markProcessed($notification); |
|
290 | + |
|
291 | + /** @var AccountManager $accountManager */ |
|
292 | + $accountManager = \OC::$server->query(AccountManager::class); |
|
293 | + $accountManager->deleteUser($this); |
|
294 | + |
|
295 | + /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
296 | + $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
297 | + if ($this->emitter) { |
|
298 | + /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
299 | + $this->emitter->emit('\OC\User', 'postDelete', [$this]); |
|
300 | + } |
|
301 | + $this->dispatcher->dispatchTyped(new UserDeletedEvent($this)); |
|
302 | + } |
|
303 | + return !($result === false); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Set the password of the user |
|
308 | + * |
|
309 | + * @param string $password |
|
310 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + public function setPassword($password, $recoveryPassword = null) { |
|
314 | + $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
315 | + 'password' => $password, |
|
316 | + 'recoveryPassword' => $recoveryPassword, |
|
317 | + ])); |
|
318 | + if ($this->emitter) { |
|
319 | + $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]); |
|
320 | + } |
|
321 | + if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
322 | + $result = $this->backend->setPassword($this->uid, $password); |
|
323 | + |
|
324 | + if ($result !== false) { |
|
325 | + $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
326 | + 'password' => $password, |
|
327 | + 'recoveryPassword' => $recoveryPassword, |
|
328 | + ])); |
|
329 | + if ($this->emitter) { |
|
330 | + $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + return !($result === false); |
|
335 | + } else { |
|
336 | + return false; |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * get the users home folder to mount |
|
342 | + * |
|
343 | + * @return string |
|
344 | + */ |
|
345 | + public function getHome() { |
|
346 | + if (!$this->home) { |
|
347 | + if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
348 | + $this->home = $home; |
|
349 | + } elseif ($this->config) { |
|
350 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
351 | + } else { |
|
352 | + $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
353 | + } |
|
354 | + } |
|
355 | + return $this->home; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Get the name of the backend class the user is connected with |
|
360 | + * |
|
361 | + * @return string |
|
362 | + */ |
|
363 | + public function getBackendClassName() { |
|
364 | + if ($this->backend instanceof IUserBackend) { |
|
365 | + return $this->backend->getBackendName(); |
|
366 | + } |
|
367 | + return get_class($this->backend); |
|
368 | + } |
|
369 | + |
|
370 | + public function getBackend() { |
|
371 | + return $this->backend; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * check if the backend allows the user to change his avatar on Personal page |
|
376 | + * |
|
377 | + * @return bool |
|
378 | + */ |
|
379 | + public function canChangeAvatar() { |
|
380 | + if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
381 | + return $this->backend->canChangeAvatar($this->uid); |
|
382 | + } |
|
383 | + return true; |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * check if the backend supports changing passwords |
|
388 | + * |
|
389 | + * @return bool |
|
390 | + */ |
|
391 | + public function canChangePassword() { |
|
392 | + return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * check if the backend supports changing display names |
|
397 | + * |
|
398 | + * @return bool |
|
399 | + */ |
|
400 | + public function canChangeDisplayName() { |
|
401 | + if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
402 | + return false; |
|
403 | + } |
|
404 | + return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * check if the user is enabled |
|
409 | + * |
|
410 | + * @return bool |
|
411 | + */ |
|
412 | + public function isEnabled() { |
|
413 | + if ($this->enabled === null) { |
|
414 | + $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); |
|
415 | + $this->enabled = $enabled === 'true'; |
|
416 | + } |
|
417 | + return (bool) $this->enabled; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * set the enabled status for the user |
|
422 | + * |
|
423 | + * @param bool $enabled |
|
424 | + */ |
|
425 | + public function setEnabled(bool $enabled = true) { |
|
426 | + $oldStatus = $this->isEnabled(); |
|
427 | + $this->enabled = $enabled; |
|
428 | + if ($oldStatus !== $this->enabled) { |
|
429 | + // TODO: First change the value, then trigger the event as done for all other properties. |
|
430 | + $this->triggerChange('enabled', $enabled, $oldStatus); |
|
431 | + $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * get the users email address |
|
437 | + * |
|
438 | + * @return string|null |
|
439 | + * @since 9.0.0 |
|
440 | + */ |
|
441 | + public function getEMailAddress() { |
|
442 | + return $this->getPrimaryEMailAddress() ?? $this->getSystemEMailAddress(); |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * @inheritDoc |
|
447 | + */ |
|
448 | + public function getSystemEMailAddress(): ?string { |
|
449 | + return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * @inheritDoc |
|
454 | + */ |
|
455 | + public function getPrimaryEMailAddress(): ?string { |
|
456 | + return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null); |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * get the users' quota |
|
461 | + * |
|
462 | + * @return string |
|
463 | + * @since 9.0.0 |
|
464 | + */ |
|
465 | + public function getQuota() { |
|
466 | + // allow apps to modify the user quota by hooking into the event |
|
467 | + $event = new GetQuotaEvent($this); |
|
468 | + $this->dispatcher->dispatchTyped($event); |
|
469 | + $overwriteQuota = $event->getQuota(); |
|
470 | + if ($overwriteQuota) { |
|
471 | + $quota = $overwriteQuota; |
|
472 | + } else { |
|
473 | + $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
474 | + } |
|
475 | + if ($quota === 'default') { |
|
476 | + $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
477 | + |
|
478 | + // if unlimited quota is not allowed => avoid getting 'unlimited' as default_quota fallback value |
|
479 | + // use the first preset instead |
|
480 | + $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1'; |
|
481 | + if (!$allowUnlimitedQuota) { |
|
482 | + $presets = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
483 | + $presets = array_filter(array_map('trim', explode(',', $presets))); |
|
484 | + $quotaPreset = array_values(array_diff($presets, ['default', 'none'])); |
|
485 | + if (count($quotaPreset) > 0) { |
|
486 | + $quota = $this->config->getAppValue('files', 'default_quota', $quotaPreset[0]); |
|
487 | + } |
|
488 | + } |
|
489 | + } |
|
490 | + return $quota; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * set the users' quota |
|
495 | + * |
|
496 | + * @param string $quota |
|
497 | + * @return void |
|
498 | + * @since 9.0.0 |
|
499 | + */ |
|
500 | + public function setQuota($quota) { |
|
501 | + $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
502 | + if ($quota !== 'none' and $quota !== 'default') { |
|
503 | + $quota = OC_Helper::computerFileSize($quota); |
|
504 | + $quota = OC_Helper::humanFileSize($quota); |
|
505 | + } |
|
506 | + if ($quota !== $oldQuota) { |
|
507 | + $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
508 | + $this->triggerChange('quota', $quota, $oldQuota); |
|
509 | + } |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * get the avatar image if it exists |
|
514 | + * |
|
515 | + * @param int $size |
|
516 | + * @return IImage|null |
|
517 | + * @since 9.0.0 |
|
518 | + */ |
|
519 | + public function getAvatarImage($size) { |
|
520 | + // delay the initialization |
|
521 | + if (is_null($this->avatarManager)) { |
|
522 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
523 | + } |
|
524 | + |
|
525 | + $avatar = $this->avatarManager->getAvatar($this->uid); |
|
526 | + $image = $avatar->get(-1); |
|
527 | + if ($image) { |
|
528 | + return $image; |
|
529 | + } |
|
530 | + |
|
531 | + return null; |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * get the federation cloud id |
|
536 | + * |
|
537 | + * @return string |
|
538 | + * @since 9.0.0 |
|
539 | + */ |
|
540 | + public function getCloudId() { |
|
541 | + $uid = $this->getUID(); |
|
542 | + $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
543 | + $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
|
544 | + return $uid . '@' . $server; |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * @param string $url |
|
549 | + * @return string |
|
550 | + */ |
|
551 | + private function removeProtocolFromUrl($url) { |
|
552 | + if (strpos($url, 'https://') === 0) { |
|
553 | + return substr($url, strlen('https://')); |
|
554 | + } elseif (strpos($url, 'http://') === 0) { |
|
555 | + return substr($url, strlen('http://')); |
|
556 | + } |
|
557 | + |
|
558 | + return $url; |
|
559 | + } |
|
560 | + |
|
561 | + public function triggerChange($feature, $value = null, $oldValue = null) { |
|
562 | + $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
563 | + 'feature' => $feature, |
|
564 | + 'value' => $value, |
|
565 | + 'oldValue' => $oldValue, |
|
566 | + ])); |
|
567 | + if ($this->emitter) { |
|
568 | + $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); |
|
569 | + } |
|
570 | + } |
|
571 | 571 | } |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | */ |
254 | 254 | public function delete() { |
255 | 255 | /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
256 | - $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
256 | + $this->legacyDispatcher->dispatch(IUser::class.'::preDelete', new GenericEvent($this)); |
|
257 | 257 | if ($this->emitter) { |
258 | 258 | /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
259 | 259 | $this->emitter->emit('\OC\User', 'preDelete', [$this]); |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | $accountManager->deleteUser($this); |
294 | 294 | |
295 | 295 | /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
296 | - $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
296 | + $this->legacyDispatcher->dispatch(IUser::class.'::postDelete', new GenericEvent($this)); |
|
297 | 297 | if ($this->emitter) { |
298 | 298 | /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
299 | 299 | $this->emitter->emit('\OC\User', 'postDelete', [$this]); |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | * @return bool |
312 | 312 | */ |
313 | 313 | public function setPassword($password, $recoveryPassword = null) { |
314 | - $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
314 | + $this->legacyDispatcher->dispatch(IUser::class.'::preSetPassword', new GenericEvent($this, [ |
|
315 | 315 | 'password' => $password, |
316 | 316 | 'recoveryPassword' => $recoveryPassword, |
317 | 317 | ])); |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | $result = $this->backend->setPassword($this->uid, $password); |
323 | 323 | |
324 | 324 | if ($result !== false) { |
325 | - $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
325 | + $this->legacyDispatcher->dispatch(IUser::class.'::postSetPassword', new GenericEvent($this, [ |
|
326 | 326 | 'password' => $password, |
327 | 327 | 'recoveryPassword' => $recoveryPassword, |
328 | 328 | ])); |
@@ -347,9 +347,9 @@ discard block |
||
347 | 347 | if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
348 | 348 | $this->home = $home; |
349 | 349 | } elseif ($this->config) { |
350 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
350 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/'.$this->uid; |
|
351 | 351 | } else { |
352 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
352 | + $this->home = \OC::$SERVERROOT.'/data/'.$this->uid; |
|
353 | 353 | } |
354 | 354 | } |
355 | 355 | return $this->home; |
@@ -541,7 +541,7 @@ discard block |
||
541 | 541 | $uid = $this->getUID(); |
542 | 542 | $server = $this->urlGenerator->getAbsoluteURL('/'); |
543 | 543 | $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
544 | - return $uid . '@' . $server; |
|
544 | + return $uid.'@'.$server; |
|
545 | 545 | } |
546 | 546 | |
547 | 547 | /** |
@@ -559,7 +559,7 @@ discard block |
||
559 | 559 | } |
560 | 560 | |
561 | 561 | public function triggerChange($feature, $value = null, $oldValue = null) { |
562 | - $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
562 | + $this->legacyDispatcher->dispatch(IUser::class.'::changeUser', new GenericEvent($this, [ |
|
563 | 563 | 'feature' => $feature, |
564 | 564 | 'value' => $value, |
565 | 565 | 'oldValue' => $oldValue, |
@@ -50,237 +50,237 @@ |
||
50 | 50 | |
51 | 51 | class ChangePasswordController extends Controller { |
52 | 52 | |
53 | - /** @var string */ |
|
54 | - private $userId; |
|
53 | + /** @var string */ |
|
54 | + private $userId; |
|
55 | 55 | |
56 | - /** @var IUserManager */ |
|
57 | - private $userManager; |
|
56 | + /** @var IUserManager */ |
|
57 | + private $userManager; |
|
58 | 58 | |
59 | - /** @var IL10N */ |
|
60 | - private $l; |
|
59 | + /** @var IL10N */ |
|
60 | + private $l; |
|
61 | 61 | |
62 | - /** @var GroupManager */ |
|
63 | - private $groupManager; |
|
62 | + /** @var GroupManager */ |
|
63 | + private $groupManager; |
|
64 | 64 | |
65 | - /** @var Session */ |
|
66 | - private $userSession; |
|
65 | + /** @var Session */ |
|
66 | + private $userSession; |
|
67 | 67 | |
68 | - /** @var IAppManager */ |
|
69 | - private $appManager; |
|
68 | + /** @var IAppManager */ |
|
69 | + private $appManager; |
|
70 | 70 | |
71 | - public function __construct(string $appName, |
|
72 | - IRequest $request, |
|
73 | - string $userId, |
|
74 | - IUserManager $userManager, |
|
75 | - IUserSession $userSession, |
|
76 | - IGroupManager $groupManager, |
|
77 | - IAppManager $appManager, |
|
78 | - IL10N $l) { |
|
79 | - parent::__construct($appName, $request); |
|
71 | + public function __construct(string $appName, |
|
72 | + IRequest $request, |
|
73 | + string $userId, |
|
74 | + IUserManager $userManager, |
|
75 | + IUserSession $userSession, |
|
76 | + IGroupManager $groupManager, |
|
77 | + IAppManager $appManager, |
|
78 | + IL10N $l) { |
|
79 | + parent::__construct($appName, $request); |
|
80 | 80 | |
81 | - $this->userId = $userId; |
|
82 | - $this->userManager = $userManager; |
|
83 | - $this->userSession = $userSession; |
|
84 | - $this->groupManager = $groupManager; |
|
85 | - $this->appManager = $appManager; |
|
86 | - $this->l = $l; |
|
87 | - } |
|
81 | + $this->userId = $userId; |
|
82 | + $this->userManager = $userManager; |
|
83 | + $this->userSession = $userSession; |
|
84 | + $this->groupManager = $groupManager; |
|
85 | + $this->appManager = $appManager; |
|
86 | + $this->l = $l; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @NoAdminRequired |
|
91 | - * @NoSubAdminRequired |
|
92 | - * @BruteForceProtection(action=changePersonalPassword) |
|
93 | - */ |
|
94 | - public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse { |
|
95 | - $loginName = $this->userSession->getLoginName(); |
|
96 | - /** @var IUser $user */ |
|
97 | - $user = $this->userManager->checkPassword($loginName, $oldpassword); |
|
98 | - if ($user === false) { |
|
99 | - $response = new JSONResponse([ |
|
100 | - 'status' => 'error', |
|
101 | - 'data' => [ |
|
102 | - 'message' => $this->l->t('Wrong password'), |
|
103 | - ], |
|
104 | - ]); |
|
105 | - $response->throttle(); |
|
106 | - return $response; |
|
107 | - } |
|
89 | + /** |
|
90 | + * @NoAdminRequired |
|
91 | + * @NoSubAdminRequired |
|
92 | + * @BruteForceProtection(action=changePersonalPassword) |
|
93 | + */ |
|
94 | + public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse { |
|
95 | + $loginName = $this->userSession->getLoginName(); |
|
96 | + /** @var IUser $user */ |
|
97 | + $user = $this->userManager->checkPassword($loginName, $oldpassword); |
|
98 | + if ($user === false) { |
|
99 | + $response = new JSONResponse([ |
|
100 | + 'status' => 'error', |
|
101 | + 'data' => [ |
|
102 | + 'message' => $this->l->t('Wrong password'), |
|
103 | + ], |
|
104 | + ]); |
|
105 | + $response->throttle(); |
|
106 | + return $response; |
|
107 | + } |
|
108 | 108 | |
109 | - try { |
|
110 | - if ($newpassword === null || $user->setPassword($newpassword) === false) { |
|
111 | - return new JSONResponse([ |
|
112 | - 'status' => 'error', |
|
113 | - 'data' => [ |
|
114 | - 'message' => $this->l->t('Unable to change personal password'), |
|
115 | - ], |
|
116 | - ]); |
|
117 | - } |
|
118 | - // password policy app throws exception |
|
119 | - } catch (HintException $e) { |
|
120 | - return new JSONResponse([ |
|
121 | - 'status' => 'error', |
|
122 | - 'data' => [ |
|
123 | - 'message' => $e->getHint(), |
|
124 | - ], |
|
125 | - ]); |
|
126 | - } |
|
109 | + try { |
|
110 | + if ($newpassword === null || $user->setPassword($newpassword) === false) { |
|
111 | + return new JSONResponse([ |
|
112 | + 'status' => 'error', |
|
113 | + 'data' => [ |
|
114 | + 'message' => $this->l->t('Unable to change personal password'), |
|
115 | + ], |
|
116 | + ]); |
|
117 | + } |
|
118 | + // password policy app throws exception |
|
119 | + } catch (HintException $e) { |
|
120 | + return new JSONResponse([ |
|
121 | + 'status' => 'error', |
|
122 | + 'data' => [ |
|
123 | + 'message' => $e->getHint(), |
|
124 | + ], |
|
125 | + ]); |
|
126 | + } |
|
127 | 127 | |
128 | - $this->userSession->updateSessionTokenPassword($newpassword); |
|
128 | + $this->userSession->updateSessionTokenPassword($newpassword); |
|
129 | 129 | |
130 | - return new JSONResponse([ |
|
131 | - 'status' => 'success', |
|
132 | - 'data' => [ |
|
133 | - 'message' => $this->l->t('Saved'), |
|
134 | - ], |
|
135 | - ]); |
|
136 | - } |
|
130 | + return new JSONResponse([ |
|
131 | + 'status' => 'success', |
|
132 | + 'data' => [ |
|
133 | + 'message' => $this->l->t('Saved'), |
|
134 | + ], |
|
135 | + ]); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @NoAdminRequired |
|
140 | - * @PasswordConfirmationRequired |
|
141 | - */ |
|
142 | - public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse { |
|
143 | - if ($username === null) { |
|
144 | - return new JSONResponse([ |
|
145 | - 'status' => 'error', |
|
146 | - 'data' => [ |
|
147 | - 'message' => $this->l->t('No user supplied'), |
|
148 | - ], |
|
149 | - ]); |
|
150 | - } |
|
138 | + /** |
|
139 | + * @NoAdminRequired |
|
140 | + * @PasswordConfirmationRequired |
|
141 | + */ |
|
142 | + public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse { |
|
143 | + if ($username === null) { |
|
144 | + return new JSONResponse([ |
|
145 | + 'status' => 'error', |
|
146 | + 'data' => [ |
|
147 | + 'message' => $this->l->t('No user supplied'), |
|
148 | + ], |
|
149 | + ]); |
|
150 | + } |
|
151 | 151 | |
152 | - if ($password === null) { |
|
153 | - return new JSONResponse([ |
|
154 | - 'status' => 'error', |
|
155 | - 'data' => [ |
|
156 | - 'message' => $this->l->t('Unable to change password'), |
|
157 | - ], |
|
158 | - ]); |
|
159 | - } |
|
152 | + if ($password === null) { |
|
153 | + return new JSONResponse([ |
|
154 | + 'status' => 'error', |
|
155 | + 'data' => [ |
|
156 | + 'message' => $this->l->t('Unable to change password'), |
|
157 | + ], |
|
158 | + ]); |
|
159 | + } |
|
160 | 160 | |
161 | - $currentUser = $this->userSession->getUser(); |
|
162 | - $targetUser = $this->userManager->get($username); |
|
163 | - if ($currentUser === null || $targetUser === null || |
|
164 | - !($this->groupManager->isAdmin($this->userId) || |
|
165 | - $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) |
|
166 | - ) { |
|
167 | - return new JSONResponse([ |
|
168 | - 'status' => 'error', |
|
169 | - 'data' => [ |
|
170 | - 'message' => $this->l->t('Authentication error'), |
|
171 | - ], |
|
172 | - ]); |
|
173 | - } |
|
161 | + $currentUser = $this->userSession->getUser(); |
|
162 | + $targetUser = $this->userManager->get($username); |
|
163 | + if ($currentUser === null || $targetUser === null || |
|
164 | + !($this->groupManager->isAdmin($this->userId) || |
|
165 | + $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) |
|
166 | + ) { |
|
167 | + return new JSONResponse([ |
|
168 | + 'status' => 'error', |
|
169 | + 'data' => [ |
|
170 | + 'message' => $this->l->t('Authentication error'), |
|
171 | + ], |
|
172 | + ]); |
|
173 | + } |
|
174 | 174 | |
175 | - if ($this->appManager->isEnabledForUser('encryption')) { |
|
176 | - //handle the recovery case |
|
177 | - $crypt = new \OCA\Encryption\Crypto\Crypt( |
|
178 | - \OC::$server->getLogger(), |
|
179 | - \OC::$server->getUserSession(), |
|
180 | - \OC::$server->getConfig(), |
|
181 | - \OC::$server->getL10N('encryption')); |
|
182 | - $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
|
183 | - $util = new \OCA\Encryption\Util( |
|
184 | - new \OC\Files\View(), |
|
185 | - $crypt, |
|
186 | - \OC::$server->getLogger(), |
|
187 | - \OC::$server->getUserSession(), |
|
188 | - \OC::$server->getConfig(), |
|
189 | - \OC::$server->getUserManager()); |
|
190 | - $keyManager = new \OCA\Encryption\KeyManager( |
|
191 | - $keyStorage, |
|
192 | - $crypt, |
|
193 | - \OC::$server->getConfig(), |
|
194 | - \OC::$server->getUserSession(), |
|
195 | - new \OCA\Encryption\Session(\OC::$server->getSession()), |
|
196 | - \OC::$server->getLogger(), |
|
197 | - $util, |
|
198 | - \OC::$server->getLockingProvider() |
|
199 | - ); |
|
200 | - $recovery = new \OCA\Encryption\Recovery( |
|
201 | - \OC::$server->getUserSession(), |
|
202 | - $crypt, |
|
203 | - $keyManager, |
|
204 | - \OC::$server->getConfig(), |
|
205 | - \OC::$server->getEncryptionFilesHelper(), |
|
206 | - new \OC\Files\View()); |
|
207 | - $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
|
175 | + if ($this->appManager->isEnabledForUser('encryption')) { |
|
176 | + //handle the recovery case |
|
177 | + $crypt = new \OCA\Encryption\Crypto\Crypt( |
|
178 | + \OC::$server->getLogger(), |
|
179 | + \OC::$server->getUserSession(), |
|
180 | + \OC::$server->getConfig(), |
|
181 | + \OC::$server->getL10N('encryption')); |
|
182 | + $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
|
183 | + $util = new \OCA\Encryption\Util( |
|
184 | + new \OC\Files\View(), |
|
185 | + $crypt, |
|
186 | + \OC::$server->getLogger(), |
|
187 | + \OC::$server->getUserSession(), |
|
188 | + \OC::$server->getConfig(), |
|
189 | + \OC::$server->getUserManager()); |
|
190 | + $keyManager = new \OCA\Encryption\KeyManager( |
|
191 | + $keyStorage, |
|
192 | + $crypt, |
|
193 | + \OC::$server->getConfig(), |
|
194 | + \OC::$server->getUserSession(), |
|
195 | + new \OCA\Encryption\Session(\OC::$server->getSession()), |
|
196 | + \OC::$server->getLogger(), |
|
197 | + $util, |
|
198 | + \OC::$server->getLockingProvider() |
|
199 | + ); |
|
200 | + $recovery = new \OCA\Encryption\Recovery( |
|
201 | + \OC::$server->getUserSession(), |
|
202 | + $crypt, |
|
203 | + $keyManager, |
|
204 | + \OC::$server->getConfig(), |
|
205 | + \OC::$server->getEncryptionFilesHelper(), |
|
206 | + new \OC\Files\View()); |
|
207 | + $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
|
208 | 208 | |
209 | - $validRecoveryPassword = false; |
|
210 | - $recoveryEnabledForUser = false; |
|
211 | - if ($recoveryAdminEnabled) { |
|
212 | - $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
|
213 | - $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
|
214 | - } |
|
209 | + $validRecoveryPassword = false; |
|
210 | + $recoveryEnabledForUser = false; |
|
211 | + if ($recoveryAdminEnabled) { |
|
212 | + $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
|
213 | + $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
|
214 | + } |
|
215 | 215 | |
216 | - if ($recoveryEnabledForUser && $recoveryPassword === '') { |
|
217 | - return new JSONResponse([ |
|
218 | - 'status' => 'error', |
|
219 | - 'data' => [ |
|
220 | - 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), |
|
221 | - ] |
|
222 | - ]); |
|
223 | - } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
224 | - return new JSONResponse([ |
|
225 | - 'status' => 'error', |
|
226 | - 'data' => [ |
|
227 | - 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), |
|
228 | - ] |
|
229 | - ]); |
|
230 | - } else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
|
231 | - try { |
|
232 | - $result = $targetUser->setPassword($password, $recoveryPassword); |
|
233 | - // password policy app throws exception |
|
234 | - } catch (HintException $e) { |
|
235 | - return new JSONResponse([ |
|
236 | - 'status' => 'error', |
|
237 | - 'data' => [ |
|
238 | - 'message' => $e->getHint(), |
|
239 | - ], |
|
240 | - ]); |
|
241 | - } |
|
242 | - if (!$result && $recoveryEnabledForUser) { |
|
243 | - return new JSONResponse([ |
|
244 | - 'status' => 'error', |
|
245 | - 'data' => [ |
|
246 | - 'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was updated.'), |
|
247 | - ] |
|
248 | - ]); |
|
249 | - } elseif (!$result && !$recoveryEnabledForUser) { |
|
250 | - return new JSONResponse([ |
|
251 | - 'status' => 'error', |
|
252 | - 'data' => [ |
|
253 | - 'message' => $this->l->t('Unable to change password'), |
|
254 | - ] |
|
255 | - ]); |
|
256 | - } |
|
257 | - } |
|
258 | - } else { |
|
259 | - try { |
|
260 | - if ($targetUser->setPassword($password) === false) { |
|
261 | - return new JSONResponse([ |
|
262 | - 'status' => 'error', |
|
263 | - 'data' => [ |
|
264 | - 'message' => $this->l->t('Unable to change password'), |
|
265 | - ], |
|
266 | - ]); |
|
267 | - } |
|
268 | - // password policy app throws exception |
|
269 | - } catch (HintException $e) { |
|
270 | - return new JSONResponse([ |
|
271 | - 'status' => 'error', |
|
272 | - 'data' => [ |
|
273 | - 'message' => $e->getHint(), |
|
274 | - ], |
|
275 | - ]); |
|
276 | - } |
|
277 | - } |
|
216 | + if ($recoveryEnabledForUser && $recoveryPassword === '') { |
|
217 | + return new JSONResponse([ |
|
218 | + 'status' => 'error', |
|
219 | + 'data' => [ |
|
220 | + 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), |
|
221 | + ] |
|
222 | + ]); |
|
223 | + } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
224 | + return new JSONResponse([ |
|
225 | + 'status' => 'error', |
|
226 | + 'data' => [ |
|
227 | + 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), |
|
228 | + ] |
|
229 | + ]); |
|
230 | + } else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
|
231 | + try { |
|
232 | + $result = $targetUser->setPassword($password, $recoveryPassword); |
|
233 | + // password policy app throws exception |
|
234 | + } catch (HintException $e) { |
|
235 | + return new JSONResponse([ |
|
236 | + 'status' => 'error', |
|
237 | + 'data' => [ |
|
238 | + 'message' => $e->getHint(), |
|
239 | + ], |
|
240 | + ]); |
|
241 | + } |
|
242 | + if (!$result && $recoveryEnabledForUser) { |
|
243 | + return new JSONResponse([ |
|
244 | + 'status' => 'error', |
|
245 | + 'data' => [ |
|
246 | + 'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was updated.'), |
|
247 | + ] |
|
248 | + ]); |
|
249 | + } elseif (!$result && !$recoveryEnabledForUser) { |
|
250 | + return new JSONResponse([ |
|
251 | + 'status' => 'error', |
|
252 | + 'data' => [ |
|
253 | + 'message' => $this->l->t('Unable to change password'), |
|
254 | + ] |
|
255 | + ]); |
|
256 | + } |
|
257 | + } |
|
258 | + } else { |
|
259 | + try { |
|
260 | + if ($targetUser->setPassword($password) === false) { |
|
261 | + return new JSONResponse([ |
|
262 | + 'status' => 'error', |
|
263 | + 'data' => [ |
|
264 | + 'message' => $this->l->t('Unable to change password'), |
|
265 | + ], |
|
266 | + ]); |
|
267 | + } |
|
268 | + // password policy app throws exception |
|
269 | + } catch (HintException $e) { |
|
270 | + return new JSONResponse([ |
|
271 | + 'status' => 'error', |
|
272 | + 'data' => [ |
|
273 | + 'message' => $e->getHint(), |
|
274 | + ], |
|
275 | + ]); |
|
276 | + } |
|
277 | + } |
|
278 | 278 | |
279 | - return new JSONResponse([ |
|
280 | - 'status' => 'success', |
|
281 | - 'data' => [ |
|
282 | - 'username' => $username, |
|
283 | - ], |
|
284 | - ]); |
|
285 | - } |
|
279 | + return new JSONResponse([ |
|
280 | + 'status' => 'success', |
|
281 | + 'data' => [ |
|
282 | + 'username' => $username, |
|
283 | + ], |
|
284 | + ]); |
|
285 | + } |
|
286 | 286 | } |