PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/user_ldap/group_proxy.php

https://github.com/sezuan/core
PHP | 201 lines | 96 code | 20 blank | 85 comment | 10 complexity | c218b922cbf6bb58b64a05e36175da90 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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 Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
  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\GROUP_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 $gid string, the gid connected to the request
  44. * @param $method string, the method of the group 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($gid, $method, $parameters) {
  49. $cacheKey = $this->getGroupCacheKey($gid);
  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 gid from the request.
  60. * @param $gid string, the gid connected to the request
  61. * @param $method string, the method of the group 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($gid, $method, $parameters) {
  66. $cacheKey = $this->getGroupCacheKey($gid);;
  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 group vanished
  74. //because sometimes methods return false with a reason
  75. $groupExists = call_user_func_array(
  76. array($this->backends[$prefix], 'groupExists'),
  77. array($gid)
  78. );
  79. if(!$groupExists) {
  80. $this->writeToCache($cacheKey, null);
  81. }
  82. }
  83. return $result;
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * @brief is user in group?
  90. * @param $uid uid of the user
  91. * @param $gid gid of the group
  92. * @returns true/false
  93. *
  94. * Checks whether the user is member of a group or not.
  95. */
  96. public function inGroup($uid, $gid) {
  97. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  98. }
  99. /**
  100. * @brief Get all groups a user belongs to
  101. * @param $uid Name of the user
  102. * @returns array with group names
  103. *
  104. * This function fetches all groups a user belongs to. It does not check
  105. * if the user exists at all.
  106. */
  107. public function getUserGroups($uid) {
  108. $groups = array();
  109. foreach($this->backends as $backend) {
  110. $backendGroups = $backend->getUserGroups($uid);
  111. if (is_array($backendGroups)) {
  112. $groups = array_merge($groups, $backendGroups);
  113. }
  114. }
  115. return $groups;
  116. }
  117. /**
  118. * @brief get a list of all users in a group
  119. * @returns array with user ids
  120. */
  121. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  122. $users = array();
  123. foreach($this->backends as $backend) {
  124. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  125. if (is_array($backendUsers)) {
  126. $users = array_merge($users, $backendUsers);
  127. }
  128. }
  129. return $users;
  130. }
  131. /**
  132. * @brief get a list of all display names in a group
  133. * @returns array with display names (value) and user ids(key)
  134. */
  135. public function displayNamesInGroup($gid, $search, $limit, $offset) {
  136. $displayNames = array();
  137. foreach($this->backends as $backend) {
  138. $backendUsers = $backend->displayNamesInGroup($gid, $search, $limit, $offset);
  139. if (is_array($backendUsers)) {
  140. $displayNames = array_merge($displayNames, $backendUsers);
  141. }
  142. }
  143. return $displayNames;
  144. }
  145. /**
  146. * @brief get a list of all groups
  147. * @returns array with group names
  148. *
  149. * Returns a list with all groups
  150. */
  151. public function getGroups($search = '', $limit = -1, $offset = 0) {
  152. $groups = array();
  153. foreach($this->backends as $backend) {
  154. $backendGroups = $backend->getGroups($search, $limit, $offset);
  155. if (is_array($backendGroups)) {
  156. $groups = array_merge($groups, $backendGroups);
  157. }
  158. }
  159. return $groups;
  160. }
  161. /**
  162. * check if a group exists
  163. * @param string $gid
  164. * @return bool
  165. */
  166. public function groupExists($gid) {
  167. return $this->handleRequest($gid, 'groupExists', array($gid));
  168. }
  169. /**
  170. * @brief Check if backend implements actions
  171. * @param $actions bitwise-or'ed actions
  172. * @returns boolean
  173. *
  174. * Returns the supported actions as int to be
  175. * compared with OC_USER_BACKEND_CREATE_USER etc.
  176. */
  177. public function implementsActions($actions) {
  178. //it's the same across all our user backends obviously
  179. return $this->refBackend->implementsActions($actions);
  180. }
  181. }