@@ -30,35 +30,35 @@ |
||
| 30 | 30 | * Event to allow apps to |
| 31 | 31 | */ |
| 32 | 32 | class GetQuotaEvent extends Event { |
| 33 | - /** @var IUser */ |
|
| 34 | - private $user; |
|
| 35 | - /** @var string|null */ |
|
| 36 | - private $quota = null; |
|
| 33 | + /** @var IUser */ |
|
| 34 | + private $user; |
|
| 35 | + /** @var string|null */ |
|
| 36 | + private $quota = null; |
|
| 37 | 37 | |
| 38 | - public function __construct(IUser $user) { |
|
| 39 | - parent::__construct(); |
|
| 40 | - $this->user = $user; |
|
| 41 | - } |
|
| 38 | + public function __construct(IUser $user) { |
|
| 39 | + parent::__construct(); |
|
| 40 | + $this->user = $user; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function getUser(): IUser { |
|
| 44 | - return $this->user; |
|
| 45 | - } |
|
| 43 | + public function getUser(): IUser { |
|
| 44 | + return $this->user; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get the set quota as human readable string, or null if no overwrite is set |
|
| 49 | - * |
|
| 50 | - * @return string|null |
|
| 51 | - */ |
|
| 52 | - public function getQuota(): ?string { |
|
| 53 | - return $this->quota; |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * Get the set quota as human readable string, or null if no overwrite is set |
|
| 49 | + * |
|
| 50 | + * @return string|null |
|
| 51 | + */ |
|
| 52 | + public function getQuota(): ?string { |
|
| 53 | + return $this->quota; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Set the quota overwrite as human readable string |
|
| 58 | - * |
|
| 59 | - * @param string $quota |
|
| 60 | - */ |
|
| 61 | - public function setQuota(string $quota): void { |
|
| 62 | - $this->quota = $quota; |
|
| 63 | - } |
|
| 56 | + /** |
|
| 57 | + * Set the quota overwrite as human readable string |
|
| 58 | + * |
|
| 59 | + * @param string $quota |
|
| 60 | + */ |
|
| 61 | + public function setQuota(string $quota): void { |
|
| 62 | + $this->quota = $quota; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -56,448 +56,448 @@ |
||
| 56 | 56 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 57 | 57 | |
| 58 | 58 | class User implements IUser { |
| 59 | - /** @var string */ |
|
| 60 | - private $uid; |
|
| 61 | - |
|
| 62 | - /** @var string */ |
|
| 63 | - private $displayName; |
|
| 64 | - |
|
| 65 | - /** @var UserInterface|null */ |
|
| 66 | - private $backend; |
|
| 67 | - /** @var EventDispatcherInterface */ |
|
| 68 | - private $legacyDispatcher; |
|
| 69 | - |
|
| 70 | - /** @var IEventDispatcher */ |
|
| 71 | - private $dispatcher; |
|
| 72 | - |
|
| 73 | - /** @var bool */ |
|
| 74 | - private $enabled; |
|
| 75 | - |
|
| 76 | - /** @var Emitter|Manager */ |
|
| 77 | - private $emitter; |
|
| 78 | - |
|
| 79 | - /** @var string */ |
|
| 80 | - private $home; |
|
| 81 | - |
|
| 82 | - /** @var int */ |
|
| 83 | - private $lastLogin; |
|
| 84 | - |
|
| 85 | - /** @var \OCP\IConfig */ |
|
| 86 | - private $config; |
|
| 87 | - |
|
| 88 | - /** @var IAvatarManager */ |
|
| 89 | - private $avatarManager; |
|
| 90 | - |
|
| 91 | - /** @var IURLGenerator */ |
|
| 92 | - private $urlGenerator; |
|
| 93 | - |
|
| 94 | - public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
| 95 | - $this->uid = $uid; |
|
| 96 | - $this->backend = $backend; |
|
| 97 | - $this->legacyDispatcher = $dispatcher; |
|
| 98 | - $this->emitter = $emitter; |
|
| 99 | - if (is_null($config)) { |
|
| 100 | - $config = \OC::$server->getConfig(); |
|
| 101 | - } |
|
| 102 | - $this->config = $config; |
|
| 103 | - $this->urlGenerator = $urlGenerator; |
|
| 104 | - $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
| 105 | - $this->enabled = ($enabled === 'true'); |
|
| 106 | - $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
| 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 (!isset($this->displayName)) { |
|
| 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 | - * set the email address of the user |
|
| 170 | - * |
|
| 171 | - * @param string|null $mailAddress |
|
| 172 | - * @return void |
|
| 173 | - * @since 9.0.0 |
|
| 174 | - */ |
|
| 175 | - public function setEMailAddress($mailAddress) { |
|
| 176 | - $oldMailAddress = $this->getEMailAddress(); |
|
| 177 | - if ($oldMailAddress !== $mailAddress) { |
|
| 178 | - if ($mailAddress === '') { |
|
| 179 | - $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
| 180 | - } else { |
|
| 181 | - $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
| 182 | - } |
|
| 183 | - $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * returns the timestamp of the user's last login or 0 if the user did never |
|
| 189 | - * login |
|
| 190 | - * |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function getLastLogin() { |
|
| 194 | - return $this->lastLogin; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * updates the timestamp of the most recent login of this user |
|
| 199 | - */ |
|
| 200 | - public function updateLastLoginTimestamp() { |
|
| 201 | - $firstTimeLogin = ($this->lastLogin === 0); |
|
| 202 | - $this->lastLogin = time(); |
|
| 203 | - $this->config->setUserValue( |
|
| 204 | - $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
| 205 | - |
|
| 206 | - return $firstTimeLogin; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Delete the user |
|
| 211 | - * |
|
| 212 | - * @return bool |
|
| 213 | - */ |
|
| 214 | - public function delete() { |
|
| 215 | - $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
| 216 | - if ($this->emitter) { |
|
| 217 | - $this->emitter->emit('\OC\User', 'preDelete', [$this]); |
|
| 218 | - } |
|
| 219 | - // get the home now because it won't return it after user deletion |
|
| 220 | - $homePath = $this->getHome(); |
|
| 221 | - $result = $this->backend->deleteUser($this->uid); |
|
| 222 | - if ($result) { |
|
| 223 | - |
|
| 224 | - // FIXME: Feels like an hack - suggestions? |
|
| 225 | - |
|
| 226 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 227 | - // We have to delete the user from all groups |
|
| 228 | - foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
| 229 | - $group = $groupManager->get($groupId); |
|
| 230 | - if ($group) { |
|
| 231 | - $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this)); |
|
| 232 | - $group->removeUser($this); |
|
| 233 | - $this->dispatcher->dispatchTyped(new UserRemovedEvent($group, $this)); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - // Delete the user's keys in preferences |
|
| 237 | - \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
| 238 | - |
|
| 239 | - // Delete user files in /data/ |
|
| 240 | - if ($homePath !== false) { |
|
| 241 | - // FIXME: this operates directly on FS, should use View instead... |
|
| 242 | - // also this is not testable/mockable... |
|
| 243 | - \OC_Helper::rmdirr($homePath); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Delete the users entry in the storage table |
|
| 247 | - Storage::remove('home::' . $this->uid); |
|
| 248 | - |
|
| 249 | - \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
| 250 | - \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
| 251 | - |
|
| 252 | - /** @var IAvatarManager $avatarManager */ |
|
| 253 | - $avatarManager = \OC::$server->query(AvatarManager::class); |
|
| 254 | - $avatarManager->deleteUserAvatar($this->uid); |
|
| 255 | - |
|
| 256 | - $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
| 257 | - $notification->setUser($this->uid); |
|
| 258 | - \OC::$server->getNotificationManager()->markProcessed($notification); |
|
| 259 | - |
|
| 260 | - /** @var AccountManager $accountManager */ |
|
| 261 | - $accountManager = \OC::$server->query(AccountManager::class); |
|
| 262 | - $accountManager->deleteUser($this); |
|
| 263 | - |
|
| 264 | - $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
| 265 | - if ($this->emitter) { |
|
| 266 | - $this->emitter->emit('\OC\User', 'postDelete', [$this]); |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - return !($result === false); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Set the password of the user |
|
| 274 | - * |
|
| 275 | - * @param string $password |
|
| 276 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
| 277 | - * @return bool |
|
| 278 | - */ |
|
| 279 | - public function setPassword($password, $recoveryPassword = null) { |
|
| 280 | - $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
| 281 | - 'password' => $password, |
|
| 282 | - 'recoveryPassword' => $recoveryPassword, |
|
| 283 | - ])); |
|
| 284 | - if ($this->emitter) { |
|
| 285 | - $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]); |
|
| 286 | - } |
|
| 287 | - if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
| 288 | - $result = $this->backend->setPassword($this->uid, $password); |
|
| 289 | - $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
| 290 | - 'password' => $password, |
|
| 291 | - 'recoveryPassword' => $recoveryPassword, |
|
| 292 | - ])); |
|
| 293 | - if ($this->emitter) { |
|
| 294 | - $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); |
|
| 295 | - } |
|
| 296 | - return !($result === false); |
|
| 297 | - } else { |
|
| 298 | - return false; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * get the users home folder to mount |
|
| 304 | - * |
|
| 305 | - * @return string |
|
| 306 | - */ |
|
| 307 | - public function getHome() { |
|
| 308 | - if (!$this->home) { |
|
| 309 | - if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
| 310 | - $this->home = $home; |
|
| 311 | - } elseif ($this->config) { |
|
| 312 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
| 313 | - } else { |
|
| 314 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - return $this->home; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the name of the backend class the user is connected with |
|
| 322 | - * |
|
| 323 | - * @return string |
|
| 324 | - */ |
|
| 325 | - public function getBackendClassName() { |
|
| 326 | - if ($this->backend instanceof IUserBackend) { |
|
| 327 | - return $this->backend->getBackendName(); |
|
| 328 | - } |
|
| 329 | - return get_class($this->backend); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - public function getBackend() { |
|
| 333 | - return $this->backend; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * check if the backend allows the user to change his avatar on Personal page |
|
| 338 | - * |
|
| 339 | - * @return bool |
|
| 340 | - */ |
|
| 341 | - public function canChangeAvatar() { |
|
| 342 | - if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 343 | - return $this->backend->canChangeAvatar($this->uid); |
|
| 344 | - } |
|
| 345 | - return true; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * check if the backend supports changing passwords |
|
| 350 | - * |
|
| 351 | - * @return bool |
|
| 352 | - */ |
|
| 353 | - public function canChangePassword() { |
|
| 354 | - return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * check if the backend supports changing display names |
|
| 359 | - * |
|
| 360 | - * @return bool |
|
| 361 | - */ |
|
| 362 | - public function canChangeDisplayName() { |
|
| 363 | - if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 366 | - return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * check if the user is enabled |
|
| 371 | - * |
|
| 372 | - * @return bool |
|
| 373 | - */ |
|
| 374 | - public function isEnabled() { |
|
| 375 | - return $this->enabled; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * set the enabled status for the user |
|
| 380 | - * |
|
| 381 | - * @param bool $enabled |
|
| 382 | - */ |
|
| 383 | - public function setEnabled(bool $enabled = true) { |
|
| 384 | - $oldStatus = $this->isEnabled(); |
|
| 385 | - $this->enabled = $enabled; |
|
| 386 | - if ($oldStatus !== $this->enabled) { |
|
| 387 | - // TODO: First change the value, then trigger the event as done for all other properties. |
|
| 388 | - $this->triggerChange('enabled', $enabled, $oldStatus); |
|
| 389 | - $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * get the users email address |
|
| 395 | - * |
|
| 396 | - * @return string|null |
|
| 397 | - * @since 9.0.0 |
|
| 398 | - */ |
|
| 399 | - public function getEMailAddress() { |
|
| 400 | - return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * get the users' quota |
|
| 405 | - * |
|
| 406 | - * @return string |
|
| 407 | - * @since 9.0.0 |
|
| 408 | - */ |
|
| 409 | - public function getQuota() { |
|
| 410 | - // allow apps to modify the user quota by hooking into the event |
|
| 411 | - $event = new GetQuotaEvent($this); |
|
| 412 | - $this->dispatcher->dispatchTyped($event); |
|
| 413 | - $overwriteQuota = $event->getQuota(); |
|
| 414 | - if ($overwriteQuota) { |
|
| 415 | - $quota = $overwriteQuota; |
|
| 416 | - } else { |
|
| 417 | - $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
| 418 | - } |
|
| 419 | - if ($quota === 'default') { |
|
| 420 | - $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
| 421 | - } |
|
| 422 | - return $quota; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * set the users' quota |
|
| 427 | - * |
|
| 428 | - * @param string $quota |
|
| 429 | - * @return void |
|
| 430 | - * @since 9.0.0 |
|
| 431 | - */ |
|
| 432 | - public function setQuota($quota) { |
|
| 433 | - $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
| 434 | - if ($quota !== 'none' and $quota !== 'default') { |
|
| 435 | - $quota = OC_Helper::computerFileSize($quota); |
|
| 436 | - $quota = OC_Helper::humanFileSize($quota); |
|
| 437 | - } |
|
| 438 | - if ($quota !== $oldQuota) { |
|
| 439 | - $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
| 440 | - $this->triggerChange('quota', $quota, $oldQuota); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * get the avatar image if it exists |
|
| 446 | - * |
|
| 447 | - * @param int $size |
|
| 448 | - * @return IImage|null |
|
| 449 | - * @since 9.0.0 |
|
| 450 | - */ |
|
| 451 | - public function getAvatarImage($size) { |
|
| 452 | - // delay the initialization |
|
| 453 | - if (is_null($this->avatarManager)) { |
|
| 454 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - $avatar = $this->avatarManager->getAvatar($this->uid); |
|
| 458 | - $image = $avatar->get(-1); |
|
| 459 | - if ($image) { |
|
| 460 | - return $image; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - return null; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * get the federation cloud id |
|
| 468 | - * |
|
| 469 | - * @return string |
|
| 470 | - * @since 9.0.0 |
|
| 471 | - */ |
|
| 472 | - public function getCloudId() { |
|
| 473 | - $uid = $this->getUID(); |
|
| 474 | - $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 475 | - $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
|
| 476 | - return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * @param string $url |
|
| 481 | - * @return string |
|
| 482 | - */ |
|
| 483 | - private function removeProtocolFromUrl($url) { |
|
| 484 | - if (strpos($url, 'https://') === 0) { |
|
| 485 | - return substr($url, strlen('https://')); |
|
| 486 | - } elseif (strpos($url, 'http://') === 0) { |
|
| 487 | - return substr($url, strlen('http://')); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - return $url; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - public function triggerChange($feature, $value = null, $oldValue = null) { |
|
| 494 | - $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
| 495 | - 'feature' => $feature, |
|
| 496 | - 'value' => $value, |
|
| 497 | - 'oldValue' => $oldValue, |
|
| 498 | - ])); |
|
| 499 | - if ($this->emitter) { |
|
| 500 | - $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); |
|
| 501 | - } |
|
| 502 | - } |
|
| 59 | + /** @var string */ |
|
| 60 | + private $uid; |
|
| 61 | + |
|
| 62 | + /** @var string */ |
|
| 63 | + private $displayName; |
|
| 64 | + |
|
| 65 | + /** @var UserInterface|null */ |
|
| 66 | + private $backend; |
|
| 67 | + /** @var EventDispatcherInterface */ |
|
| 68 | + private $legacyDispatcher; |
|
| 69 | + |
|
| 70 | + /** @var IEventDispatcher */ |
|
| 71 | + private $dispatcher; |
|
| 72 | + |
|
| 73 | + /** @var bool */ |
|
| 74 | + private $enabled; |
|
| 75 | + |
|
| 76 | + /** @var Emitter|Manager */ |
|
| 77 | + private $emitter; |
|
| 78 | + |
|
| 79 | + /** @var string */ |
|
| 80 | + private $home; |
|
| 81 | + |
|
| 82 | + /** @var int */ |
|
| 83 | + private $lastLogin; |
|
| 84 | + |
|
| 85 | + /** @var \OCP\IConfig */ |
|
| 86 | + private $config; |
|
| 87 | + |
|
| 88 | + /** @var IAvatarManager */ |
|
| 89 | + private $avatarManager; |
|
| 90 | + |
|
| 91 | + /** @var IURLGenerator */ |
|
| 92 | + private $urlGenerator; |
|
| 93 | + |
|
| 94 | + public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
| 95 | + $this->uid = $uid; |
|
| 96 | + $this->backend = $backend; |
|
| 97 | + $this->legacyDispatcher = $dispatcher; |
|
| 98 | + $this->emitter = $emitter; |
|
| 99 | + if (is_null($config)) { |
|
| 100 | + $config = \OC::$server->getConfig(); |
|
| 101 | + } |
|
| 102 | + $this->config = $config; |
|
| 103 | + $this->urlGenerator = $urlGenerator; |
|
| 104 | + $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
| 105 | + $this->enabled = ($enabled === 'true'); |
|
| 106 | + $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
| 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 (!isset($this->displayName)) { |
|
| 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 | + * set the email address of the user |
|
| 170 | + * |
|
| 171 | + * @param string|null $mailAddress |
|
| 172 | + * @return void |
|
| 173 | + * @since 9.0.0 |
|
| 174 | + */ |
|
| 175 | + public function setEMailAddress($mailAddress) { |
|
| 176 | + $oldMailAddress = $this->getEMailAddress(); |
|
| 177 | + if ($oldMailAddress !== $mailAddress) { |
|
| 178 | + if ($mailAddress === '') { |
|
| 179 | + $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
| 180 | + } else { |
|
| 181 | + $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
| 182 | + } |
|
| 183 | + $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * returns the timestamp of the user's last login or 0 if the user did never |
|
| 189 | + * login |
|
| 190 | + * |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function getLastLogin() { |
|
| 194 | + return $this->lastLogin; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * updates the timestamp of the most recent login of this user |
|
| 199 | + */ |
|
| 200 | + public function updateLastLoginTimestamp() { |
|
| 201 | + $firstTimeLogin = ($this->lastLogin === 0); |
|
| 202 | + $this->lastLogin = time(); |
|
| 203 | + $this->config->setUserValue( |
|
| 204 | + $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
| 205 | + |
|
| 206 | + return $firstTimeLogin; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Delete the user |
|
| 211 | + * |
|
| 212 | + * @return bool |
|
| 213 | + */ |
|
| 214 | + public function delete() { |
|
| 215 | + $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
| 216 | + if ($this->emitter) { |
|
| 217 | + $this->emitter->emit('\OC\User', 'preDelete', [$this]); |
|
| 218 | + } |
|
| 219 | + // get the home now because it won't return it after user deletion |
|
| 220 | + $homePath = $this->getHome(); |
|
| 221 | + $result = $this->backend->deleteUser($this->uid); |
|
| 222 | + if ($result) { |
|
| 223 | + |
|
| 224 | + // FIXME: Feels like an hack - suggestions? |
|
| 225 | + |
|
| 226 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 227 | + // We have to delete the user from all groups |
|
| 228 | + foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
| 229 | + $group = $groupManager->get($groupId); |
|
| 230 | + if ($group) { |
|
| 231 | + $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this)); |
|
| 232 | + $group->removeUser($this); |
|
| 233 | + $this->dispatcher->dispatchTyped(new UserRemovedEvent($group, $this)); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + // Delete the user's keys in preferences |
|
| 237 | + \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
| 238 | + |
|
| 239 | + // Delete user files in /data/ |
|
| 240 | + if ($homePath !== false) { |
|
| 241 | + // FIXME: this operates directly on FS, should use View instead... |
|
| 242 | + // also this is not testable/mockable... |
|
| 243 | + \OC_Helper::rmdirr($homePath); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Delete the users entry in the storage table |
|
| 247 | + Storage::remove('home::' . $this->uid); |
|
| 248 | + |
|
| 249 | + \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
| 250 | + \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
| 251 | + |
|
| 252 | + /** @var IAvatarManager $avatarManager */ |
|
| 253 | + $avatarManager = \OC::$server->query(AvatarManager::class); |
|
| 254 | + $avatarManager->deleteUserAvatar($this->uid); |
|
| 255 | + |
|
| 256 | + $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
| 257 | + $notification->setUser($this->uid); |
|
| 258 | + \OC::$server->getNotificationManager()->markProcessed($notification); |
|
| 259 | + |
|
| 260 | + /** @var AccountManager $accountManager */ |
|
| 261 | + $accountManager = \OC::$server->query(AccountManager::class); |
|
| 262 | + $accountManager->deleteUser($this); |
|
| 263 | + |
|
| 264 | + $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
| 265 | + if ($this->emitter) { |
|
| 266 | + $this->emitter->emit('\OC\User', 'postDelete', [$this]); |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + return !($result === false); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Set the password of the user |
|
| 274 | + * |
|
| 275 | + * @param string $password |
|
| 276 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
| 277 | + * @return bool |
|
| 278 | + */ |
|
| 279 | + public function setPassword($password, $recoveryPassword = null) { |
|
| 280 | + $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
| 281 | + 'password' => $password, |
|
| 282 | + 'recoveryPassword' => $recoveryPassword, |
|
| 283 | + ])); |
|
| 284 | + if ($this->emitter) { |
|
| 285 | + $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]); |
|
| 286 | + } |
|
| 287 | + if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
| 288 | + $result = $this->backend->setPassword($this->uid, $password); |
|
| 289 | + $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
| 290 | + 'password' => $password, |
|
| 291 | + 'recoveryPassword' => $recoveryPassword, |
|
| 292 | + ])); |
|
| 293 | + if ($this->emitter) { |
|
| 294 | + $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); |
|
| 295 | + } |
|
| 296 | + return !($result === false); |
|
| 297 | + } else { |
|
| 298 | + return false; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * get the users home folder to mount |
|
| 304 | + * |
|
| 305 | + * @return string |
|
| 306 | + */ |
|
| 307 | + public function getHome() { |
|
| 308 | + if (!$this->home) { |
|
| 309 | + if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
| 310 | + $this->home = $home; |
|
| 311 | + } elseif ($this->config) { |
|
| 312 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
| 313 | + } else { |
|
| 314 | + $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + return $this->home; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the name of the backend class the user is connected with |
|
| 322 | + * |
|
| 323 | + * @return string |
|
| 324 | + */ |
|
| 325 | + public function getBackendClassName() { |
|
| 326 | + if ($this->backend instanceof IUserBackend) { |
|
| 327 | + return $this->backend->getBackendName(); |
|
| 328 | + } |
|
| 329 | + return get_class($this->backend); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + public function getBackend() { |
|
| 333 | + return $this->backend; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * check if the backend allows the user to change his avatar on Personal page |
|
| 338 | + * |
|
| 339 | + * @return bool |
|
| 340 | + */ |
|
| 341 | + public function canChangeAvatar() { |
|
| 342 | + if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 343 | + return $this->backend->canChangeAvatar($this->uid); |
|
| 344 | + } |
|
| 345 | + return true; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * check if the backend supports changing passwords |
|
| 350 | + * |
|
| 351 | + * @return bool |
|
| 352 | + */ |
|
| 353 | + public function canChangePassword() { |
|
| 354 | + return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * check if the backend supports changing display names |
|
| 359 | + * |
|
| 360 | + * @return bool |
|
| 361 | + */ |
|
| 362 | + public function canChangeDisplayName() { |
|
| 363 | + if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | + return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * check if the user is enabled |
|
| 371 | + * |
|
| 372 | + * @return bool |
|
| 373 | + */ |
|
| 374 | + public function isEnabled() { |
|
| 375 | + return $this->enabled; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * set the enabled status for the user |
|
| 380 | + * |
|
| 381 | + * @param bool $enabled |
|
| 382 | + */ |
|
| 383 | + public function setEnabled(bool $enabled = true) { |
|
| 384 | + $oldStatus = $this->isEnabled(); |
|
| 385 | + $this->enabled = $enabled; |
|
| 386 | + if ($oldStatus !== $this->enabled) { |
|
| 387 | + // TODO: First change the value, then trigger the event as done for all other properties. |
|
| 388 | + $this->triggerChange('enabled', $enabled, $oldStatus); |
|
| 389 | + $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * get the users email address |
|
| 395 | + * |
|
| 396 | + * @return string|null |
|
| 397 | + * @since 9.0.0 |
|
| 398 | + */ |
|
| 399 | + public function getEMailAddress() { |
|
| 400 | + return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * get the users' quota |
|
| 405 | + * |
|
| 406 | + * @return string |
|
| 407 | + * @since 9.0.0 |
|
| 408 | + */ |
|
| 409 | + public function getQuota() { |
|
| 410 | + // allow apps to modify the user quota by hooking into the event |
|
| 411 | + $event = new GetQuotaEvent($this); |
|
| 412 | + $this->dispatcher->dispatchTyped($event); |
|
| 413 | + $overwriteQuota = $event->getQuota(); |
|
| 414 | + if ($overwriteQuota) { |
|
| 415 | + $quota = $overwriteQuota; |
|
| 416 | + } else { |
|
| 417 | + $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
| 418 | + } |
|
| 419 | + if ($quota === 'default') { |
|
| 420 | + $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
| 421 | + } |
|
| 422 | + return $quota; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * set the users' quota |
|
| 427 | + * |
|
| 428 | + * @param string $quota |
|
| 429 | + * @return void |
|
| 430 | + * @since 9.0.0 |
|
| 431 | + */ |
|
| 432 | + public function setQuota($quota) { |
|
| 433 | + $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
| 434 | + if ($quota !== 'none' and $quota !== 'default') { |
|
| 435 | + $quota = OC_Helper::computerFileSize($quota); |
|
| 436 | + $quota = OC_Helper::humanFileSize($quota); |
|
| 437 | + } |
|
| 438 | + if ($quota !== $oldQuota) { |
|
| 439 | + $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
| 440 | + $this->triggerChange('quota', $quota, $oldQuota); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * get the avatar image if it exists |
|
| 446 | + * |
|
| 447 | + * @param int $size |
|
| 448 | + * @return IImage|null |
|
| 449 | + * @since 9.0.0 |
|
| 450 | + */ |
|
| 451 | + public function getAvatarImage($size) { |
|
| 452 | + // delay the initialization |
|
| 453 | + if (is_null($this->avatarManager)) { |
|
| 454 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + $avatar = $this->avatarManager->getAvatar($this->uid); |
|
| 458 | + $image = $avatar->get(-1); |
|
| 459 | + if ($image) { |
|
| 460 | + return $image; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + return null; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * get the federation cloud id |
|
| 468 | + * |
|
| 469 | + * @return string |
|
| 470 | + * @since 9.0.0 |
|
| 471 | + */ |
|
| 472 | + public function getCloudId() { |
|
| 473 | + $uid = $this->getUID(); |
|
| 474 | + $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 475 | + $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
|
| 476 | + return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * @param string $url |
|
| 481 | + * @return string |
|
| 482 | + */ |
|
| 483 | + private function removeProtocolFromUrl($url) { |
|
| 484 | + if (strpos($url, 'https://') === 0) { |
|
| 485 | + return substr($url, strlen('https://')); |
|
| 486 | + } elseif (strpos($url, 'http://') === 0) { |
|
| 487 | + return substr($url, strlen('http://')); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + return $url; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + public function triggerChange($feature, $value = null, $oldValue = null) { |
|
| 494 | + $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
| 495 | + 'feature' => $feature, |
|
| 496 | + 'value' => $value, |
|
| 497 | + 'oldValue' => $oldValue, |
|
| 498 | + ])); |
|
| 499 | + if ($this->emitter) { |
|
| 500 | + $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | 503 | } |