您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

User_Proxy.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. */
  196. public function userExistsOnLDAP($user, bool $ignoreCache = false): bool {
  197. $id = ($user instanceof User) ? $user->getUsername() : $user;
  198. return $this->handleRequest($id, 'userExistsOnLDAP', [$user, $ignoreCache]);
  199. }
  200. /**
  201. * Check if the password is correct
  202. *
  203. * @param string $uid The username
  204. * @param string $password The password
  205. * @return bool
  206. *
  207. * Check if the password is correct without logging in the user
  208. */
  209. public function checkPassword($uid, $password) {
  210. return $this->handleRequest($uid, 'checkPassword', [$uid, $password]);
  211. }
  212. /**
  213. * returns the username for the given login name, if available
  214. *
  215. * @param string $loginName
  216. * @return string|false
  217. */
  218. public function loginName2UserName($loginName) {
  219. $id = 'LOGINNAME,' . $loginName;
  220. return $this->handleRequest($id, 'loginName2UserName', [$loginName]);
  221. }
  222. /**
  223. * returns the username for the given LDAP DN, if available
  224. *
  225. * @param string $dn
  226. * @return string|false with the username
  227. */
  228. public function dn2UserName($dn) {
  229. $id = 'DN,' . $dn;
  230. return $this->handleRequest($id, 'dn2UserName', [$dn]);
  231. }
  232. /**
  233. * get the user's home directory
  234. *
  235. * @param string $uid the username
  236. * @return boolean
  237. */
  238. public function getHome($uid) {
  239. return $this->handleRequest($uid, 'getHome', [$uid]);
  240. }
  241. /**
  242. * get display name of the user
  243. *
  244. * @param string $uid user ID of the user
  245. * @return string display name
  246. */
  247. public function getDisplayName($uid) {
  248. return $this->handleRequest($uid, 'getDisplayName', [$uid]);
  249. }
  250. /**
  251. * set display name of the user
  252. *
  253. * @param string $uid user ID of the user
  254. * @param string $displayName new display name
  255. * @return string display name
  256. */
  257. public function setDisplayName($uid, $displayName) {
  258. return $this->handleRequest($uid, 'setDisplayName', [$uid, $displayName]);
  259. }
  260. /**
  261. * checks whether the user is allowed to change his avatar in Nextcloud
  262. *
  263. * @param string $uid the Nextcloud user name
  264. * @return boolean either the user can or cannot
  265. */
  266. public function canChangeAvatar($uid) {
  267. return $this->handleRequest($uid, 'canChangeAvatar', [$uid], true);
  268. }
  269. /**
  270. * Get a list of all display names and user ids.
  271. *
  272. * @param string $search
  273. * @param int|null $limit
  274. * @param int|null $offset
  275. * @return array an array of all displayNames (value) and the corresponding uids (key)
  276. */
  277. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  278. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  279. $users = [];
  280. foreach ($this->backends as $backend) {
  281. $backendUsers = $backend->getDisplayNames($search, $limit, $offset);
  282. if (is_array($backendUsers)) {
  283. $users = $users + $backendUsers;
  284. }
  285. }
  286. return $users;
  287. }
  288. /**
  289. * delete a user
  290. *
  291. * @param string $uid The username of the user to delete
  292. * @return bool
  293. *
  294. * Deletes a user
  295. */
  296. public function deleteUser($uid) {
  297. return $this->handleRequest($uid, 'deleteUser', [$uid]);
  298. }
  299. /**
  300. * Set password
  301. *
  302. * @param string $uid The username
  303. * @param string $password The new password
  304. * @return bool
  305. *
  306. */
  307. public function setPassword($uid, $password) {
  308. return $this->handleRequest($uid, 'setPassword', [$uid, $password]);
  309. }
  310. /**
  311. * @return bool
  312. */
  313. public function hasUserListings() {
  314. return $this->refBackend->hasUserListings();
  315. }
  316. /**
  317. * Count the number of users
  318. *
  319. * @return int|bool
  320. */
  321. public function countUsers() {
  322. $users = false;
  323. foreach ($this->backends as $backend) {
  324. $backendUsers = $backend->countUsers();
  325. if ($backendUsers !== false) {
  326. $users += $backendUsers;
  327. }
  328. }
  329. return $users;
  330. }
  331. /**
  332. * Return access for LDAP interaction.
  333. *
  334. * @param string $uid
  335. * @return Access instance of Access for LDAP interaction
  336. */
  337. public function getLDAPAccess($uid) {
  338. return $this->handleRequest($uid, 'getLDAPAccess', [$uid]);
  339. }
  340. /**
  341. * Return a new LDAP connection for the specified user.
  342. * The connection needs to be closed manually.
  343. *
  344. * @param string $uid
  345. * @return resource|\LDAP\Connection The LDAP connection
  346. */
  347. public function getNewLDAPConnection($uid) {
  348. return $this->handleRequest($uid, 'getNewLDAPConnection', [$uid]);
  349. }
  350. /**
  351. * Creates a new user in LDAP
  352. *
  353. * @param $username
  354. * @param $password
  355. * @return bool
  356. */
  357. public function createUser($username, $password) {
  358. return $this->handleRequest($username, 'createUser', [$username, $password]);
  359. }
  360. }