LazyUser.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 getHome() {
  62. return $this->getUser()->getHome();
  63. }
  64. public function getBackendClassName() {
  65. return $this->getUser()->getBackendClassName();
  66. }
  67. public function getBackend(): ?UserInterface {
  68. return $this->getUser()->getBackend();
  69. }
  70. public function canChangeAvatar() {
  71. return $this->getUser()->canChangeAvatar();
  72. }
  73. public function canChangePassword() {
  74. return $this->getUser()->canChangePassword();
  75. }
  76. public function canChangeDisplayName() {
  77. return $this->getUser()->canChangeDisplayName();
  78. }
  79. public function isEnabled() {
  80. return $this->getUser()->isEnabled();
  81. }
  82. public function setEnabled(bool $enabled = true) {
  83. return $this->getUser()->setEnabled($enabled);
  84. }
  85. public function getEMailAddress() {
  86. return $this->getUser()->getEMailAddress();
  87. }
  88. public function getSystemEMailAddress(): ?string {
  89. return $this->getUser()->getSystemEMailAddress();
  90. }
  91. public function getPrimaryEMailAddress(): ?string {
  92. return $this->getUser()->getPrimaryEMailAddress();
  93. }
  94. public function getAvatarImage($size) {
  95. return $this->getUser()->getAvatarImage($size);
  96. }
  97. public function getCloudId() {
  98. return $this->getUser()->getCloudId();
  99. }
  100. public function setEMailAddress($mailAddress) {
  101. $this->getUser()->setEMailAddress($mailAddress);
  102. }
  103. public function setSystemEMailAddress(string $mailAddress): void {
  104. $this->getUser()->setSystemEMailAddress($mailAddress);
  105. }
  106. public function setPrimaryEMailAddress(string $mailAddress): void {
  107. $this->getUser()->setPrimaryEMailAddress($mailAddress);
  108. }
  109. public function getQuota() {
  110. return $this->getUser()->getQuota();
  111. }
  112. public function setQuota($quota) {
  113. $this->getUser()->setQuota($quota);
  114. }
  115. public function getManagerUids(): array {
  116. return $this->getUser()->getManagerUids();
  117. }
  118. public function setManagerUids(array $uids): void {
  119. $this->getUser()->setManagerUids($uids);
  120. }
  121. }