User_Proxy.php 13 KB

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