1
0

AuthorizedGroupMapper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Carl Schwan <carl@carlschwan.eu>
  4. *
  5. * @author Carl Schwan <carl@carlschwan.eu>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Settings;
  24. use OCP\AppFramework\Db\Entity;
  25. use OCP\AppFramework\Db\QBMapper;
  26. use OCP\DB\Exception;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IUser;
  32. /**
  33. * @template-extends QBMapper<AuthorizedGroup>
  34. */
  35. class AuthorizedGroupMapper extends QBMapper {
  36. public function __construct(IDBConnection $db) {
  37. parent::__construct($db, 'authorized_groups', AuthorizedGroup::class);
  38. }
  39. /**
  40. * @throws Exception
  41. */
  42. public function findAllClassesForUser(IUser $user): array {
  43. $qb = $this->db->getQueryBuilder();
  44. /** @var IGroupManager $groupManager */
  45. $groupManager = \OC::$server->get(IGroupManager::class);
  46. $groups = $groupManager->getUserGroups($user);
  47. if (count($groups) === 0) {
  48. return [];
  49. }
  50. $result = $qb->select('class')
  51. ->from($this->getTableName(), 'auth')
  52. ->where($qb->expr()->in('group_id', array_map(function (IGroup $group) use ($qb) {
  53. return $qb->createNamedParameter($group->getGID());
  54. }, $groups), IQueryBuilder::PARAM_STR))
  55. ->executeQuery();
  56. $classes = [];
  57. while ($row = $result->fetch()) {
  58. $classes[] = $row['class'];
  59. }
  60. $result->closeCursor();
  61. return $classes;
  62. }
  63. /**
  64. * @throws \OCP\AppFramework\Db\DoesNotExistException
  65. * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
  66. * @throws \OCP\DB\Exception
  67. */
  68. public function find(int $id): AuthorizedGroup {
  69. $queryBuilder = $this->db->getQueryBuilder();
  70. $queryBuilder->select('*')
  71. ->from($this->getTableName())
  72. ->where($queryBuilder->expr()->eq('id', $queryBuilder->createNamedParameter($id)));
  73. /** @var AuthorizedGroup $authorizedGroup */
  74. $authorizedGroup = $this->findEntity($queryBuilder);
  75. return $authorizedGroup;
  76. }
  77. /**
  78. * Get all the authorizations stored in the database.
  79. *
  80. * @return AuthorizedGroup[]
  81. * @throws \OCP\DB\Exception
  82. */
  83. public function findAll(): array {
  84. $qb = $this->db->getQueryBuilder();
  85. $qb->select('*')->from($this->getTableName());
  86. return $this->findEntities($qb);
  87. }
  88. public function findByGroupIdAndClass(string $groupId, string $class) {
  89. $qb = $this->db->getQueryBuilder();
  90. $qb->select('*')
  91. ->from($this->getTableName())
  92. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($groupId)))
  93. ->andWhere($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  94. return $this->findEntity($qb);
  95. }
  96. /**
  97. * @return Entity[]
  98. * @throws \OCP\DB\Exception
  99. */
  100. public function findExistingGroupsForClass(string $class): array {
  101. $qb = $this->db->getQueryBuilder();
  102. $qb->select('*')
  103. ->from($this->getTableName())
  104. ->where($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  105. return $this->findEntities($qb);
  106. }
  107. /**
  108. * @throws Exception
  109. */
  110. public function removeGroup(string $gid) {
  111. $qb = $this->db->getQueryBuilder();
  112. $qb->delete($this->getTableName())
  113. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($gid)))
  114. ->executeStatement();
  115. }
  116. }