1
0

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