Skip to content

Commit 8615d63

Browse files
authored
IBX-9328: Implemented dedicated endpoint that handles user group creation under root (#177)
For more details see https://issues.ibexa.co/browse/IBX-9328 and #177 Key changes: * Implemented dedicated endpoint that handles user group creation under root
1 parent f86cb0a commit 8615d63

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,12 +2340,6 @@ parameters:
23402340
count: 1
23412341
path: src/lib/Server/Controller/User.php
23422342

2343-
-
2344-
message: '#^Method Ibexa\\Rest\\Server\\Controller\\User\:\:createUserGroup\(\) has parameter \$groupPath with no type specified\.$#'
2345-
identifier: missingType.parameter
2346-
count: 1
2347-
path: src/lib/Server/Controller/User.php
2348-
23492343
-
23502344
message: '#^Method Ibexa\\Rest\\Server\\Controller\\User\:\:deleteUser\(\) has parameter \$userId with no type specified\.$#'
23512345
identifier: missingType.parameter

src/bundle/Resources/config/routing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ ibexa.rest.load_root_user_group:
10381038
ibexa.rest.create_root_user_group:
10391039
path: /user/groups/subgroups
10401040
defaults:
1041-
_controller: Ibexa\Rest\Server\Controller\User:createUserGroup
1041+
_controller: Ibexa\Rest\Server\Controller\User::createRootUserGroup
10421042
methods: [POST]
10431043

10441044
ibexa.rest.load_user_group:

src/lib/Server/Controller/User.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ibexa\Contracts\Core\Repository\Values\User\User as RepositoryUser;
2020
use Ibexa\Contracts\Core\Repository\Values\User\UserGroupRoleAssignment;
2121
use Ibexa\Contracts\Core\Repository\Values\User\UserRoleAssignment;
22+
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
2223
use Ibexa\Contracts\Rest\Exceptions\NotFoundException;
2324
use Ibexa\Core\Base\Exceptions\UnauthorizedException;
2425
use Ibexa\Rest\Message;
@@ -103,6 +104,8 @@ class User extends RestController
103104
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver */
104105
private $permissionResolver;
105106

107+
private ConfigResolverInterface $configResolver;
108+
106109
public function __construct(
107110
UserService $userService,
108111
RoleService $roleService,
@@ -111,7 +114,8 @@ public function __construct(
111114
LocationService $locationService,
112115
SectionService $sectionService,
113116
Repository $repository,
114-
PermissionResolver $permissionResolver
117+
PermissionResolver $permissionResolver,
118+
ConfigResolverInterface $configResolver
115119
) {
116120
$this->userService = $userService;
117121
$this->roleService = $roleService;
@@ -121,6 +125,7 @@ public function __construct(
121125
$this->sectionService = $sectionService;
122126
$this->repository = $repository;
123127
$this->permissionResolver = $permissionResolver;
128+
$this->configResolver = $configResolver;
124129
}
125130

126131
/**
@@ -234,20 +239,38 @@ public function redirectToCurrentUser(?UserInterface $user): Values\TemporaryRed
234239
);
235240
}
236241

242+
/**
243+
* Create a new user group under the root location.
244+
*
245+
* @throws \Ibexa\Contracts\Rest\Exceptions\NotFoundException
246+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
247+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException
248+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException
249+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
250+
*/
251+
public function createRootUserGroup(Request $request): Values\CreatedUserGroup
252+
{
253+
$rootPath = $this->configResolver->getParameter('users_group_root_subtree_path');
254+
255+
return $this->createUserGroup($rootPath, $request);
256+
}
257+
237258
/**
238259
* Create a new user group under the given parent
239260
* To create a top level group use /user/groups/1/5/subgroups.
240261
*
241-
* @param $groupPath
262+
* @param string $groupPath
242263
*
243-
* @throws \Ibexa\Rest\Server\Exceptions\BadRequestException
244-
*
245-
* @return \Ibexa\Rest\Server\Values\CreatedUserGroup
264+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException
265+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException
266+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
267+
* @throws \Ibexa\Contracts\Rest\Exceptions\NotFoundException
268+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
246269
*/
247-
public function createUserGroup($groupPath, Request $request)
270+
public function createUserGroup($groupPath, Request $request): Values\CreatedUserGroup
248271
{
249272
$userGroupLocation = $this->locationService->loadLocation(
250-
$this->extractLocationIdFromPath($groupPath)
273+
(int)$this->extractLocationIdFromPath($groupPath)
251274
);
252275

253276
$createdUserGroup = $this->userService->createUserGroup(

tests/bundle/Functional/UserTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,51 @@ public function testCreateUserGroup()
6969
return $href;
7070
}
7171

72+
/**
73+
* Covers POST /user/groups/subgroups (user group creation under root subtree).
74+
*/
75+
public function testCreateRootUserGroup(): void
76+
{
77+
$text = $this->addTestSuffix(__FUNCTION__);
78+
$xml = <<< XML
79+
<?xml version="1.0" encoding="UTF-8"?>
80+
<UserGroupCreate>
81+
<mainLanguageCode>eng-GB</mainLanguageCode>
82+
<remoteId>{$text}</remoteId>
83+
<fields>
84+
<field>
85+
<fieldDefinitionIdentifier>name</fieldDefinitionIdentifier>
86+
<languageCode>eng-GB</languageCode>
87+
<fieldValue>{$text}</fieldValue>
88+
</field>
89+
<field>
90+
<fieldDefinitionIdentifier>description</fieldDefinitionIdentifier>
91+
<languageCode>eng-GB</languageCode>
92+
<fieldValue>Description of {$text}</fieldValue>
93+
</field>
94+
</fields>
95+
</UserGroupCreate>
96+
XML;
97+
$request = $this->createHttpRequest(
98+
'POST',
99+
'/api/ibexa/v2/user/groups/subgroups',
100+
'UserGroupCreate+xml',
101+
'UserGroup+json',
102+
$xml
103+
);
104+
$response = $this->sendHttpRequest($request);
105+
106+
self::assertHttpResponseCodeEquals($response, 201);
107+
self::assertHttpResponseHasHeader($response, self::HEADER_LOCATION);
108+
109+
$href = $response->getHeader(self::HEADER_LOCATION)[0];
110+
111+
$trimmedHref = str_replace('/api/ibexa/v2/user/groups/', '', $href);
112+
$parts = explode('/', $trimmedHref);
113+
114+
self::assertSame('1/5', $parts[0] . '/' . $parts[1]);
115+
}
116+
72117
/**
73118
* @param $userGroupId Covers GET /user/groups/{groupId}
74119
* @depends testCreateUserGroup

0 commit comments

Comments
 (0)