LazyUser.php 4.3 KB

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