1
0

Group_Proxy.php 10 KB

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