User_Proxy.php 12 KB

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