AuthorizedGroupMapper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class AuthorizedGroupMapper extends QBMapper {
  33. public function __construct(IDBConnection $db) {
  34. parent::__construct($db, 'authorized_groups', AuthorizedGroup::class);
  35. }
  36. /**
  37. * @throws Exception
  38. */
  39. public function findAllClassesForUser(IUser $user): array {
  40. $qb = $this->db->getQueryBuilder();
  41. /** @var IGroupManager $groupManager */
  42. $groupManager = \OC::$server->get(IGroupManager::class);
  43. $groups = $groupManager->getUserGroups($user);
  44. if (count($groups) === 0) {
  45. return [];
  46. }
  47. $result = $qb->select('class')
  48. ->from($this->getTableName(), 'auth')
  49. ->where($qb->expr()->in('group_id', array_map(function (IGroup $group) use ($qb) {
  50. return $qb->createNamedParameter($group->getGID());
  51. }, $groups), IQueryBuilder::PARAM_STR))
  52. ->executeQuery();
  53. $classes = [];
  54. while ($row = $result->fetch()) {
  55. $classes[] = $row['class'];
  56. }
  57. $result->closeCursor();
  58. return $classes;
  59. }
  60. /**
  61. * @throws \OCP\AppFramework\Db\DoesNotExistException
  62. * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
  63. * @throws \OCP\DB\Exception
  64. */
  65. public function find(int $id): AuthorizedGroup {
  66. $queryBuilder = $this->db->getQueryBuilder();
  67. $queryBuilder->select('*')
  68. ->from($this->getTableName())
  69. ->where($queryBuilder->expr()->eq('id', $queryBuilder->createNamedParameter($id)));
  70. /** @var AuthorizedGroup $authorizedGroup */
  71. $authorizedGroup = $this->findEntity($queryBuilder);
  72. return $authorizedGroup;
  73. }
  74. /**
  75. * Get all the authorizations stored in the database.
  76. *
  77. * @return AuthorizedGroup[]
  78. * @throws \OCP\DB\Exception
  79. */
  80. public function findAll(): array {
  81. $qb = $this->db->getQueryBuilder();
  82. $qb->select('*')->from($this->getTableName());
  83. return $this->findEntities($qb);
  84. }
  85. public function findByGroupIdAndClass(string $groupId, string $class) {
  86. $qb = $this->db->getQueryBuilder();
  87. $qb->select('*')
  88. ->from($this->getTableName())
  89. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($groupId)))
  90. ->andWhere($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  91. return $this->findEntity($qb);
  92. }
  93. /**
  94. * @return Entity[]
  95. * @throws \OCP\DB\Exception
  96. */
  97. public function findExistingGroupsForClass(string $class): array {
  98. $qb = $this->db->getQueryBuilder();
  99. $qb->select('*')
  100. ->from($this->getTableName())
  101. ->where($qb->expr()->eq('class', $qb->createNamedParameter($class)));
  102. return $this->findEntities($qb);
  103. }
  104. /**
  105. * @throws Exception
  106. */
  107. public function removeGroup(string $gid) {
  108. $qb = $this->db->getQueryBuilder();
  109. $qb->delete($this->getTableName())
  110. ->where($qb->expr()->eq('group_id', $qb->createNamedParameter($gid)))
  111. ->executeStatement();
  112. }
  113. }