1
0

User_Proxy.php 13 KB

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