group_proxy.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  74. $this->writeToCache($cacheKey, null);
  75. }
  76. return $result;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. * @brief is user in group?
  83. * @param $uid uid of the user
  84. * @param $gid gid of the group
  85. * @returns true/false
  86. *
  87. * Checks whether the user is member of a group or not.
  88. */
  89. public function inGroup($uid, $gid) {
  90. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  91. }
  92. /**
  93. * @brief Get all groups a user belongs to
  94. * @param $uid Name of the user
  95. * @returns array with group names
  96. *
  97. * This function fetches all groups a user belongs to. It does not check
  98. * if the user exists at all.
  99. */
  100. public function getUserGroups($uid) {
  101. $groups = array();
  102. foreach($this->backends as $backend) {
  103. $backendGroups = $backend->getUserGroups($uid);
  104. if (is_array($backendGroups)) {
  105. $groups = array_merge($groups, $backendGroups);
  106. }
  107. }
  108. return $groups;
  109. }
  110. /**
  111. * @brief get a list of all users in a group
  112. * @returns array with user ids
  113. */
  114. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  115. $users = array();
  116. foreach($this->backends as $backend) {
  117. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  118. if (is_array($backendUsers)) {
  119. $users = array_merge($users, $backendUsers);
  120. }
  121. }
  122. return $users;
  123. }
  124. /**
  125. * @brief get a list of all groups
  126. * @returns array with group names
  127. *
  128. * Returns a list with all groups
  129. */
  130. public function getGroups($search = '', $limit = -1, $offset = 0) {
  131. $groups = array();
  132. foreach($this->backends as $backend) {
  133. $backendGroups = $backend->getGroups($search, $limit, $offset);
  134. if (is_array($backendGroups)) {
  135. $groups = array_merge($groups, $backendGroups);
  136. }
  137. }
  138. return $groups;
  139. }
  140. /**
  141. * check if a group exists
  142. * @param string $gid
  143. * @return bool
  144. */
  145. public function groupExists($gid) {
  146. return $this->handleRequest($gid, 'groupExists', array($gid));
  147. }
  148. /**
  149. * @brief Check if backend implements actions
  150. * @param $actions bitwise-or'ed actions
  151. * @returns boolean
  152. *
  153. * Returns the supported actions as int to be
  154. * compared with OC_USER_BACKEND_CREATE_USER etc.
  155. */
  156. public function implementsActions($actions) {
  157. //it's the same across all our user backends obviously
  158. return $this->refBackend->implementsActions($actions);
  159. }
  160. }