1
0

LazyUser.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /** @var IUser */
  34. $user = $this->user;
  35. return $user;
  36. }
  37. public function getUID() {
  38. return $this->uid;
  39. }
  40. public function getDisplayName() {
  41. if ($this->displayName) {
  42. return $this->displayName;
  43. }
  44. return $this->userManager->getDisplayName($this->uid) ?? $this->uid;
  45. }
  46. public function setDisplayName($displayName) {
  47. return $this->getUser()->setDisplayName($displayName);
  48. }
  49. public function getLastLogin() {
  50. return $this->getUser()->getLastLogin();
  51. }
  52. public function updateLastLoginTimestamp() {
  53. return $this->getUser()->updateLastLoginTimestamp();
  54. }
  55. public function delete() {
  56. return $this->getUser()->delete();
  57. }
  58. public function setPassword($password, $recoveryPassword = null) {
  59. return $this->getUser()->setPassword($password, $recoveryPassword);
  60. }
  61. public function getPasswordHash(): ?string {
  62. return $this->getUser()->getPasswordHash();
  63. }
  64. public function setPasswordHash(string $passwordHash): bool {
  65. return $this->getUser()->setPasswordHash($passwordHash);
  66. }
  67. public function getHome() {
  68. return $this->getUser()->getHome();
  69. }
  70. public function getBackendClassName() {
  71. return $this->getUser()->getBackendClassName();
  72. }
  73. public function getBackend(): ?UserInterface {
  74. return $this->getUser()->getBackend();
  75. }
  76. public function canChangeAvatar() {
  77. return $this->getUser()->canChangeAvatar();
  78. }
  79. public function canChangePassword() {
  80. return $this->getUser()->canChangePassword();
  81. }
  82. public function canChangeDisplayName() {
  83. return $this->getUser()->canChangeDisplayName();
  84. }
  85. public function isEnabled() {
  86. return $this->getUser()->isEnabled();
  87. }
  88. public function setEnabled(bool $enabled = true) {
  89. return $this->getUser()->setEnabled($enabled);
  90. }
  91. public function getEMailAddress() {
  92. return $this->getUser()->getEMailAddress();
  93. }
  94. public function getSystemEMailAddress(): ?string {
  95. return $this->getUser()->getSystemEMailAddress();
  96. }
  97. public function getPrimaryEMailAddress(): ?string {
  98. return $this->getUser()->getPrimaryEMailAddress();
  99. }
  100. public function getAvatarImage($size) {
  101. return $this->getUser()->getAvatarImage($size);
  102. }
  103. public function getCloudId() {
  104. return $this->getUser()->getCloudId();
  105. }
  106. public function setEMailAddress($mailAddress) {
  107. $this->getUser()->setEMailAddress($mailAddress);
  108. }
  109. public function setSystemEMailAddress(string $mailAddress): void {
  110. $this->getUser()->setSystemEMailAddress($mailAddress);
  111. }
  112. public function setPrimaryEMailAddress(string $mailAddress): void {
  113. $this->getUser()->setPrimaryEMailAddress($mailAddress);
  114. }
  115. public function getQuota() {
  116. return $this->getUser()->getQuota();
  117. }
  118. public function setQuota($quota) {
  119. $this->getUser()->setQuota($quota);
  120. }
  121. public function getManagerUids(): array {
  122. return $this->getUser()->getManagerUids();
  123. }
  124. public function setManagerUids(array $uids): void {
  125. $this->getUser()->setManagerUids($uids);
  126. }
  127. }