AuthorizedGroupMapper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Settings;
  7. use OCP\AppFramework\Db\Entity;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\DB\Exception;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. use OCP\IGroup;
  13. use OCP\IGroupManager;
  14. use OCP\IUser;
  15. /**
  16. * @template-extends QBMapper<AuthorizedGroup>
  17. */
  18. class AuthorizedGroupMapper extends QBMapper {
  19. public function __construct(IDBConnection $db) {
  20. parent::__construct($db, 'authorized_groups', AuthorizedGroup::class);
  21. }
  22. /**
  23. * @throws Exception
  24. */
  25. public function findAllClassesForUser(IUser $user): array {
  26. $qb = $this->db->getQueryBuilder();
  27. /** @var IGroupManager $groupManager */
  28. $groupManager = \OC::$server->get(IGroupManager::class);
  29. $groups = $groupManager->getUserGroups($user);
  30. if (count($groups) === 0) {
  31. return [];
  32. }
  33. $result = $qb->select('class')
  34. ->from($this->getTableName(), 'auth')
  35. ->where($qb->expr()->in('group_id', array_map(function (IGroup $group) use ($qb) {
  36. return $qb->createNamedParameter($group->getGID());
  37. }, $groups), IQueryBuilder::PARAM_STR))
  38. ->executeQuery();
  39. $classes = [];
  40. while ($row = $result->fetch()) {
  41. $classes[] = $row['class'];
  42. }
  43. $result->closeCursor();
  44. return $classes;
  45. }
  46. /**
  47. * @throws \OCP\AppFramework\Db\DoesNotExistException
  48. * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
  49. * @throws \OCP\DB\Exception
  50. */
  51. public function find(int $id): AuthorizedGroup {
  52. $queryBuilder = $this->db->getQueryBuilder();
  53. $queryBuilder->select('*')
  54. ->from($this->getTableName())
  55. ->where($queryBuilder->expr()->eq('id', $queryBuilder->createNamedParameter($id)));
  56. /** @var AuthorizedGroup $authorizedGroup */
  57. $authorizedGroup = $this->findEntity($queryBuilder);
  58. return $authorizedGroup;
  59. }
  60. /**
  61. * Get all the authorizations stored in the database.
  62. *
  63. * @return AuthorizedGroup[]
  64. * @throws \OCP\DB\Exception
  65. */
  66. public function findAll(): array {
  67. $qb = $this->db->getQueryBuilder();
  68. $qb->select('*')->from($this->getTableName());
  69. return $this->findEntities($qb);
  70. }
  71. public function findByGroupIdAndClass(string $groupId, string $class) {
  72. $qb = $this->db->getQueryBuilder();
  73. $qb->select('*')
  74. ->from($this->getTableName())
  75. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($groupId)))
  76. ->andWhere($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  77. return $this->findEntity($qb);
  78. }
  79. /**
  80. * @return Entity[]
  81. * @throws \OCP\DB\Exception
  82. */
  83. public function findExistingGroupsForClass(string $class): array {
  84. $qb = $this->db->getQueryBuilder();
  85. $qb->select('*')
  86. ->from($this->getTableName())
  87. ->where($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  88. return $this->findEntities($qb);
  89. }
  90. /**
  91. * @throws Exception
  92. */
  93. public function removeGroup(string $gid) {
  94. $qb = $this->db->getQueryBuilder();
  95. $qb->delete($this->getTableName())
  96. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($gid)))
  97. ->executeStatement();
  98. }
  99. }