AuthorizedGroupService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Nextcloud GmbH
  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 OCA\Settings\Service;
  24. use OC\Settings\AuthorizedGroup;
  25. use OC\Settings\AuthorizedGroupMapper;
  26. use OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  28. use OCP\DB\Exception;
  29. use OCP\IGroup;
  30. class AuthorizedGroupService {
  31. /** @var AuthorizedGroupMapper $mapper */
  32. private $mapper;
  33. public function __construct(AuthorizedGroupMapper $mapper) {
  34. $this->mapper = $mapper;
  35. }
  36. /**
  37. * @return AuthorizedGroup[]
  38. */
  39. public function findAll(): array {
  40. return $this->mapper->findAll();
  41. }
  42. /**
  43. * Find AuthorizedGroup by id.
  44. *
  45. * @param int $id
  46. */
  47. public function find(int $id): ?AuthorizedGroup {
  48. return $this->mapper->find($id);
  49. }
  50. /**
  51. * @param $e
  52. * @throws NotFoundException
  53. */
  54. private function handleException(\Exception $e): void {
  55. if ($e instanceof DoesNotExistException ||
  56. $e instanceof MultipleObjectsReturnedException) {
  57. throw new NotFoundException("AuthorizedGroup not found");
  58. } else {
  59. throw $e;
  60. }
  61. }
  62. /**
  63. * Create a new AuthorizedGroup
  64. *
  65. * @param string $groupId
  66. * @param string $class
  67. * @return AuthorizedGroup
  68. * @throws Exception
  69. */
  70. public function create(string $groupId, string $class): AuthorizedGroup {
  71. $authorizedGroup = new AuthorizedGroup();
  72. $authorizedGroup->setGroupId($groupId);
  73. $authorizedGroup->setClass($class);
  74. return $this->mapper->insert($authorizedGroup);
  75. }
  76. /**
  77. * @throws NotFoundException
  78. */
  79. public function delete(int $id): void {
  80. try {
  81. $authorizedGroup = $this->mapper->find($id);
  82. $this->mapper->delete($authorizedGroup);
  83. } catch (\Exception $e) {
  84. $this->handleException($e);
  85. }
  86. }
  87. public function findExistingGroupsForClass(string $class): array {
  88. try {
  89. $authorizedGroup = $this->mapper->findExistingGroupsForClass($class);
  90. return $authorizedGroup;
  91. } catch (\Exception $e) {
  92. return [];
  93. }
  94. }
  95. public function removeAuthorizationAssociatedTo(IGroup $group): void {
  96. try {
  97. $this->mapper->removeGroup($group->getGID());
  98. } catch (\Exception $e) {
  99. $this->handleException($e);
  100. }
  101. }
  102. }