User_Proxy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. use OCP\User\Backend\ICountUsersBackend;
  38. class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP, ICountUsersBackend {
  39. private $backends = [];
  40. /** @var User_LDAP */
  41. private $refBackend = null;
  42. public function __construct(
  43. Helper $helper,
  44. ILDAPWrapper $ldap,
  45. IConfig $ocConfig,
  46. INotificationManager $notificationManager,
  47. IUserSession $userSession,
  48. UserPluginManager $userPluginManager
  49. ) {
  50. parent::__construct($ldap);
  51. $serverConfigPrefixes = $helper->getServerConfigurationPrefixes(true);
  52. foreach ($serverConfigPrefixes as $configPrefix) {
  53. $this->backends[$configPrefix] =
  54. new User_LDAP($this->getAccess($configPrefix), $ocConfig, $notificationManager, $userSession, $userPluginManager);
  55. if (is_null($this->refBackend)) {
  56. $this->refBackend = &$this->backends[$configPrefix];
  57. }
  58. }
  59. }
  60. /**
  61. * Tries the backends one after the other until a positive result is returned from the specified method
  62. *
  63. * @param string $id the uid connected to the request
  64. * @param string $method the method of the user backend that shall be called
  65. * @param array $parameters an array of parameters to be passed
  66. * @return mixed the result of the method or false
  67. */
  68. protected function walkBackends($id, $method, $parameters) {
  69. $uid = $id;
  70. $cacheKey = $this->getUserCacheKey($uid);
  71. foreach ($this->backends as $configPrefix => $backend) {
  72. $instance = $backend;
  73. if (!method_exists($instance, $method)
  74. && method_exists($this->getAccess($configPrefix), $method)) {
  75. $instance = $this->getAccess($configPrefix);
  76. }
  77. if ($result = call_user_func_array([$instance, $method], $parameters)) {
  78. if (!$this->isSingleBackend()) {
  79. $this->writeToCache($cacheKey, $configPrefix);
  80. }
  81. return $result;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Asks the backend connected to the server that supposely takes care of the uid from the request.
  88. *
  89. * @param string $id the uid connected to the request
  90. * @param string $method the method of the user backend that shall be called
  91. * @param array $parameters an array of parameters to be passed
  92. * @param mixed $passOnWhen the result matches this variable
  93. * @return mixed the result of the method or false
  94. */
  95. protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
  96. $uid = $id;
  97. $cacheKey = $this->getUserCacheKey($uid);
  98. $prefix = $this->getFromCache($cacheKey);
  99. //in case the uid has been found in the past, try this stored connection first
  100. if (!is_null($prefix)) {
  101. if (isset($this->backends[$prefix])) {
  102. $instance = $this->backends[$prefix];
  103. if (!method_exists($instance, $method)
  104. && method_exists($this->getAccess($prefix), $method)) {
  105. $instance = $this->getAccess($prefix);
  106. }
  107. $result = call_user_func_array([$instance, $method], $parameters);
  108. if ($result === $passOnWhen) {
  109. //not found here, reset cache to null if user vanished
  110. //because sometimes methods return false with a reason
  111. $userExists = call_user_func_array(
  112. [$this->backends[$prefix], 'userExistsOnLDAP'],
  113. [$uid]
  114. );
  115. if (!$userExists) {
  116. $this->writeToCache($cacheKey, null);
  117. }
  118. }
  119. return $result;
  120. }
  121. }
  122. return false;
  123. }
  124. protected function activeBackends(): int {
  125. return count($this->backends);
  126. }
  127. /**
  128. * Check if backend implements actions
  129. *
  130. * @param int $actions bitwise-or'ed actions
  131. * @return boolean
  132. *
  133. * Returns the supported actions as int to be
  134. * compared with \OC\User\Backend::CREATE_USER etc.
  135. */
  136. public function implementsActions($actions) {
  137. //it's the same across all our user backends obviously
  138. return $this->refBackend->implementsActions($actions);
  139. }
  140. /**
  141. * Backend name to be shown in user management
  142. *
  143. * @return string the name of the backend to be shown
  144. */
  145. public function getBackendName() {
  146. return $this->refBackend->getBackendName();
  147. }
  148. /**
  149. * Get a list of all users
  150. *
  151. * @param string $search
  152. * @param null|int $limit
  153. * @param null|int $offset
  154. * @return string[] an array of all uids
  155. */
  156. public function getUsers($search = '', $limit = 10, $offset = 0) {
  157. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  158. $users = [];
  159. foreach ($this->backends as $backend) {
  160. $backendUsers = $backend->getUsers($search, $limit, $offset);
  161. if (is_array($backendUsers)) {
  162. $users = array_merge($users, $backendUsers);
  163. }
  164. }
  165. return $users;
  166. }
  167. /**
  168. * check if a user exists
  169. *
  170. * @param string $uid the username
  171. * @return boolean
  172. */
  173. public function userExists($uid) {
  174. $existsOnLDAP = false;
  175. $existsLocally = $this->handleRequest($uid, 'userExists', [$uid]);
  176. if ($existsLocally) {
  177. $existsOnLDAP = $this->userExistsOnLDAP($uid);
  178. }
  179. if ($existsLocally && !$existsOnLDAP) {
  180. try {
  181. $user = $this->getLDAPAccess($uid)->userManager->get($uid);
  182. if ($user instanceof User) {
  183. $user->markUser();
  184. }
  185. } catch (\Exception $e) {
  186. // ignore
  187. }
  188. }
  189. return $existsLocally;
  190. }
  191. /**
  192. * check if a user exists on LDAP
  193. *
  194. * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user
  195. * name or an instance of that user
  196. */
  197. public function userExistsOnLDAP($user, bool $ignoreCache = false): bool {
  198. $id = ($user instanceof User) ? $user->getUsername() : $user;
  199. return $this->handleRequest($id, 'userExistsOnLDAP', [$user, $ignoreCache]);
  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 int|null $limit
  275. * @param int|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|\LDAP\Connection 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. }