user_proxy.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Artuhr Schiwon
  6. * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\user_ldap;
  23. class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
  24. private $backends = array();
  25. private $refBackend = null;
  26. /**
  27. * @brief Constructor
  28. * @param $serverConfigPrefixes array containing the config Prefixes
  29. */
  30. public function __construct($serverConfigPrefixes) {
  31. parent::__construct();
  32. foreach($serverConfigPrefixes as $configPrefix) {
  33. $this->backends[$configPrefix] = new \OCA\user_ldap\USER_LDAP();
  34. $connector = $this->getConnector($configPrefix);
  35. $this->backends[$configPrefix]->setConnector($connector);
  36. if(is_null($this->refBackend)) {
  37. $this->refBackend = &$this->backends[$configPrefix];
  38. }
  39. }
  40. }
  41. /**
  42. * @brief Tries the backends one after the other until a positive result is returned from the specified method
  43. * @param $uid string, the uid connected to the request
  44. * @param $method string, the method of the user backend that shall be called
  45. * @param $parameters an array of parameters to be passed
  46. * @return mixed, the result of the method or false
  47. */
  48. protected function walkBackends($uid, $method, $parameters) {
  49. $cacheKey = $this->getUserCacheKey($uid);
  50. foreach($this->backends as $configPrefix => $backend) {
  51. if($result = call_user_func_array(array($backend, $method), $parameters)) {
  52. $this->writeToCache($cacheKey, $configPrefix);
  53. return $result;
  54. }
  55. }
  56. return false;
  57. }
  58. /**
  59. * @brief Asks the backend connected to the server that supposely takes care of the uid from the request.
  60. * @param $uid string, the uid connected to the request
  61. * @param $method string, the method of the user backend that shall be called
  62. * @param $parameters an array of parameters to be passed
  63. * @return mixed, the result of the method or false
  64. */
  65. protected function callOnLastSeenOn($uid, $method, $parameters) {
  66. $cacheKey = $this->getUserCacheKey($uid);
  67. $prefix = $this->getFromCache($cacheKey);
  68. //in case the uid has been found in the past, try this stored connection first
  69. if(!is_null($prefix)) {
  70. if(isset($this->backends[$prefix])) {
  71. $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
  72. if(!$result) {
  73. //not found here, reset cache to null if user vanished
  74. //because sometimes methods return false with a reason
  75. $userExists = call_user_func_array(
  76. array($this->backends[$prefix], 'userExists'),
  77. array($uid)
  78. );
  79. if(!$userExists) {
  80. $this->writeToCache($cacheKey, null);
  81. }
  82. }
  83. return $result;
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * @brief Check if backend implements actions
  90. * @param $actions bitwise-or'ed actions
  91. * @returns boolean
  92. *
  93. * Returns the supported actions as int to be
  94. * compared with OC_USER_BACKEND_CREATE_USER etc.
  95. */
  96. public function implementsActions($actions) {
  97. //it's the same across all our user backends obviously
  98. return $this->refBackend->implementsActions($actions);
  99. }
  100. /**
  101. * @brief Get a list of all users
  102. * @returns array with all uids
  103. *
  104. * Get a list of all users.
  105. */
  106. public function getUsers($search = '', $limit = 10, $offset = 0) {
  107. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  108. $users = array();
  109. foreach($this->backends as $backend) {
  110. $backendUsers = $backend->getUsers($search, $limit, $offset);
  111. if (is_array($backendUsers)) {
  112. $users = array_merge($users, $backendUsers);
  113. }
  114. }
  115. return $users;
  116. }
  117. /**
  118. * @brief check if a user exists
  119. * @param string $uid the username
  120. * @return boolean
  121. */
  122. public function userExists($uid) {
  123. return $this->handleRequest($uid, 'userExists', array($uid));
  124. }
  125. /**
  126. * @brief Check if the password is correct
  127. * @param $uid The username
  128. * @param $password The password
  129. * @returns true/false
  130. *
  131. * Check if the password is correct without logging in the user
  132. */
  133. public function checkPassword($uid, $password) {
  134. return $this->handleRequest($uid, 'checkPassword', array($uid, $password));
  135. }
  136. /**
  137. * @brief get the user's home directory
  138. * @param string $uid the username
  139. * @return boolean
  140. */
  141. public function getHome($uid) {
  142. return $this->handleRequest($uid, 'getHome', array($uid));
  143. }
  144. /**
  145. * @brief get display name of the user
  146. * @param $uid user ID of the user
  147. * @return display name
  148. */
  149. public function getDisplayName($uid) {
  150. return $this->handleRequest($uid, 'getDisplayName', array($uid));
  151. }
  152. /**
  153. * @brief Get a list of all display names
  154. * @returns array with all displayNames (value) and the corresponding uids (key)
  155. *
  156. * Get a list of all display names and user ids.
  157. */
  158. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  159. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  160. $users = array();
  161. foreach($this->backends as $backend) {
  162. $backendUsers = $backend->getDisplayNames($search, $limit, $offset);
  163. if (is_array($backendUsers)) {
  164. $users = $users + $backendUsers;
  165. }
  166. }
  167. return $users;
  168. }
  169. /**
  170. * @brief delete a user
  171. * @param $uid The username of the user to delete
  172. * @returns true/false
  173. *
  174. * Deletes a user
  175. */
  176. public function deleteUser($uid) {
  177. return false;
  178. }
  179. /**
  180. * @return bool
  181. */
  182. public function hasUserListings() {
  183. return $this->refBackend->hasUserListings();
  184. }
  185. }