user_proxy.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. use OCA\user_ldap\lib\ILDAPWrapper;
  24. class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
  25. private $backends = array();
  26. private $refBackend = null;
  27. /**
  28. * Constructor
  29. * @param array $serverConfigPrefixes array containing the config Prefixes
  30. */
  31. public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
  32. parent::__construct($ldap);
  33. foreach($serverConfigPrefixes as $configPrefix) {
  34. $this->backends[$configPrefix] =
  35. new \OCA\user_ldap\USER_LDAP($this->getAccess($configPrefix));
  36. if(is_null($this->refBackend)) {
  37. $this->refBackend = &$this->backends[$configPrefix];
  38. }
  39. }
  40. }
  41. /**
  42. * Tries the backends one after the other until a positive result is returned from the specified method
  43. * @param string $uid the uid connected to the request
  44. * @param string $method the method of the user backend that shall be called
  45. * @param array $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. $instance = $backend;
  52. if(!method_exists($instance, $method)
  53. && method_exists($this->getAccess($configPrefix), $method)) {
  54. $instance = $this->getAccess($configPrefix);
  55. }
  56. if($result = call_user_func_array(array($instance, $method), $parameters)) {
  57. $this->writeToCache($cacheKey, $configPrefix);
  58. return $result;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Asks the backend connected to the server that supposely takes care of the uid from the request.
  65. * @param string $uid the uid connected to the request
  66. * @param string $method the method of the user backend that shall be called
  67. * @param array $parameters an array of parameters to be passed
  68. * @param mixed $passOnWhen the result matches this variable
  69. * @return mixed the result of the method or false
  70. */
  71. protected function callOnLastSeenOn($uid, $method, $parameters, $passOnWhen) {
  72. $cacheKey = $this->getUserCacheKey($uid);
  73. $prefix = $this->getFromCache($cacheKey);
  74. //in case the uid has been found in the past, try this stored connection first
  75. if(!is_null($prefix)) {
  76. if(isset($this->backends[$prefix])) {
  77. $instance = $this->backends[$prefix];
  78. if(!method_exists($instance, $method)
  79. && method_exists($this->getAccess($prefix), $method)) {
  80. $instance = $this->getAccess($prefix);
  81. }
  82. $result = call_user_func_array(array($instance, $method), $parameters);
  83. if($result === $passOnWhen) {
  84. //not found here, reset cache to null if user vanished
  85. //because sometimes methods return false with a reason
  86. $userExists = call_user_func_array(
  87. array($this->backends[$prefix], 'userExists'),
  88. array($uid)
  89. );
  90. if(!$userExists) {
  91. $this->writeToCache($cacheKey, null);
  92. }
  93. }
  94. return $result;
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * Check if backend implements actions
  101. * @param int $actions bitwise-or'ed actions
  102. * @return boolean
  103. *
  104. * Returns the supported actions as int to be
  105. * compared with OC_USER_BACKEND_CREATE_USER etc.
  106. */
  107. public function implementsActions($actions) {
  108. //it's the same across all our user backends obviously
  109. return $this->refBackend->implementsActions($actions);
  110. }
  111. /**
  112. * Get a list of all users
  113. * @return string[] with all uids
  114. *
  115. * Get a list of all users.
  116. */
  117. public function getUsers($search = '', $limit = 10, $offset = 0) {
  118. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  119. $users = array();
  120. foreach($this->backends as $backend) {
  121. $backendUsers = $backend->getUsers($search, $limit, $offset);
  122. if (is_array($backendUsers)) {
  123. $users = array_merge($users, $backendUsers);
  124. }
  125. }
  126. return $users;
  127. }
  128. /**
  129. * check if a user exists
  130. * @param string $uid the username
  131. * @return boolean
  132. */
  133. public function userExists($uid) {
  134. return $this->handleRequest($uid, 'userExists', array($uid));
  135. }
  136. /**
  137. * Check if the password is correct
  138. * @param string $uid The username
  139. * @param string $password The password
  140. * @return bool
  141. *
  142. * Check if the password is correct without logging in the user
  143. */
  144. public function checkPassword($uid, $password) {
  145. return $this->handleRequest($uid, 'checkPassword', array($uid, $password));
  146. }
  147. /**
  148. * get the user's home directory
  149. * @param string $uid the username
  150. * @return boolean
  151. */
  152. public function getHome($uid) {
  153. return $this->handleRequest($uid, 'getHome', array($uid));
  154. }
  155. /**
  156. * get display name of the user
  157. * @param string $uid user ID of the user
  158. * @return string display name
  159. */
  160. public function getDisplayName($uid) {
  161. return $this->handleRequest($uid, 'getDisplayName', array($uid));
  162. }
  163. /**
  164. * checks whether the user is allowed to change his avatar in ownCloud
  165. * @param string $uid the ownCloud user name
  166. * @return boolean either the user can or cannot
  167. */
  168. public function canChangeAvatar($uid) {
  169. return $this->handleRequest($uid, 'canChangeAvatar', array($uid), true);
  170. }
  171. /**
  172. * Get a list of all display names
  173. * @return array with all displayNames (value) and the corresponding uids (key)
  174. *
  175. * Get a list of all display names and user ids.
  176. */
  177. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  178. //we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
  179. $users = array();
  180. foreach($this->backends as $backend) {
  181. $backendUsers = $backend->getDisplayNames($search, $limit, $offset);
  182. if (is_array($backendUsers)) {
  183. $users = $users + $backendUsers;
  184. }
  185. }
  186. return $users;
  187. }
  188. /**
  189. * delete a user
  190. * @param string $uid The username of the user to delete
  191. * @return bool
  192. *
  193. * Deletes a user
  194. */
  195. public function deleteUser($uid) {
  196. return false;
  197. }
  198. /**
  199. * @return bool
  200. */
  201. public function hasUserListings() {
  202. return $this->refBackend->hasUserListings();
  203. }
  204. /**
  205. * Count the number of users
  206. * @return int|bool
  207. */
  208. public function countUsers() {
  209. $users = false;
  210. foreach($this->backends as $backend) {
  211. $backendUsers = $backend->countUsers();
  212. if ($backendUsers !== false) {
  213. $users += $backendUsers;
  214. }
  215. }
  216. return $users;
  217. }
  218. }