GroupMembershipMapper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\User_LDAP\Db;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\IDBConnection;
  11. /**
  12. * @template-extends QBMapper<GroupMembership>
  13. */
  14. class GroupMembershipMapper extends QBMapper {
  15. public function __construct(IDBConnection $db) {
  16. parent::__construct($db, 'ldap_group_membership', GroupMembership::class);
  17. }
  18. /**
  19. * @return string[]
  20. */
  21. public function getKnownGroups(): array {
  22. $query = $this->db->getQueryBuilder();
  23. $result = $query->selectDistinct('groupid')
  24. ->from($this->getTableName())
  25. ->executeQuery();
  26. $groups = array_column($result->fetchAll(), 'groupid');
  27. $result->closeCursor();
  28. return $groups;
  29. }
  30. /**
  31. * @return GroupMembership[]
  32. */
  33. public function findGroupMemberships(string $groupid): array {
  34. $qb = $this->db->getQueryBuilder();
  35. $select = $qb->select('*')
  36. ->from($this->getTableName())
  37. ->where($qb->expr()->eq('groupid', $qb->createNamedParameter($groupid)));
  38. return $this->findEntities($select);
  39. }
  40. /**
  41. * @return GroupMembership[]
  42. */
  43. public function findGroupMembershipsForUser(string $userid): array {
  44. $qb = $this->db->getQueryBuilder();
  45. $select = $qb->select('*')
  46. ->from($this->getTableName())
  47. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userid)));
  48. return $this->findEntities($select);
  49. }
  50. public function deleteGroups(array $removedGroups): void {
  51. $query = $this->db->getQueryBuilder();
  52. $query->delete($this->getTableName())
  53. ->where($query->expr()->in('groupid', $query->createParameter('groupids')));
  54. foreach (array_chunk($removedGroups, 1000) as $removedGroupsChunk) {
  55. $query->setParameter('groupids', $removedGroupsChunk, IQueryBuilder::PARAM_STR_ARRAY);
  56. $query->executeStatement();
  57. }
  58. }
  59. }