1
0

User_Proxy.php 12 KB

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