group_proxy.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\user_ldap;
  25. use OCA\user_ldap\lib\ILDAPWrapper;
  26. class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
  27. private $backends = array();
  28. private $refBackend = null;
  29. /**
  30. * Constructor
  31. * @param string[] $serverConfigPrefixes array containing the config Prefixes
  32. */
  33. public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
  34. parent::__construct($ldap);
  35. foreach($serverConfigPrefixes as $configPrefix) {
  36. $this->backends[$configPrefix] =
  37. new \OCA\user_ldap\GROUP_LDAP($this->getAccess($configPrefix));
  38. if(is_null($this->refBackend)) {
  39. $this->refBackend = &$this->backends[$configPrefix];
  40. }
  41. }
  42. }
  43. /**
  44. * Tries the backends one after the other until a positive result is returned from the specified method
  45. * @param string $gid the gid connected to the request
  46. * @param string $method the method of the group backend that shall be called
  47. * @param array $parameters an array of parameters to be passed
  48. * @return mixed, the result of the method or false
  49. */
  50. protected function walkBackends($gid, $method, $parameters) {
  51. $cacheKey = $this->getGroupCacheKey($gid);
  52. foreach($this->backends as $configPrefix => $backend) {
  53. if($result = call_user_func_array(array($backend, $method), $parameters)) {
  54. $this->writeToCache($cacheKey, $configPrefix);
  55. return $result;
  56. }
  57. }
  58. return false;
  59. }
  60. /**
  61. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  62. * @param string $gid the gid connected to the request
  63. * @param string $method the method of the group backend that shall be called
  64. * @param array $parameters an array of parameters to be passed
  65. * @param mixed $passOnWhen the result matches this variable
  66. * @return mixed, the result of the method or false
  67. */
  68. protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
  69. $cacheKey = $this->getGroupCacheKey($gid);;
  70. $prefix = $this->getFromCache($cacheKey);
  71. //in case the uid has been found in the past, try this stored connection first
  72. if(!is_null($prefix)) {
  73. if(isset($this->backends[$prefix])) {
  74. $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
  75. if($result === $passOnWhen) {
  76. //not found here, reset cache to null if group vanished
  77. //because sometimes methods return false with a reason
  78. $groupExists = call_user_func_array(
  79. array($this->backends[$prefix], 'groupExists'),
  80. array($gid)
  81. );
  82. if(!$groupExists) {
  83. $this->writeToCache($cacheKey, null);
  84. }
  85. }
  86. return $result;
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * is user in group?
  93. * @param string $uid uid of the user
  94. * @param string $gid gid of the group
  95. * @return bool
  96. *
  97. * Checks whether the user is member of a group or not.
  98. */
  99. public function inGroup($uid, $gid) {
  100. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  101. }
  102. /**
  103. * Get all groups a user belongs to
  104. * @param string $uid Name of the user
  105. * @return string[] with group names
  106. *
  107. * This function fetches all groups a user belongs to. It does not check
  108. * if the user exists at all.
  109. */
  110. public function getUserGroups($uid) {
  111. $groups = array();
  112. foreach($this->backends as $backend) {
  113. $backendGroups = $backend->getUserGroups($uid);
  114. if (is_array($backendGroups)) {
  115. $groups = array_merge($groups, $backendGroups);
  116. }
  117. }
  118. return $groups;
  119. }
  120. /**
  121. * get a list of all users in a group
  122. * @return string[] with user ids
  123. */
  124. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  125. $users = array();
  126. foreach($this->backends as $backend) {
  127. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  128. if (is_array($backendUsers)) {
  129. $users = array_merge($users, $backendUsers);
  130. }
  131. }
  132. return $users;
  133. }
  134. /**
  135. * returns the number of users in a group, who match the search term
  136. * @param string $gid the internal group name
  137. * @param string $search optional, a search string
  138. * @return int|bool
  139. */
  140. public function countUsersInGroup($gid, $search = '') {
  141. return $this->handleRequest(
  142. $gid, 'countUsersInGroup', array($gid, $search));
  143. }
  144. /**
  145. * get a list of all groups
  146. * @return string[] with group names
  147. *
  148. * Returns a list with all groups
  149. */
  150. public function getGroups($search = '', $limit = -1, $offset = 0) {
  151. $groups = array();
  152. foreach($this->backends as $backend) {
  153. $backendGroups = $backend->getGroups($search, $limit, $offset);
  154. if (is_array($backendGroups)) {
  155. $groups = array_merge($groups, $backendGroups);
  156. }
  157. }
  158. return $groups;
  159. }
  160. /**
  161. * check if a group exists
  162. * @param string $gid
  163. * @return bool
  164. */
  165. public function groupExists($gid) {
  166. return $this->handleRequest($gid, 'groupExists', array($gid));
  167. }
  168. /**
  169. * Check if backend implements actions
  170. * @param int $actions bitwise-or'ed actions
  171. * @return boolean
  172. *
  173. * Returns the supported actions as int to be
  174. * compared with OC_USER_BACKEND_CREATE_USER etc.
  175. */
  176. public function implementsActions($actions) {
  177. //it's the same across all our user backends obviously
  178. return $this->refBackend->implementsActions($actions);
  179. }
  180. }