LazyUser.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\User;
  23. use OCP\IUser;
  24. use OCP\IUserManager;
  25. use OCP\UserInterface;
  26. class LazyUser implements IUser {
  27. private ?IUser $user = null;
  28. private string $uid;
  29. private ?string $displayName;
  30. private IUserManager $userManager;
  31. private ?UserInterface $backend;
  32. public function __construct(string $uid, IUserManager $userManager, ?string $displayName = null, ?UserInterface $backend = null) {
  33. $this->uid = $uid;
  34. $this->userManager = $userManager;
  35. $this->displayName = $displayName;
  36. $this->backend = $backend;
  37. }
  38. private function getUser(): IUser {
  39. if ($this->user === null) {
  40. if ($this->backend) {
  41. /** @var \OC\User\Manager $manager */
  42. $manager = $this->userManager;
  43. $this->user = $manager->getUserObject($this->uid, $this->backend);
  44. } else {
  45. $this->user = $this->userManager->get($this->uid);
  46. }
  47. }
  48. if ($this->user === null) {
  49. throw new NoUserException('User not found in backend');
  50. }
  51. return $this->user;
  52. }
  53. public function getUID() {
  54. return $this->uid;
  55. }
  56. public function getDisplayName() {
  57. if ($this->displayName) {
  58. return $this->displayName;
  59. }
  60. return $this->userManager->getDisplayName($this->uid) ?? $this->uid;
  61. }
  62. public function setDisplayName($displayName) {
  63. return $this->getUser()->setDisplayName($displayName);
  64. }
  65. public function getLastLogin() {
  66. return $this->getUser()->getLastLogin();
  67. }
  68. public function updateLastLoginTimestamp() {
  69. return $this->getUser()->updateLastLoginTimestamp();
  70. }
  71. public function delete() {
  72. return $this->getUser()->delete();
  73. }
  74. public function setPassword($password, $recoveryPassword = null) {
  75. return $this->getUser()->setPassword($password, $recoveryPassword);
  76. }
  77. public function getHome() {
  78. return $this->getUser()->getHome();
  79. }
  80. public function getBackendClassName() {
  81. return $this->getUser()->getBackendClassName();
  82. }
  83. public function getBackend(): ?UserInterface {
  84. return $this->getUser()->getBackend();
  85. }
  86. public function canChangeAvatar() {
  87. return $this->getUser()->canChangeAvatar();
  88. }
  89. public function canChangePassword() {
  90. return $this->getUser()->canChangePassword();
  91. }
  92. public function canChangeDisplayName() {
  93. return $this->getUser()->canChangeDisplayName();
  94. }
  95. public function isEnabled() {
  96. return $this->getUser()->isEnabled();
  97. }
  98. public function setEnabled(bool $enabled = true) {
  99. return $this->getUser()->setEnabled($enabled);
  100. }
  101. public function getEMailAddress() {
  102. return $this->getUser()->getEMailAddress();
  103. }
  104. public function getSystemEMailAddress(): ?string {
  105. return $this->getUser()->getSystemEMailAddress();
  106. }
  107. public function getPrimaryEMailAddress(): ?string {
  108. return $this->getUser()->getPrimaryEMailAddress();
  109. }
  110. public function getAvatarImage($size) {
  111. return $this->getUser()->getAvatarImage($size);
  112. }
  113. public function getCloudId() {
  114. return $this->getUser()->getCloudId();
  115. }
  116. public function setEMailAddress($mailAddress) {
  117. $this->getUser()->setEMailAddress($mailAddress);
  118. }
  119. public function setSystemEMailAddress(string $mailAddress): void {
  120. $this->getUser()->setSystemEMailAddress($mailAddress);
  121. }
  122. public function setPrimaryEMailAddress(string $mailAddress): void {
  123. $this->getUser()->setPrimaryEMailAddress($mailAddress);
  124. }
  125. public function getQuota() {
  126. return $this->getUser()->getQuota();
  127. }
  128. public function setQuota($quota) {
  129. $this->getUser()->setQuota($quota);
  130. }
  131. public function getManagerUids(): array {
  132. return $this->getUser()->getManagerUids();
  133. }
  134. public function setManagerUids(array $uids): void {
  135. $this->getUser()->setManagerUids($uids);
  136. }
  137. }