You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User_Proxy.php 11KB

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