1
0

AuthorizedGroupController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Controller;
  7. use OC\Settings\AuthorizedGroup;
  8. use OCA\Settings\Service\AuthorizedGroupService;
  9. use OCA\Settings\Service\NotFoundException;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\DB\Exception;
  13. use OCP\IRequest;
  14. class AuthorizedGroupController extends Controller {
  15. public function __construct(
  16. string $AppName,
  17. IRequest $request,
  18. private AuthorizedGroupService $authorizedGroupService,
  19. ) {
  20. parent::__construct($AppName, $request);
  21. }
  22. /**
  23. * @throws NotFoundException
  24. * @throws Exception
  25. */
  26. public function saveSettings(array $newGroups, string $class): DataResponse {
  27. $currentGroups = $this->authorizedGroupService->findExistingGroupsForClass($class);
  28. foreach ($currentGroups as $group) {
  29. /** @var AuthorizedGroup $group */
  30. $removed = true;
  31. foreach ($newGroups as $groupData) {
  32. if ($groupData['gid'] === $group->getGroupId()) {
  33. $removed = false;
  34. break;
  35. }
  36. }
  37. if ($removed) {
  38. $this->authorizedGroupService->delete($group->getId());
  39. }
  40. }
  41. foreach ($newGroups as $groupData) {
  42. $added = true;
  43. foreach ($currentGroups as $group) {
  44. /** @var AuthorizedGroup $group */
  45. if ($groupData['gid'] === $group->getGroupId()) {
  46. $added = false;
  47. break;
  48. }
  49. }
  50. if ($added) {
  51. $this->authorizedGroupService->create($groupData['gid'], $class);
  52. }
  53. }
  54. return new DataResponse(['valid' => true]);
  55. }
  56. }