User_Proxy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-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;
  8. use OCA\User_LDAP\User\DeletedUsersIndex;
  9. use OCA\User_LDAP\User\OfflineUser;
  10. use OCA\User_LDAP\User\User;
  11. use OCP\IUserBackend;
  12. use OCP\Notification\IManager as INotificationManager;
  13. use OCP\User\Backend\ICountMappedUsersBackend;
  14. use OCP\User\Backend\ICountUsersBackend;
  15. use OCP\User\Backend\IGetUserCreationDateBackend;
  16. use OCP\User\Backend\IProvideEnabledStateBackend;
  17. use OCP\UserInterface;
  18. use Psr\Log\LoggerInterface;
  19. class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend, IGetUserCreationDateBackend {
  20. /** @var User_LDAP[] */
  21. private array $backends = [];
  22. private ?User_LDAP $refBackend = null;
  23. private bool $isSetUp = false;
  24. public function __construct(
  25. private Helper $helper,
  26. ILDAPWrapper $ldap,
  27. AccessFactory $accessFactory,
  28. private INotificationManager $notificationManager,
  29. private UserPluginManager $userPluginManager,
  30. private LoggerInterface $logger,
  31. private DeletedUsersIndex $deletedUsersIndex,
  32. ) {
  33. parent::__construct($ldap, $accessFactory);
  34. }
  35. protected function setup(): void {
  36. if ($this->isSetUp) {
  37. return;
  38. }
  39. $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
  40. foreach ($serverConfigPrefixes as $configPrefix) {
  41. $this->backends[$configPrefix] = new User_LDAP(
  42. $this->getAccess($configPrefix),
  43. $this->notificationManager,
  44. $this->userPluginManager,
  45. $this->logger,
  46. $this->deletedUsersIndex,
  47. );
  48. if (is_null($this->refBackend)) {
  49. $this->refBackend = $this->backends[$configPrefix];
  50. }
  51. }
  52. $this->isSetUp = true;
  53. }
  54. /**
  55. * Tries the backends one after the other until a positive result is returned from the specified method
  56. *
  57. * @param string $id the uid connected to the request
  58. * @param string $method the method of the user backend that shall be called
  59. * @param array $parameters an array of parameters to be passed
  60. * @return mixed the result of the method or false
  61. */
  62. protected function walkBackends($id, $method, $parameters) {
  63. $this->setup();
  64. $uid = $id;
  65. $cacheKey = $this->getUserCacheKey($uid);
  66. foreach ($this->backends as $configPrefix => $backend) {
  67. $instance = $backend;
  68. if (!method_exists($instance, $method)
  69. && method_exists($this->getAccess($configPrefix), $method)) {
  70. $instance = $this->getAccess($configPrefix);
  71. }
  72. if ($result = call_user_func_array([$instance, $method], $parameters)) {
  73. if (!$this->isSingleBackend()) {
  74. $this->writeToCache($cacheKey, $configPrefix);
  75. }
  76. return $result;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. * Asks the backend connected to the server that supposely takes care of the uid from the request.
  83. *
  84. * @param string $id the uid connected to the request
  85. * @param string $method the method of the user backend that shall be called
  86. * @param array $parameters an array of parameters to be passed
  87. * @param mixed $passOnWhen the result matches this variable
  88. * @return mixed the result of the method or false
  89. */
  90. protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
  91. $this->setup();
  92. $uid = $id;
  93. $cacheKey = $this->getUserCacheKey($uid);
  94. $prefix = $this->getFromCache($cacheKey);
  95. //in case the uid has been found in the past, try this stored connection first
  96. if (!is_null($prefix)) {
  97. if (isset($this->backends[$prefix])) {
  98. $instance = $this->backends[$prefix];
  99. if (!method_exists($instance, $method)
  100. && method_exists($this->getAccess($prefix), $method)) {
  101. $instance = $this->getAccess($prefix);
  102. }
  103. $result = call_user_func_array([$instance, $method], $parameters);
  104. if ($result === $passOnWhen) {
  105. //not found here, reset cache to null if user vanished
  106. //because sometimes methods return false with a reason
  107. $userExists = call_user_func_array(
  108. [$this->backends[$prefix], 'userExistsOnLDAP'],
  109. [$uid]
  110. );
  111. if (!$userExists) {
  112. $this->writeToCache($cacheKey, null);
  113. }
  114. }
  115. return $result;
  116. }
  117. }
  118. return false;
  119. }
  120. protected function activeBackends(): int {
  121. $this->setup();
  122. return count($this->backends);
  123. }
  124. /**
  125. * Check if backend implements actions
  126. *
  127. * @param int $actions bitwise-or'ed actions
  128. * @return boolean
  129. *
  130. * Returns the supported actions as int to be
  131. * compared with \OC\User\Backend::CREATE_USER etc.
  132. */
  133. public function implementsActions($actions) {
  134. $this->setup();
  135. //it's the same across all our user backends obviously
  136. return $this->refBackend->implementsActions($actions);
  137. }
  138. /**
  139. * Backend name to be shown in user management
  140. *
  141. * @return string the name of the backend to be shown
  142. */
  143. public function getBackendName() {
  144. $this->setup();
  145. return $this->refBackend->getBackendName();
  146. }
  147. /**
  148. * Get a list of all users
  149. *
  150. * @param string $search
  151. * @param null|int $limit
  152. * @param null|int $offset
  153. * @return string[] an array of all uids
  154. */
  155. public function getUsers($search = '', $limit = 10, $offset = 0) {
  156. $this->setup();
  157. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  158. $users = [];
  159. foreach ($this->backends as $backend) {
  160. $backendUsers = $backend->getUsers($search, $limit, $offset);
  161. if (is_array($backendUsers)) {
  162. $users = array_merge($users, $backendUsers);
  163. }
  164. }
  165. return $users;
  166. }
  167. /**
  168. * check if a user exists
  169. *
  170. * @param string $uid the username
  171. * @return boolean
  172. */
  173. public function userExists($uid) {
  174. $existsOnLDAP = false;
  175. $existsLocally = $this->handleRequest($uid, 'userExists', [$uid]);
  176. if ($existsLocally) {
  177. $existsOnLDAP = $this->userExistsOnLDAP($uid);
  178. }
  179. if ($existsLocally && !$existsOnLDAP) {
  180. try {
  181. $user = $this->getLDAPAccess($uid)->userManager->get($uid);
  182. if ($user instanceof User) {
  183. $user->markUser();
  184. }
  185. } catch (\Exception $e) {
  186. // ignore
  187. }
  188. }
  189. return $existsLocally;
  190. }
  191. /**
  192. * check if a user exists on LDAP
  193. *
  194. * @param string|User $user either the Nextcloud user
  195. * name or an instance of that user
  196. */
  197. public function userExistsOnLDAP($user, bool $ignoreCache = false): bool {
  198. $id = ($user instanceof User) ? $user->getUsername() : $user;
  199. return $this->handleRequest($id, 'userExistsOnLDAP', [$user, $ignoreCache]);
  200. }
  201. /**
  202. * Check if the password is correct
  203. *
  204. * @param string $uid The username
  205. * @param string $password The password
  206. * @return bool
  207. *
  208. * Check if the password is correct without logging in the user
  209. */
  210. public function checkPassword($uid, $password) {
  211. return $this->handleRequest($uid, 'checkPassword', [$uid, $password]);
  212. }
  213. /**
  214. * returns the username for the given login name, if available
  215. *
  216. * @param string $loginName
  217. * @return string|false
  218. */
  219. public function loginName2UserName($loginName) {
  220. $id = 'LOGINNAME,' . $loginName;
  221. return $this->handleRequest($id, 'loginName2UserName', [$loginName]);
  222. }
  223. /**
  224. * returns the username for the given LDAP DN, if available
  225. *
  226. * @param string $dn
  227. * @return string|false with the username
  228. */
  229. public function dn2UserName($dn) {
  230. $id = 'DN,' . $dn;
  231. return $this->handleRequest($id, 'dn2UserName', [$dn]);
  232. }
  233. /**
  234. * get the user's home directory
  235. *
  236. * @param string $uid the username
  237. * @return boolean
  238. */
  239. public function getHome($uid) {
  240. return $this->handleRequest($uid, 'getHome', [$uid]);
  241. }
  242. /**
  243. * get display name of the user
  244. *
  245. * @param string $uid user ID of the user
  246. * @return string display name
  247. */
  248. public function getDisplayName($uid) {
  249. return $this->handleRequest($uid, 'getDisplayName', [$uid]);
  250. }
  251. /**
  252. * set display name of the user
  253. *
  254. * @param string $uid user ID of the user
  255. * @param string $displayName new display name
  256. * @return string display name
  257. */
  258. public function setDisplayName($uid, $displayName) {
  259. return $this->handleRequest($uid, 'setDisplayName', [$uid, $displayName]);
  260. }
  261. /**
  262. * checks whether the user is allowed to change their avatar in Nextcloud
  263. *
  264. * @param string $uid the Nextcloud user name
  265. * @return boolean either the user can or cannot
  266. */
  267. public function canChangeAvatar($uid) {
  268. return $this->handleRequest($uid, 'canChangeAvatar', [$uid], true);
  269. }
  270. /**
  271. * Get a list of all display names and user ids.
  272. *
  273. * @param string $search
  274. * @param int|null $limit
  275. * @param int|null $offset
  276. * @return array an array of all displayNames (value) and the corresponding uids (key)
  277. */
  278. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  279. $this->setup();
  280. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  281. $users = [];
  282. foreach ($this->backends as $backend) {
  283. $backendUsers = $backend->getDisplayNames($search, $limit, $offset);
  284. if (is_array($backendUsers)) {
  285. $users = $users + $backendUsers;
  286. }
  287. }
  288. return $users;
  289. }
  290. /**
  291. * delete a user
  292. *
  293. * @param string $uid The username of the user to delete
  294. * @return bool
  295. *
  296. * Deletes a user
  297. */
  298. public function deleteUser($uid) {
  299. return $this->handleRequest($uid, 'deleteUser', [$uid]);
  300. }
  301. /**
  302. * Set password
  303. *
  304. * @param string $uid The username
  305. * @param string $password The new password
  306. * @return bool
  307. *
  308. */
  309. public function setPassword($uid, $password) {
  310. return $this->handleRequest($uid, 'setPassword', [$uid, $password]);
  311. }
  312. /**
  313. * @return bool
  314. */
  315. public function hasUserListings() {
  316. $this->setup();
  317. return $this->refBackend->hasUserListings();
  318. }
  319. /**
  320. * Count the number of users
  321. *
  322. * @return int|false
  323. */
  324. public function countUsers() {
  325. $this->setup();
  326. $users = false;
  327. foreach ($this->backends as $backend) {
  328. $backendUsers = $backend->countUsers();
  329. if ($backendUsers !== false) {
  330. $users = (int)$users + $backendUsers;
  331. }
  332. }
  333. return $users;
  334. }
  335. /**
  336. * Count the number of mapped users
  337. */
  338. public function countMappedUsers(): int {
  339. $this->setup();
  340. $users = 0;
  341. foreach ($this->backends as $backend) {
  342. $users += $backend->countMappedUsers();
  343. }
  344. return $users;
  345. }
  346. /**
  347. * Return access for LDAP interaction.
  348. *
  349. * @param string $uid
  350. * @return Access instance of Access for LDAP interaction
  351. */
  352. public function getLDAPAccess($uid) {
  353. return $this->handleRequest($uid, 'getLDAPAccess', [$uid]);
  354. }
  355. /**
  356. * Return a new LDAP connection for the specified user.
  357. * The connection needs to be closed manually.
  358. *
  359. * @param string $uid
  360. * @return \LDAP\Connection The LDAP connection
  361. */
  362. public function getNewLDAPConnection($uid) {
  363. return $this->handleRequest($uid, 'getNewLDAPConnection', [$uid]);
  364. }
  365. /**
  366. * Creates a new user in LDAP
  367. *
  368. * @param $username
  369. * @param $password
  370. * @return bool
  371. */
  372. public function createUser($username, $password) {
  373. return $this->handleRequest($username, 'createUser', [$username, $password]);
  374. }
  375. public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool {
  376. return $this->handleRequest($uid, 'isUserEnabled', [$uid, $queryDatabaseValue]);
  377. }
  378. public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool {
  379. return $this->handleRequest($uid, 'setUserEnabled', [$uid, $enabled, $queryDatabaseValue, $setDatabaseValue]);
  380. }
  381. public function getDisabledUserList(?int $limit = null, int $offset = 0, string $search = ''): array {
  382. if ((int)$this->getAccess(array_key_first($this->backends) ?? '')->connection->markRemnantsAsDisabled !== 1) {
  383. return [];
  384. }
  385. $disabledUsers = $this->deletedUsersIndex->getUsers();
  386. if ($search !== '') {
  387. $disabledUsers = array_filter(
  388. $disabledUsers,
  389. fn (OfflineUser $user): bool =>
  390. mb_stripos($user->getOCName(), $search) !== false ||
  391. mb_stripos($user->getUID(), $search) !== false ||
  392. mb_stripos($user->getDisplayName(), $search) !== false ||
  393. mb_stripos($user->getEmail(), $search) !== false,
  394. );
  395. }
  396. return array_map(
  397. fn (OfflineUser $user) => $user->getOCName(),
  398. array_slice(
  399. $disabledUsers,
  400. $offset,
  401. $limit
  402. )
  403. );
  404. }
  405. public function getUserCreationDate(string $uid): ?\DateTime {
  406. return $this->handleRequest($uid, 'getUserCreationDate', [$uid]) ?: null;
  407. }
  408. }