1
0

User_Proxy.php 11 KB

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