AuthorizedGroupController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Carl Schwan <carl@carlschwan.eu>
  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\Controller;
  24. use OC\Settings\AuthorizedGroup;
  25. use OCA\Settings\Service\AuthorizedGroupService;
  26. use OCA\Settings\Service\NotFoundException;
  27. use OCP\DB\Exception;
  28. use OCP\IRequest;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Controller;
  31. class AuthorizedGroupController extends Controller {
  32. /** @var AuthorizedGroupService $authorizedGroupService */
  33. private $authorizedGroupService;
  34. public function __construct(string $AppName, IRequest $request, AuthorizedGroupService $authorizedGroupService) {
  35. parent::__construct($AppName, $request);
  36. $this->authorizedGroupService = $authorizedGroupService;
  37. }
  38. /**
  39. * @throws NotFoundException
  40. * @throws Exception
  41. */
  42. public function saveSettings(array $newGroups, string $class): DataResponse {
  43. $currentGroups = $this->authorizedGroupService->findExistingGroupsForClass($class);
  44. foreach ($currentGroups as $group) {
  45. /** @var AuthorizedGroup $group */
  46. $removed = true;
  47. foreach ($newGroups as $groupData) {
  48. if ($groupData['gid'] === $group->getGroupId()) {
  49. $removed = false;
  50. break;
  51. }
  52. }
  53. if ($removed) {
  54. $this->authorizedGroupService->delete($group->getId());
  55. }
  56. }
  57. foreach ($newGroups as $groupData) {
  58. $added = true;
  59. foreach ($currentGroups as $group) {
  60. /** @var AuthorizedGroup $group */
  61. if ($groupData['gid'] === $group->getGroupId()) {
  62. $added = false;
  63. break;
  64. }
  65. }
  66. if ($added) {
  67. $this->authorizedGroupService->create($groupData['gid'], $class);
  68. }
  69. }
  70. return new DataResponse(['valid' => true]);
  71. }
  72. }