From 043252b8b1dbb4cf6835c30e68809a9bef04181f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kruli=C5=A1?= Date: Sat, 20 Sep 2025 20:22:10 +0200 Subject: [PATCH] Adding endpoint for fetching attributes of a single group. --- .../GroupExternalAttributesPresenter.php | 18 +++++++++++++ app/V1Module/router/RouterFactory.php | 1 + .../GroupExternalAttributesPresenter.phpt | 27 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/app/V1Module/presenters/GroupExternalAttributesPresenter.php b/app/V1Module/presenters/GroupExternalAttributesPresenter.php index 717ecac8b..be3321158 100644 --- a/app/V1Module/presenters/GroupExternalAttributesPresenter.php +++ b/app/V1Module/presenters/GroupExternalAttributesPresenter.php @@ -93,6 +93,24 @@ public function actionDefault(string $instance, ?string $service, ?string $user) )); } + public function checkGet(string $groupId) + { + $group = $this->groups->findOrThrow($groupId); + if (!$this->groupAcl->canViewDetail($group)) { + throw new ForbiddenRequestException(); + } + } + + /** + * Get all external attributes for a group. This endpoint is meant to be used by webapp to display the attributes. + * @GET + */ + #[Path("groupId", new VUuid(), required: true)] + public function actionGet(string $groupId) + { + $attributes = $this->groupExternalAttributes->findBy(['group' => $groupId]); + $this->sendSuccessResponse($attributes); + } public function checkAdd(string $groupId) { diff --git a/app/V1Module/router/RouterFactory.php b/app/V1Module/router/RouterFactory.php index afeafc2f7..ef07b77a8 100644 --- a/app/V1Module/router/RouterFactory.php +++ b/app/V1Module/router/RouterFactory.php @@ -311,6 +311,7 @@ private static function createGroupAttributesRoutes(string $prefix): RouteList $router = new RouteList(); $router[] = new GetRoute($prefix, "GroupExternalAttributes:"); + $router[] = new GetRoute("$prefix/", "GroupExternalAttributes:get"); $router[] = new PostRoute("$prefix/", "GroupExternalAttributes:add"); $router[] = new DeleteRoute("$prefix/", "GroupExternalAttributes:remove"); return $router; diff --git a/tests/Presenters/GroupExternalAttributesPresenter.phpt b/tests/Presenters/GroupExternalAttributesPresenter.phpt index ef57c4ef0..264f63a29 100644 --- a/tests/Presenters/GroupExternalAttributesPresenter.phpt +++ b/tests/Presenters/GroupExternalAttributesPresenter.phpt @@ -184,6 +184,33 @@ class TestGroupExternalAttributesPresenter extends Tester\TestCase Assert::true($total > 0); } + public function testGetGroupAttributes() + { + PresenterTestHelper::loginDefaultAdmin($this->container); + + $groups = array_filter($this->presenter->groups->findAll(), function ($g) { + return !$g->isArchived() && count($g->getExternalAttributes()) > 1; + }); + Assert::true(count($groups) > 0); + $group = reset($groups); + + $payload = PresenterTestHelper::performPresenterRequest( + $this->presenter, + 'V1:GroupExternalAttributes', + 'GET', + ['action' => 'get', 'groupId' => $group->getId()], + ); + + Assert::count(count($group->getExternalAttributes()), $payload); + $attributes = array_map(function ($a) { + return join('|', [$a->getService(), $a->getKey(), $a->getValue()]); + }, $group->getExternalAttributes()->toArray()); + foreach ($payload as $a) { + $id = join('|', [$a->getService(), $a->getKey(), $a->getValue()]); + Assert::true(in_array($id, $attributes)); + } + } + public function testGetAttributesAdd() { PresenterTestHelper::loginDefaultAdmin($this->container, [TokenScope::GROUP_EXTERNAL]);