User_Proxy.php 12 KB

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