LazyUser.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\User;
  8. use OCP\IUser;
  9. use OCP\IUserManager;
  10. use OCP\UserInterface;
  11. class LazyUser implements IUser {
  12. private ?IUser $user = null;
  13. private string $uid;
  14. private ?string $displayName;
  15. private IUserManager $userManager;
  16. private ?UserInterface $backend;
  17. public function __construct(string $uid, IUserManager $userManager, ?string $displayName = null, ?UserInterface $backend = null) {
  18. $this->uid = $uid;
  19. $this->userManager = $userManager;
  20. $this->displayName = $displayName;
  21. $this->backend = $backend;
  22. }
  23. private function getUser(): IUser {
  24. if ($this->user === null) {
  25. if ($this->backend) {
  26. /** @var \OC\User\Manager $manager */
  27. $manager = $this->userManager;
  28. $this->user = $manager->getUserObject($this->uid, $this->backend);
  29. } else {
  30. $this->user = $this->userManager->get($this->uid);
  31. }
  32. }
  33. if ($this->user === null) {
  34. throw new NoUserException('User not found in backend');
  35. }
  36. return $this->user;
  37. }
  38. public function getUID() {
  39. return $this->uid;
  40. }
  41. public function getDisplayName() {
  42. if ($this->displayName) {
  43. return $this->displayName;
  44. }
  45. return $this->userManager->getDisplayName($this->uid) ?? $this->uid;
  46. }
  47. public function setDisplayName($displayName) {
  48. return $this->getUser()->setDisplayName($displayName);
  49. }
  50. public function getLastLogin() {
  51. return $this->getUser()->getLastLogin();
  52. }
  53. public function updateLastLoginTimestamp() {
  54. return $this->getUser()->updateLastLoginTimestamp();
  55. }
  56. public function delete() {
  57. return $this->getUser()->delete();
  58. }
  59. public function setPassword($password, $recoveryPassword = null) {
  60. return $this->getUser()->setPassword($password, $recoveryPassword);
  61. }
  62. public function getPasswordHash(): ?string {
  63. return $this->getUser()->getPasswordHash();
  64. }
  65. public function setPasswordHash(string $passwordHash): bool {
  66. return $this->getUser()->setPasswordHash($passwordHash);
  67. }
  68. public function getHome() {
  69. return $this->getUser()->getHome();
  70. }
  71. public function getBackendClassName() {
  72. return $this->getUser()->getBackendClassName();
  73. }
  74. public function getBackend(): ?UserInterface {
  75. return $this->getUser()->getBackend();
  76. }
  77. public function canChangeAvatar() {
  78. return $this->getUser()->canChangeAvatar();
  79. }
  80. public function canChangePassword() {
  81. return $this->getUser()->canChangePassword();
  82. }
  83. public function canChangeDisplayName() {
  84. return $this->getUser()->canChangeDisplayName();
  85. }
  86. public function isEnabled() {
  87. return $this->getUser()->isEnabled();
  88. }
  89. public function setEnabled(bool $enabled = true) {
  90. return $this->getUser()->setEnabled($enabled);
  91. }
  92. public function getEMailAddress() {
  93. return $this->getUser()->getEMailAddress();
  94. }
  95. public function getSystemEMailAddress(): ?string {
  96. return $this->getUser()->getSystemEMailAddress();
  97. }
  98. public function getPrimaryEMailAddress(): ?string {
  99. return $this->getUser()->getPrimaryEMailAddress();
  100. }
  101. public function getAvatarImage($size) {
  102. return $this->getUser()->getAvatarImage($size);
  103. }
  104. public function getCloudId() {
  105. return $this->getUser()->getCloudId();
  106. }
  107. public function setEMailAddress($mailAddress) {
  108. $this->getUser()->setEMailAddress($mailAddress);
  109. }
  110. public function setSystemEMailAddress(string $mailAddress): void {
  111. $this->getUser()->setSystemEMailAddress($mailAddress);
  112. }
  113. public function setPrimaryEMailAddress(string $mailAddress): void {
  114. $this->getUser()->setPrimaryEMailAddress($mailAddress);
  115. }
  116. public function getQuota() {
  117. return $this->getUser()->getQuota();
  118. }
  119. public function setQuota($quota) {
  120. $this->getUser()->setQuota($quota);
  121. }
  122. public function getManagerUids(): array {
  123. return $this->getUser()->getManagerUids();
  124. }
  125. public function setManagerUids(array $uids): void {
  126. $this->getUser()->setManagerUids($uids);
  127. }
  128. }