User_Proxy.php 13 KB

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