group_proxy.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
  25. private $backends = array();
  26. private $refBackend = null;
  27. /**
  28. * Constructor
  29. * @param string[] $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\GROUP_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 $gid the gid connected to the request
  44. * @param string $method the method of the group 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($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. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  60. * @param string $gid the gid connected to the request
  61. * @param string $method the method of the group backend that shall be called
  62. * @param array $parameters an array of parameters to be passed
  63. * @param mixed $passOnWhen the result matches this variable
  64. * @return mixed, the result of the method or false
  65. */
  66. protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
  67. $cacheKey = $this->getGroupCacheKey($gid);;
  68. $prefix = $this->getFromCache($cacheKey);
  69. //in case the uid has been found in the past, try this stored connection first
  70. if(!is_null($prefix)) {
  71. if(isset($this->backends[$prefix])) {
  72. $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
  73. if($result === $passOnWhen) {
  74. //not found here, reset cache to null if group vanished
  75. //because sometimes methods return false with a reason
  76. $groupExists = call_user_func_array(
  77. array($this->backends[$prefix], 'groupExists'),
  78. array($gid)
  79. );
  80. if(!$groupExists) {
  81. $this->writeToCache($cacheKey, null);
  82. }
  83. }
  84. return $result;
  85. }
  86. }
  87. return false;
  88. }
  89. /**
  90. * is user in group?
  91. * @param string $uid uid of the user
  92. * @param string $gid gid of the group
  93. * @return bool
  94. *
  95. * Checks whether the user is member of a group or not.
  96. */
  97. public function inGroup($uid, $gid) {
  98. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  99. }
  100. /**
  101. * Get all groups a user belongs to
  102. * @param string $uid Name of the user
  103. * @return string[] with group names
  104. *
  105. * This function fetches all groups a user belongs to. It does not check
  106. * if the user exists at all.
  107. */
  108. public function getUserGroups($uid) {
  109. $groups = array();
  110. foreach($this->backends as $backend) {
  111. $backendGroups = $backend->getUserGroups($uid);
  112. if (is_array($backendGroups)) {
  113. $groups = array_merge($groups, $backendGroups);
  114. }
  115. }
  116. return $groups;
  117. }
  118. /**
  119. * get a list of all users in a group
  120. * @return string[] with user ids
  121. */
  122. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  123. $users = array();
  124. foreach($this->backends as $backend) {
  125. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  126. if (is_array($backendUsers)) {
  127. $users = array_merge($users, $backendUsers);
  128. }
  129. }
  130. return $users;
  131. }
  132. /**
  133. * returns the number of users in a group, who match the search term
  134. * @param string $gid the internal group name
  135. * @param string $search optional, a search string
  136. * @return int|bool
  137. */
  138. public function countUsersInGroup($gid, $search = '') {
  139. return $this->handleRequest(
  140. $gid, 'countUsersInGroup', array($gid, $search));
  141. }
  142. /**
  143. * get a list of all groups
  144. * @return string[] with group names
  145. *
  146. * Returns a list with all groups
  147. */
  148. public function getGroups($search = '', $limit = -1, $offset = 0) {
  149. $groups = array();
  150. foreach($this->backends as $backend) {
  151. $backendGroups = $backend->getGroups($search, $limit, $offset);
  152. if (is_array($backendGroups)) {
  153. $groups = array_merge($groups, $backendGroups);
  154. }
  155. }
  156. return $groups;
  157. }
  158. /**
  159. * check if a group exists
  160. * @param string $gid
  161. * @return bool
  162. */
  163. public function groupExists($gid) {
  164. return $this->handleRequest($gid, 'groupExists', array($gid));
  165. }
  166. /**
  167. * Check if backend implements actions
  168. * @param int $actions bitwise-or'ed actions
  169. * @return boolean
  170. *
  171. * Returns the supported actions as int to be
  172. * compared with OC_USER_BACKEND_CREATE_USER etc.
  173. */
  174. public function implementsActions($actions) {
  175. //it's the same across all our user backends obviously
  176. return $this->refBackend->implementsActions($actions);
  177. }
  178. }