Group_Proxy.php 9.8 KB

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