Group_Proxy.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\User_LDAP;
  28. class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP {
  29. private $backends = array();
  30. private $refBackend = null;
  31. /**
  32. * Constructor
  33. * @param string[] $serverConfigPrefixes array containing the config Prefixes
  34. */
  35. public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) {
  36. parent::__construct($ldap);
  37. foreach($serverConfigPrefixes as $configPrefix) {
  38. $this->backends[$configPrefix] =
  39. new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager);
  40. if(is_null($this->refBackend)) {
  41. $this->refBackend = &$this->backends[$configPrefix];
  42. }
  43. }
  44. }
  45. /**
  46. * Tries the backends one after the other until a positive result is returned from the specified method
  47. * @param string $gid the gid connected to the request
  48. * @param string $method the method of the group backend that shall be called
  49. * @param array $parameters an array of parameters to be passed
  50. * @return mixed, the result of the method or false
  51. */
  52. protected function walkBackends($gid, $method, $parameters) {
  53. $cacheKey = $this->getGroupCacheKey($gid);
  54. foreach($this->backends as $configPrefix => $backend) {
  55. if($result = call_user_func_array(array($backend, $method), $parameters)) {
  56. $this->writeToCache($cacheKey, $configPrefix);
  57. return $result;
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  64. * @param string $gid the gid connected to the request
  65. * @param string $method the method of the group backend that shall be called
  66. * @param array $parameters an array of parameters to be passed
  67. * @param mixed $passOnWhen the result matches this variable
  68. * @return mixed, the result of the method or false
  69. */
  70. protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
  71. $cacheKey = $this->getGroupCacheKey($gid);
  72. $prefix = $this->getFromCache($cacheKey);
  73. //in case the uid has been found in the past, try this stored connection first
  74. if(!is_null($prefix)) {
  75. if(isset($this->backends[$prefix])) {
  76. $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
  77. if($result === $passOnWhen) {
  78. //not found here, reset cache to null if group vanished
  79. //because sometimes methods return false with a reason
  80. $groupExists = call_user_func_array(
  81. array($this->backends[$prefix], 'groupExists'),
  82. array($gid)
  83. );
  84. if(!$groupExists) {
  85. $this->writeToCache($cacheKey, null);
  86. }
  87. }
  88. return $result;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * is user in group?
  95. * @param string $uid uid of the user
  96. * @param string $gid gid of the group
  97. * @return bool
  98. *
  99. * Checks whether the user is member of a group or not.
  100. */
  101. public function inGroup($uid, $gid) {
  102. return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
  103. }
  104. /**
  105. * Get all groups a user belongs to
  106. * @param string $uid Name of the user
  107. * @return string[] with group names
  108. *
  109. * This function fetches all groups a user belongs to. It does not check
  110. * if the user exists at all.
  111. */
  112. public function getUserGroups($uid) {
  113. $groups = array();
  114. foreach($this->backends as $backend) {
  115. $backendGroups = $backend->getUserGroups($uid);
  116. if (is_array($backendGroups)) {
  117. $groups = array_merge($groups, $backendGroups);
  118. }
  119. }
  120. return $groups;
  121. }
  122. /**
  123. * get a list of all users in a group
  124. * @return string[] with user ids
  125. */
  126. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  127. $users = array();
  128. foreach($this->backends as $backend) {
  129. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  130. if (is_array($backendUsers)) {
  131. $users = array_merge($users, $backendUsers);
  132. }
  133. }
  134. return $users;
  135. }
  136. /**
  137. * @param string $gid
  138. * @return bool
  139. */
  140. public function createGroup($gid) {
  141. return $this->handleRequest(
  142. $gid, 'createGroup', array($gid));
  143. }
  144. /**
  145. * delete a group
  146. * @param string $gid gid of the group to delete
  147. * @return bool
  148. */
  149. public function deleteGroup($gid) {
  150. return $this->handleRequest(
  151. $gid, 'deleteGroup', array($gid));
  152. }
  153. /**
  154. * Add a user to a group
  155. * @param string $uid Name of the user to add to group
  156. * @param string $gid Name of the group in which add the user
  157. * @return bool
  158. *
  159. * Adds a user to a group.
  160. */
  161. public function addToGroup($uid, $gid) {
  162. return $this->handleRequest(
  163. $gid, 'addToGroup', array($uid, $gid));
  164. }
  165. /**
  166. * Removes a user from a group
  167. * @param string $uid Name of the user to remove from group
  168. * @param string $gid Name of the group from which remove the user
  169. * @return bool
  170. *
  171. * removes the user from a group.
  172. */
  173. public function removeFromGroup($uid, $gid) {
  174. return $this->handleRequest(
  175. $gid, 'removeFromGroup', array($uid, $gid));
  176. }
  177. /**
  178. * returns the number of users in a group, who match the search term
  179. * @param string $gid the internal group name
  180. * @param string $search optional, a search string
  181. * @return int|bool
  182. */
  183. public function countUsersInGroup($gid, $search = '') {
  184. return $this->handleRequest(
  185. $gid, 'countUsersInGroup', array($gid, $search));
  186. }
  187. /**
  188. * get an array with group details
  189. * @param string $gid
  190. * @return array|false
  191. */
  192. public function getGroupDetails($gid) {
  193. return $this->handleRequest(
  194. $gid, 'getGroupDetails', array($gid));
  195. }
  196. /**
  197. * get a list of all groups
  198. * @return string[] with group names
  199. *
  200. * Returns a list with all groups
  201. */
  202. public function getGroups($search = '', $limit = -1, $offset = 0) {
  203. $groups = array();
  204. foreach($this->backends as $backend) {
  205. $backendGroups = $backend->getGroups($search, $limit, $offset);
  206. if (is_array($backendGroups)) {
  207. $groups = array_merge($groups, $backendGroups);
  208. }
  209. }
  210. return $groups;
  211. }
  212. /**
  213. * check if a group exists
  214. * @param string $gid
  215. * @return bool
  216. */
  217. public function groupExists($gid) {
  218. return $this->handleRequest($gid, 'groupExists', array($gid));
  219. }
  220. /**
  221. * Check if backend implements actions
  222. * @param int $actions bitwise-or'ed actions
  223. * @return boolean
  224. *
  225. * Returns the supported actions as int to be
  226. * compared with \OCP\GroupInterface::CREATE_GROUP etc.
  227. */
  228. public function implementsActions($actions) {
  229. //it's the same across all our user backends obviously
  230. return $this->refBackend->implementsActions($actions);
  231. }
  232. /**
  233. * Return access for LDAP interaction.
  234. * @param string $gid
  235. * @return Access instance of Access for LDAP interaction
  236. */
  237. public function getLDAPAccess($gid) {
  238. return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
  239. }
  240. /**
  241. * Return a new LDAP connection for the specified group.
  242. * The connection needs to be closed manually.
  243. * @param string $gid
  244. * @return resource of the LDAP connection
  245. */
  246. public function getNewLDAPConnection($gid) {
  247. return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid));
  248. }
  249. }