Group_Proxy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 Johannes Leuker <j.leuker@hosting.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\User_LDAP;
  30. use OC\ServerNotAvailableException;
  31. use OCP\Group\Backend\IBatchMethodsBackend;
  32. use OCP\Group\Backend\IDeleteGroupBackend;
  33. use OCP\Group\Backend\IGetDisplayNameBackend;
  34. use OCP\Group\Backend\IGroupDetailsBackend;
  35. use OCP\Group\Backend\INamedBackend;
  36. use OCP\GroupInterface;
  37. class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend {
  38. private $backends = [];
  39. private ?Group_LDAP $refBackend = null;
  40. private Helper $helper;
  41. private GroupPluginManager $groupPluginManager;
  42. private bool $isSetUp = false;
  43. public function __construct(
  44. Helper $helper,
  45. ILDAPWrapper $ldap,
  46. AccessFactory $accessFactory,
  47. GroupPluginManager $groupPluginManager
  48. ) {
  49. parent::__construct($ldap, $accessFactory);
  50. $this->helper = $helper;
  51. $this->groupPluginManager = $groupPluginManager;
  52. }
  53. protected function setup(): void {
  54. if ($this->isSetUp) {
  55. return;
  56. }
  57. $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
  58. foreach ($serverConfigPrefixes as $configPrefix) {
  59. $this->backends[$configPrefix] =
  60. new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager);
  61. if (is_null($this->refBackend)) {
  62. $this->refBackend = &$this->backends[$configPrefix];
  63. }
  64. }
  65. $this->isSetUp = true;
  66. }
  67. /**
  68. * Tries the backends one after the other until a positive result is returned from the specified method
  69. *
  70. * @param string $id the gid connected to the request
  71. * @param string $method the method of the group backend that shall be called
  72. * @param array $parameters an array of parameters to be passed
  73. * @return mixed the result of the method or false
  74. */
  75. protected function walkBackends($id, $method, $parameters) {
  76. $this->setup();
  77. $gid = $id;
  78. $cacheKey = $this->getGroupCacheKey($gid);
  79. foreach ($this->backends as $configPrefix => $backend) {
  80. if ($result = call_user_func_array([$backend, $method], $parameters)) {
  81. if (!$this->isSingleBackend()) {
  82. $this->writeToCache($cacheKey, $configPrefix);
  83. }
  84. return $result;
  85. }
  86. }
  87. return false;
  88. }
  89. /**
  90. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  91. *
  92. * @param string $id the gid connected to the request
  93. * @param string $method the method of the group backend that shall be called
  94. * @param array $parameters an array of parameters to be passed
  95. * @param mixed $passOnWhen the result matches this variable
  96. * @return mixed the result of the method or false
  97. */
  98. protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
  99. $this->setup();
  100. $gid = $id;
  101. $cacheKey = $this->getGroupCacheKey($gid);
  102. $prefix = $this->getFromCache($cacheKey);
  103. //in case the uid has been found in the past, try this stored connection first
  104. if (!is_null($prefix)) {
  105. if (isset($this->backends[$prefix])) {
  106. $result = call_user_func_array([$this->backends[$prefix], $method], $parameters);
  107. if ($result === $passOnWhen) {
  108. //not found here, reset cache to null if group vanished
  109. //because sometimes methods return false with a reason
  110. $groupExists = call_user_func_array(
  111. [$this->backends[$prefix], 'groupExists'],
  112. [$gid]
  113. );
  114. if (!$groupExists) {
  115. $this->writeToCache($cacheKey, null);
  116. }
  117. }
  118. return $result;
  119. }
  120. }
  121. return false;
  122. }
  123. protected function activeBackends(): int {
  124. $this->setup();
  125. return count($this->backends);
  126. }
  127. /**
  128. * is user in group?
  129. *
  130. * @param string $uid uid of the user
  131. * @param string $gid gid of the group
  132. * @return bool
  133. *
  134. * Checks whether the user is member of a group or not.
  135. */
  136. public function inGroup($uid, $gid) {
  137. return $this->handleRequest($gid, 'inGroup', [$uid, $gid]);
  138. }
  139. /**
  140. * Get all groups a user belongs to
  141. *
  142. * @param string $uid Name of the user
  143. * @return string[] with group names
  144. *
  145. * This function fetches all groups a user belongs to. It does not check
  146. * if the user exists at all.
  147. */
  148. public function getUserGroups($uid) {
  149. $this->setup();
  150. $groups = [];
  151. foreach ($this->backends as $backend) {
  152. $backendGroups = $backend->getUserGroups($uid);
  153. if (is_array($backendGroups)) {
  154. $groups = array_merge($groups, $backendGroups);
  155. }
  156. }
  157. return $groups;
  158. }
  159. /**
  160. * get a list of all users in a group
  161. *
  162. * @return array<int,string> user ids
  163. */
  164. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  165. $this->setup();
  166. $users = [];
  167. foreach ($this->backends as $backend) {
  168. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  169. if (is_array($backendUsers)) {
  170. $users = array_merge($users, $backendUsers);
  171. }
  172. }
  173. return $users;
  174. }
  175. /**
  176. * @param string $gid
  177. * @return bool
  178. */
  179. public function createGroup($gid) {
  180. return $this->handleRequest(
  181. $gid, 'createGroup', [$gid]);
  182. }
  183. /**
  184. * delete a group
  185. */
  186. public function deleteGroup(string $gid): bool {
  187. return $this->handleRequest(
  188. $gid, 'deleteGroup', [$gid]);
  189. }
  190. /**
  191. * Add a user to a group
  192. *
  193. * @param string $uid Name of the user to add to group
  194. * @param string $gid Name of the group in which add the user
  195. * @return bool
  196. *
  197. * Adds a user to a group.
  198. */
  199. public function addToGroup($uid, $gid) {
  200. return $this->handleRequest(
  201. $gid, 'addToGroup', [$uid, $gid]);
  202. }
  203. /**
  204. * Removes a user from a group
  205. *
  206. * @param string $uid Name of the user to remove from group
  207. * @param string $gid Name of the group from which remove the user
  208. * @return bool
  209. *
  210. * removes the user from a group.
  211. */
  212. public function removeFromGroup($uid, $gid) {
  213. return $this->handleRequest(
  214. $gid, 'removeFromGroup', [$uid, $gid]);
  215. }
  216. /**
  217. * returns the number of users in a group, who match the search term
  218. *
  219. * @param string $gid the internal group name
  220. * @param string $search optional, a search string
  221. * @return int|bool
  222. */
  223. public function countUsersInGroup($gid, $search = '') {
  224. return $this->handleRequest(
  225. $gid, 'countUsersInGroup', [$gid, $search]);
  226. }
  227. /**
  228. * get an array with group details
  229. *
  230. * @param string $gid
  231. * @return array|false
  232. */
  233. public function getGroupDetails($gid) {
  234. return $this->handleRequest(
  235. $gid, 'getGroupDetails', [$gid]);
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getGroupsDetails(array $gids): array {
  241. if (!($this instanceof IGroupDetailsBackend || $this->implementsActions(GroupInterface::GROUP_DETAILS))) {
  242. throw new \Exception("Should not have been called");
  243. }
  244. $groupData = [];
  245. foreach ($gids as $gid) {
  246. $groupData[$gid] = $this->handleRequest($gid, 'getGroupDetails', [$gid]);
  247. }
  248. return $groupData;
  249. }
  250. /**
  251. * get a list of all groups
  252. *
  253. * @return string[] with group names
  254. *
  255. * Returns a list with all groups
  256. */
  257. public function getGroups($search = '', $limit = -1, $offset = 0) {
  258. $this->setup();
  259. $groups = [];
  260. foreach ($this->backends as $backend) {
  261. $backendGroups = $backend->getGroups($search, $limit, $offset);
  262. if (is_array($backendGroups)) {
  263. $groups = array_merge($groups, $backendGroups);
  264. }
  265. }
  266. return $groups;
  267. }
  268. /**
  269. * check if a group exists
  270. *
  271. * @param string $gid
  272. * @return bool
  273. */
  274. public function groupExists($gid) {
  275. return $this->handleRequest($gid, 'groupExists', [$gid]);
  276. }
  277. /**
  278. * Check if a group exists
  279. *
  280. * @throws ServerNotAvailableException
  281. */
  282. public function groupExistsOnLDAP(string $gid, bool $ignoreCache = false): bool {
  283. return $this->handleRequest($gid, 'groupExistsOnLDAP', [$gid, $ignoreCache]);
  284. }
  285. /**
  286. * returns the groupname for the given LDAP DN, if available
  287. */
  288. public function dn2GroupName(string $dn): string|false {
  289. $id = 'DN,' . $dn;
  290. return $this->handleRequest($id, 'dn2GroupName', [$dn]);
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function groupsExists(array $gids): array {
  296. return array_values(array_filter(
  297. $gids,
  298. fn (string $gid): bool => $this->handleRequest($gid, 'groupExists', [$gid]),
  299. ));
  300. }
  301. /**
  302. * Check if backend implements actions
  303. *
  304. * @param int $actions bitwise-or'ed actions
  305. * @return boolean
  306. *
  307. * Returns the supported actions as int to be
  308. * compared with \OCP\GroupInterface::CREATE_GROUP etc.
  309. */
  310. public function implementsActions($actions) {
  311. $this->setup();
  312. //it's the same across all our user backends obviously
  313. return $this->refBackend->implementsActions($actions);
  314. }
  315. /**
  316. * Return access for LDAP interaction.
  317. *
  318. * @param string $gid
  319. * @return Access instance of Access for LDAP interaction
  320. */
  321. public function getLDAPAccess($gid) {
  322. return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
  323. }
  324. /**
  325. * Return a new LDAP connection for the specified group.
  326. * The connection needs to be closed manually.
  327. *
  328. * @param string $gid
  329. * @return resource|\LDAP\Connection The LDAP connection
  330. */
  331. public function getNewLDAPConnection($gid) {
  332. return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]);
  333. }
  334. public function getDisplayName(string $gid): string {
  335. return $this->handleRequest($gid, 'getDisplayName', [$gid]);
  336. }
  337. /**
  338. * Backend name to be shown in group management
  339. * @return string the name of the backend to be shown
  340. * @since 22.0.0
  341. */
  342. public function getBackendName(): string {
  343. return 'LDAP';
  344. }
  345. public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
  346. return $this->handleRequest($gid, 'searchInGroup', [$gid, $search, $limit, $offset]);
  347. }
  348. }