1
0

User_Proxy.php 12 KB

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