1
0

AuthorizedGroupService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public function __construct(
  15. private AuthorizedGroupMapper $mapper,
  16. ) {
  17. }
  18. /**
  19. * @return AuthorizedGroup[]
  20. */
  21. public function findAll(): array {
  22. return $this->mapper->findAll();
  23. }
  24. /**
  25. * Find AuthorizedGroup by id.
  26. *
  27. * @param int $id
  28. */
  29. public function find(int $id): ?AuthorizedGroup {
  30. return $this->mapper->find($id);
  31. }
  32. /**
  33. * @param $e
  34. * @throws NotFoundException
  35. */
  36. private function handleException(\Exception $e): void {
  37. if ($e instanceof DoesNotExistException ||
  38. $e instanceof MultipleObjectsReturnedException) {
  39. throw new NotFoundException('AuthorizedGroup not found');
  40. } else {
  41. throw $e;
  42. }
  43. }
  44. /**
  45. * Create a new AuthorizedGroup
  46. *
  47. * @param string $groupId
  48. * @param string $class
  49. * @return AuthorizedGroup
  50. * @throws Exception
  51. */
  52. public function create(string $groupId, string $class): AuthorizedGroup {
  53. $authorizedGroup = new AuthorizedGroup();
  54. $authorizedGroup->setGroupId($groupId);
  55. $authorizedGroup->setClass($class);
  56. return $this->mapper->insert($authorizedGroup);
  57. }
  58. /**
  59. * @throws NotFoundException
  60. */
  61. public function delete(int $id): void {
  62. try {
  63. $authorizedGroup = $this->mapper->find($id);
  64. $this->mapper->delete($authorizedGroup);
  65. } catch (\Exception $e) {
  66. $this->handleException($e);
  67. }
  68. }
  69. public function findExistingGroupsForClass(string $class): array {
  70. try {
  71. $authorizedGroup = $this->mapper->findExistingGroupsForClass($class);
  72. return $authorizedGroup;
  73. } catch (\Exception $e) {
  74. return [];
  75. }
  76. }
  77. public function removeAuthorizationAssociatedTo(IGroup $group): void {
  78. try {
  79. $this->mapper->removeGroup($group->getGID());
  80. } catch (\Exception $e) {
  81. $this->handleException($e);
  82. }
  83. }
  84. }