Manager.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\User;
  8. use OCA\User_LDAP\Access;
  9. use OCA\User_LDAP\FilesystemHelper;
  10. use OCP\Cache\CappedMemoryCache;
  11. use OCP\IAvatarManager;
  12. use OCP\IConfig;
  13. use OCP\IDBConnection;
  14. use OCP\Image;
  15. use OCP\IUserManager;
  16. use OCP\Notification\IManager as INotificationManager;
  17. use OCP\Share\IManager;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Manager
  21. *
  22. * upon request, returns an LDAP user object either by creating or from run-time
  23. * cache
  24. */
  25. class Manager {
  26. protected ?Access $access = null;
  27. protected IDBConnection $db;
  28. /** @var CappedMemoryCache<User> $usersByDN */
  29. protected CappedMemoryCache $usersByDN;
  30. /** @var CappedMemoryCache<User> $usersByUid */
  31. protected CappedMemoryCache $usersByUid;
  32. public function __construct(
  33. protected IConfig $ocConfig,
  34. protected FilesystemHelper $ocFilesystem,
  35. protected LoggerInterface $logger,
  36. protected IAvatarManager $avatarManager,
  37. protected Image $image,
  38. protected IUserManager $userManager,
  39. protected INotificationManager $notificationManager,
  40. private IManager $shareManager,
  41. ) {
  42. $this->usersByDN = new CappedMemoryCache();
  43. $this->usersByUid = new CappedMemoryCache();
  44. }
  45. /**
  46. * Binds manager to an instance of Access.
  47. * It needs to be assigned first before the manager can be used.
  48. * @param Access
  49. */
  50. public function setLdapAccess(Access $access) {
  51. $this->access = $access;
  52. }
  53. /**
  54. * @brief creates an instance of User and caches (just runtime) it in the
  55. * property array
  56. * @param string $dn the DN of the user
  57. * @param string $uid the internal (owncloud) username
  58. * @return User
  59. */
  60. private function createAndCache($dn, $uid) {
  61. $this->checkAccess();
  62. $user = new User($uid, $dn, $this->access, $this->ocConfig,
  63. $this->ocFilesystem, clone $this->image, $this->logger,
  64. $this->avatarManager, $this->userManager,
  65. $this->notificationManager);
  66. $this->usersByDN[$dn] = $user;
  67. $this->usersByUid[$uid] = $user;
  68. return $user;
  69. }
  70. /**
  71. * removes a user entry from the cache
  72. * @param $uid
  73. */
  74. public function invalidate($uid) {
  75. if (!isset($this->usersByUid[$uid])) {
  76. return;
  77. }
  78. $dn = $this->usersByUid[$uid]->getDN();
  79. unset($this->usersByUid[$uid]);
  80. unset($this->usersByDN[$dn]);
  81. }
  82. /**
  83. * @brief checks whether the Access instance has been set
  84. * @throws \Exception if Access has not been set
  85. * @psalm-assert !null $this->access
  86. * @return null
  87. */
  88. private function checkAccess() {
  89. if (is_null($this->access)) {
  90. throw new \Exception('LDAP Access instance must be set first');
  91. }
  92. }
  93. /**
  94. * returns a list of attributes that will be processed further, e.g. quota,
  95. * email, displayname, or others.
  96. *
  97. * @param bool $minimal - optional, set to true to skip attributes with big
  98. * payload
  99. * @return string[]
  100. */
  101. public function getAttributes($minimal = false) {
  102. $baseAttributes = array_merge(Access::UUID_ATTRIBUTES, ['dn', 'uid', 'samaccountname', 'memberof']);
  103. $attributes = [
  104. $this->access->getConnection()->ldapExpertUUIDUserAttr,
  105. $this->access->getConnection()->ldapQuotaAttribute,
  106. $this->access->getConnection()->ldapEmailAttribute,
  107. $this->access->getConnection()->ldapUserDisplayName,
  108. $this->access->getConnection()->ldapUserDisplayName2,
  109. $this->access->getConnection()->ldapExtStorageHomeAttribute,
  110. $this->access->getConnection()->ldapAttributePhone,
  111. $this->access->getConnection()->ldapAttributeWebsite,
  112. $this->access->getConnection()->ldapAttributeAddress,
  113. $this->access->getConnection()->ldapAttributeTwitter,
  114. $this->access->getConnection()->ldapAttributeFediverse,
  115. $this->access->getConnection()->ldapAttributeOrganisation,
  116. $this->access->getConnection()->ldapAttributeRole,
  117. $this->access->getConnection()->ldapAttributeHeadline,
  118. $this->access->getConnection()->ldapAttributeBiography,
  119. $this->access->getConnection()->ldapAttributeBirthDate,
  120. $this->access->getConnection()->ldapAttributePronouns,
  121. ];
  122. $homeRule = (string)$this->access->getConnection()->homeFolderNamingRule;
  123. if (str_starts_with($homeRule, 'attr:')) {
  124. $attributes[] = substr($homeRule, strlen('attr:'));
  125. }
  126. if (!$minimal) {
  127. // attributes that are not really important but may come with big
  128. // payload.
  129. $attributes = array_merge(
  130. $attributes,
  131. $this->access->getConnection()->resolveRule('avatar')
  132. );
  133. }
  134. $attributes = array_reduce($attributes,
  135. function ($list, $attribute) {
  136. $attribute = strtolower(trim((string)$attribute));
  137. if (!empty($attribute) && !in_array($attribute, $list)) {
  138. $list[] = $attribute;
  139. }
  140. return $list;
  141. },
  142. $baseAttributes // hard-coded, lower-case, non-empty attributes
  143. );
  144. return $attributes;
  145. }
  146. /**
  147. * Checks whether the specified user is marked as deleted
  148. * @param string $id the Nextcloud user name
  149. * @return bool
  150. */
  151. public function isDeletedUser($id) {
  152. $isDeleted = $this->ocConfig->getUserValue(
  153. $id, 'user_ldap', 'isDeleted', 0);
  154. return (int)$isDeleted === 1;
  155. }
  156. /**
  157. * creates and returns an instance of OfflineUser for the specified user
  158. * @param string $id
  159. * @return OfflineUser
  160. */
  161. public function getDeletedUser($id) {
  162. return new OfflineUser(
  163. $id,
  164. $this->ocConfig,
  165. $this->access->getUserMapper(),
  166. $this->shareManager
  167. );
  168. }
  169. /**
  170. * @brief returns a User object by its Nextcloud username
  171. * @param string $id the DN or username of the user
  172. * @return User|OfflineUser|null
  173. */
  174. protected function createInstancyByUserName($id) {
  175. //most likely a uid. Check whether it is a deleted user
  176. if ($this->isDeletedUser($id)) {
  177. return $this->getDeletedUser($id);
  178. }
  179. $dn = $this->access->username2dn($id);
  180. if ($dn !== false) {
  181. return $this->createAndCache($dn, $id);
  182. }
  183. return null;
  184. }
  185. /**
  186. * @brief returns a User object by its DN or Nextcloud username
  187. * @param string $id the DN or username of the user
  188. * @return User|OfflineUser|null
  189. * @throws \Exception when connection could not be established
  190. */
  191. public function get($id) {
  192. $this->checkAccess();
  193. if (isset($this->usersByDN[$id])) {
  194. return $this->usersByDN[$id];
  195. } elseif (isset($this->usersByUid[$id])) {
  196. return $this->usersByUid[$id];
  197. }
  198. if ($this->access->stringResemblesDN($id)) {
  199. $uid = $this->access->dn2username($id);
  200. if ($uid !== false) {
  201. return $this->createAndCache($id, $uid);
  202. }
  203. }
  204. return $this->createInstancyByUserName($id);
  205. }
  206. /**
  207. * @brief Checks whether a User object by its DN or Nextcloud username exists
  208. * @param string $id the DN or username of the user
  209. * @throws \Exception when connection could not be established
  210. */
  211. public function exists($id): bool {
  212. $this->checkAccess();
  213. $this->logger->debug('Checking if {id} exists', ['id' => $id]);
  214. if (isset($this->usersByDN[$id])) {
  215. return true;
  216. } elseif (isset($this->usersByUid[$id])) {
  217. return true;
  218. }
  219. if ($this->access->stringResemblesDN($id)) {
  220. $this->logger->debug('{id} looks like a dn', ['id' => $id]);
  221. $uid = $this->access->dn2username($id);
  222. if ($uid !== false) {
  223. return true;
  224. }
  225. }
  226. // Most likely a uid. Check whether it is a deleted user
  227. if ($this->isDeletedUser($id)) {
  228. return true;
  229. }
  230. $dn = $this->access->username2dn($id);
  231. if ($dn !== false) {
  232. return true;
  233. }
  234. return false;
  235. }
  236. }