1
0

Group_Proxy.php 8.0 KB

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