Group_Proxy.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP;
  27. class Group_Proxy extends Proxy implements \OCP\GroupInterface {
  28. private $backends = array();
  29. private $refBackend = null;
  30. /**
  31. * Constructor
  32. * @param string[] $serverConfigPrefixes array containing the config Prefixes
  33. */
  34. public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
  35. parent::__construct($ldap);
  36. foreach($serverConfigPrefixes as $configPrefix) {
  37. $this->backends[$configPrefix] =
  38. new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix));
  39. if(is_null($this->refBackend)) {
  40. $this->refBackend = &$this->backends[$configPrefix];
  41. }
  42. }
  43. }
  44. /**
  45. * Tries the backends one after the other until a positive result is returned from the specified method
  46. * @param string $gid the gid connected to the request
  47. * @param string $method the method of the group backend that shall be called
  48. * @param array $parameters an array of parameters to be passed
  49. * @return mixed, the result of the method or false
  50. */
  51. protected function walkBackends($gid, $method, $parameters) {
  52. $cacheKey = $this->getGroupCacheKey($gid);
  53. foreach($this->backends as $configPrefix => $backend) {
  54. if($result = call_user_func_array(array($backend, $method), $parameters)) {
  55. $this->writeToCache($cacheKey, $configPrefix);
  56. return $result;
  57. }
  58. }
  59. return false;
  60. }
  61. /**
  62. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  63. * @param string $gid the gid connected to the request
  64. * @param string $method the method of the group backend that shall be called
  65. * @param array $parameters an array of parameters to be passed
  66. * @param mixed $passOnWhen the result matches this variable
  67. * @return mixed, the result of the method or false
  68. */
  69. protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
  70. $cacheKey = $this->getGroupCacheKey($gid);;
  71. $prefix = $this->getFromCache($cacheKey);
  72. //in case the uid has been found in the past, try this stored connection first
  73. if(!is_null($prefix)) {
  74. if(isset($this->backends[$prefix])) {
  75. $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
  76. if($result === $passOnWhen) {
  77. //not found here, reset cache to null if group vanished
  78. //because sometimes methods return false with a reason
  79. $groupExists = call_user_func_array(
  80. array($this->backends[$prefix], 'groupExists'),
  81. array($gid)
  82. );
  83. if(!$groupExists) {
  84. $this->writeToCache($cacheKey, null);
  85. }
  86. }
  87. return $result;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * is user in group?
  94. * @param string $uid uid of the user
  95. * @param string $gid gid of the group
  96. * @return bool
  97. *
  98. * Checks whether the user is member of a group or not.
  99. */
  100. public function inGroup($uid, $gid) {
  101. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  102. }
  103. /**
  104. * Get all groups a user belongs to
  105. * @param string $uid Name of the user
  106. * @return string[] with group names
  107. *
  108. * This function fetches all groups a user belongs to. It does not check
  109. * if the user exists at all.
  110. */
  111. public function getUserGroups($uid) {
  112. $groups = array();
  113. foreach($this->backends as $backend) {
  114. $backendGroups = $backend->getUserGroups($uid);
  115. if (is_array($backendGroups)) {
  116. $groups = array_merge($groups, $backendGroups);
  117. }
  118. }
  119. return $groups;
  120. }
  121. /**
  122. * get a list of all users in a group
  123. * @return string[] with user ids
  124. */
  125. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  126. $users = array();
  127. foreach($this->backends as $backend) {
  128. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  129. if (is_array($backendUsers)) {
  130. $users = array_merge($users, $backendUsers);
  131. }
  132. }
  133. return $users;
  134. }
  135. /**
  136. * returns the number of users in a group, who match the search term
  137. * @param string $gid the internal group name
  138. * @param string $search optional, a search string
  139. * @return int|bool
  140. */
  141. public function countUsersInGroup($gid, $search = '') {
  142. return $this->handleRequest(
  143. $gid, 'countUsersInGroup', array($gid, $search));
  144. }
  145. /**
  146. * get a list of all groups
  147. * @return string[] 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. * Check if backend implements actions
  171. * @param int $actions bitwise-or'ed actions
  172. * @return 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. /**
  182. * Return access for LDAP interaction.
  183. * @param string $gid
  184. * @return Access instance of Access for LDAP interaction
  185. */
  186. public function getLDAPAccess($gid) {
  187. return $this->handleRequest($gid, 'getLDAPAccess', []);
  188. }
  189. }