user_proxy.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\user_ldap;
  26. use OCA\user_ldap\lib\ILDAPWrapper;
  27. use OCA\User_LDAP\lib\User\User;
  28. use \OCA\user_ldap\User_LDAP;
  29. use OCP\IConfig;
  30. class User_Proxy extends lib\Proxy implements \OCP\IUserBackend, \OCP\UserInterface {
  31. private $backends = array();
  32. private $refBackend = null;
  33. /**
  34. * Constructor
  35. * @param array $serverConfigPrefixes array containing the config Prefixes
  36. */
  37. public function __construct(array $serverConfigPrefixes, ILDAPWrapper $ldap, IConfig $ocConfig) {
  38. parent::__construct($ldap);
  39. foreach($serverConfigPrefixes as $configPrefix) {
  40. $this->backends[$configPrefix] =
  41. new User_LDAP($this->getAccess($configPrefix), $ocConfig);
  42. if(is_null($this->refBackend)) {
  43. $this->refBackend = &$this->backends[$configPrefix];
  44. }
  45. }
  46. }
  47. /**
  48. * Tries the backends one after the other until a positive result is returned from the specified method
  49. * @param string $uid the uid connected to the request
  50. * @param string $method the method of the user backend that shall be called
  51. * @param array $parameters an array of parameters to be passed
  52. * @return mixed the result of the method or false
  53. */
  54. protected function walkBackends($uid, $method, $parameters) {
  55. $cacheKey = $this->getUserCacheKey($uid);
  56. foreach($this->backends as $configPrefix => $backend) {
  57. $instance = $backend;
  58. if(!method_exists($instance, $method)
  59. && method_exists($this->getAccess($configPrefix), $method)) {
  60. $instance = $this->getAccess($configPrefix);
  61. }
  62. if($result = call_user_func_array(array($instance, $method), $parameters)) {
  63. $this->writeToCache($cacheKey, $configPrefix);
  64. return $result;
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * Asks the backend connected to the server that supposely takes care of the uid from the request.
  71. * @param string $uid the uid connected to the request
  72. * @param string $method the method of the user backend that shall be called
  73. * @param array $parameters an array of parameters to be passed
  74. * @param mixed $passOnWhen the result matches this variable
  75. * @return mixed the result of the method or false
  76. */
  77. protected function callOnLastSeenOn($uid, $method, $parameters, $passOnWhen) {
  78. $cacheKey = $this->getUserCacheKey($uid);
  79. $prefix = $this->getFromCache($cacheKey);
  80. //in case the uid has been found in the past, try this stored connection first
  81. if(!is_null($prefix)) {
  82. if(isset($this->backends[$prefix])) {
  83. $instance = $this->backends[$prefix];
  84. if(!method_exists($instance, $method)
  85. && method_exists($this->getAccess($prefix), $method)) {
  86. $instance = $this->getAccess($prefix);
  87. }
  88. $result = call_user_func_array(array($instance, $method), $parameters);
  89. if($result === $passOnWhen) {
  90. //not found here, reset cache to null if user vanished
  91. //because sometimes methods return false with a reason
  92. $userExists = call_user_func_array(
  93. array($this->backends[$prefix], 'userExists'),
  94. array($uid)
  95. );
  96. if(!$userExists) {
  97. $this->writeToCache($cacheKey, null);
  98. }
  99. }
  100. return $result;
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Check if backend implements actions
  107. * @param int $actions bitwise-or'ed actions
  108. * @return boolean
  109. *
  110. * Returns the supported actions as int to be
  111. * compared with OC_USER_BACKEND_CREATE_USER etc.
  112. */
  113. public function implementsActions($actions) {
  114. //it's the same across all our user backends obviously
  115. return $this->refBackend->implementsActions($actions);
  116. }
  117. /**
  118. * Backend name to be shown in user management
  119. * @return string the name of the backend to be shown
  120. */
  121. public function getBackendName() {
  122. return $this->refBackend->getBackendName();
  123. }
  124. /**
  125. * Get a list of all users
  126. * @return string[] with all uids
  127. *
  128. * Get a list of all users.
  129. */
  130. public function getUsers($search = '', $limit = 10, $offset = 0) {
  131. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  132. $users = array();
  133. foreach($this->backends as $backend) {
  134. $backendUsers = $backend->getUsers($search, $limit, $offset);
  135. if (is_array($backendUsers)) {
  136. $users = array_merge($users, $backendUsers);
  137. }
  138. }
  139. return $users;
  140. }
  141. /**
  142. * check if a user exists
  143. * @param string $uid the username
  144. * @return boolean
  145. */
  146. public function userExists($uid) {
  147. return $this->handleRequest($uid, 'userExists', array($uid));
  148. }
  149. /**
  150. * check if a user exists on LDAP
  151. * @param string|OCA\User_LDAP\lib\User\User $user either the ownCloud user
  152. * name or an instance of that user
  153. * @return boolean
  154. */
  155. public function userExistsOnLDAP($user) {
  156. $id = ($user instanceof User) ? $user->getUsername() : $user;
  157. return $this->handleRequest($id, 'userExistsOnLDAP', array($user));
  158. }
  159. /**
  160. * Check if the password is correct
  161. * @param string $uid The username
  162. * @param string $password The password
  163. * @return bool
  164. *
  165. * Check if the password is correct without logging in the user
  166. */
  167. public function checkPassword($uid, $password) {
  168. return $this->handleRequest($uid, 'checkPassword', array($uid, $password));
  169. }
  170. /**
  171. * get the user's home directory
  172. * @param string $uid the username
  173. * @return boolean
  174. */
  175. public function getHome($uid) {
  176. return $this->handleRequest($uid, 'getHome', array($uid));
  177. }
  178. /**
  179. * get display name of the user
  180. * @param string $uid user ID of the user
  181. * @return string display name
  182. */
  183. public function getDisplayName($uid) {
  184. return $this->handleRequest($uid, 'getDisplayName', array($uid));
  185. }
  186. /**
  187. * checks whether the user is allowed to change his avatar in ownCloud
  188. * @param string $uid the ownCloud user name
  189. * @return boolean either the user can or cannot
  190. */
  191. public function canChangeAvatar($uid) {
  192. return $this->handleRequest($uid, 'canChangeAvatar', array($uid), true);
  193. }
  194. /**
  195. * Get a list of all display names
  196. * @return array with all displayNames (value) and the corresponding uids (key)
  197. *
  198. * Get a list of all display names and user ids.
  199. */
  200. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  201. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  202. $users = array();
  203. foreach($this->backends as $backend) {
  204. $backendUsers = $backend->getDisplayNames($search, $limit, $offset);
  205. if (is_array($backendUsers)) {
  206. $users = $users + $backendUsers;
  207. }
  208. }
  209. return $users;
  210. }
  211. /**
  212. * delete a user
  213. * @param string $uid The username of the user to delete
  214. * @return bool
  215. *
  216. * Deletes a user
  217. */
  218. public function deleteUser($uid) {
  219. return $this->handleRequest($uid, 'deleteUser', array($uid));
  220. }
  221. /**
  222. * @return bool
  223. */
  224. public function hasUserListings() {
  225. return $this->refBackend->hasUserListings();
  226. }
  227. /**
  228. * Count the number of users
  229. * @return int|bool
  230. */
  231. public function countUsers() {
  232. $users = false;
  233. foreach($this->backends as $backend) {
  234. $backendUsers = $backend->countUsers();
  235. if ($backendUsers !== false) {
  236. $users += $backendUsers;
  237. }
  238. }
  239. return $users;
  240. }
  241. }