AuthorizedGroupService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Service;
  7. use OC\Settings\AuthorizedGroup;
  8. use OC\Settings\AuthorizedGroupMapper;
  9. use OCP\AppFramework\Db\DoesNotExistException;
  10. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  11. use OCP\DB\Exception;
  12. use OCP\IGroup;
  13. class AuthorizedGroupService {
  14. /** @var AuthorizedGroupMapper $mapper */
  15. private $mapper;
  16. public function __construct(AuthorizedGroupMapper $mapper) {
  17. $this->mapper = $mapper;
  18. }
  19. /**
  20. * @return AuthorizedGroup[]
  21. */
  22. public function findAll(): array {
  23. return $this->mapper->findAll();
  24. }
  25. /**
  26. * Find AuthorizedGroup by id.
  27. *
  28. * @param int $id
  29. */
  30. public function find(int $id): ?AuthorizedGroup {
  31. return $this->mapper->find($id);
  32. }
  33. /**
  34. * @param $e
  35. * @throws NotFoundException
  36. */
  37. private function handleException(\Exception $e): void {
  38. if ($e instanceof DoesNotExistException ||
  39. $e instanceof MultipleObjectsReturnedException) {
  40. throw new NotFoundException("AuthorizedGroup not found");
  41. } else {
  42. throw $e;
  43. }
  44. }
  45. /**
  46. * Create a new AuthorizedGroup
  47. *
  48. * @param string $groupId
  49. * @param string $class
  50. * @return AuthorizedGroup
  51. * @throws Exception
  52. */
  53. public function create(string $groupId, string $class): AuthorizedGroup {
  54. $authorizedGroup = new AuthorizedGroup();
  55. $authorizedGroup->setGroupId($groupId);
  56. $authorizedGroup->setClass($class);
  57. return $this->mapper->insert($authorizedGroup);
  58. }
  59. /**
  60. * @throws NotFoundException
  61. */
  62. public function delete(int $id): void {
  63. try {
  64. $authorizedGroup = $this->mapper->find($id);
  65. $this->mapper->delete($authorizedGroup);
  66. } catch (\Exception $e) {
  67. $this->handleException($e);
  68. }
  69. }
  70. public function findExistingGroupsForClass(string $class): array {
  71. try {
  72. $authorizedGroup = $this->mapper->findExistingGroupsForClass($class);
  73. return $authorizedGroup;
  74. } catch (\Exception $e) {
  75. return [];
  76. }
  77. }
  78. public function removeAuthorizationAssociatedTo(IGroup $group): void {
  79. try {
  80. $this->mapper->removeGroup($group->getGID());
  81. } catch (\Exception $e) {
  82. $this->handleException($e);
  83. }
  84. }
  85. }