You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Group_Proxy.php 8.2KB

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