Skip to content

Commit 39b33e8

Browse files
authored
Merge pull request #81 from leon-manq/main
adjustments regarding the new subgroup behavior since keycloak 23.0.0
2 parents b0ca585 + 8222bc4 commit 39b33e8

File tree

5 files changed

+127
-7
lines changed

5 files changed

+127
-7
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ $myCustomRepresentation = $myCustomResource->myCustomEndpoint();
110110
| `POST /admin/realms/{realm}/clients` | [Client](src/Representation/Client.php) | [Clients::import()](src/Resource/Clients.php) |
111111

112112
### [Groups](https://www.keycloak.org/docs-api/23.0.0/rest-api/index.html#_clients_resource)
113-
| Endpoint | Response | API |
114-
|----------|----------|-----|
115-
| `GET /admin/realms/{realm}/groups` | [GroupCollection](src/Collection/GroupCollection.php) | [Groups::all()](src/Resource/Groups.php) |
116-
| `GET /admin/realms/{realm}/groups/{id}` | [Group](src/Representation/Group.php) | [Groups::get()](src/Resource/Groups.php) |
117-
| `PUT /admin/realms/{realm}/groups/{id}` | `n/a` | [Groups::update()](src/Resource/Groups.php) |
118-
| `POST /admin/realms/{realm}/groups` | `n/a` | [Groups::import()](src/Resource/Groups.php) |
119-
| `DELETE /admin/realms/{realm}/groups` | `n/a` | [Groups::delete()](src/Resource/Groups.php) |
113+
| Endpoint | Response | API |
114+
|---------------------------------------------------|----------|-----------------------------------------------|
115+
| `GET /admin/realms/{realm}/groups` | [GroupCollection](src/Collection/GroupCollection.php) | [Groups::all()](src/Resource/Groups.php) |
116+
| `GET /admin/realms/{realm}/groups/{id}/children` | [GroupCollection](src/Collection/GroupCollection.php) | [Groups::children()](src/Resource/Groups.php) |
117+
| `GET /admin/realms/{realm}/groups/{id}` | [Group](src/Representation/Group.php) | [Groups::get()](src/Resource/Groups.php) |
118+
| `PUT /admin/realms/{realm}/groups/{id}` | `n/a` | [Groups::update()](src/Resource/Groups.php) |
119+
| `POST /admin/realms/{realm}/groups` | `n/a` | [Groups::create()](src/Resource/Groups.php) |
120+
| `POST /admin/realms/{realm}/groups/{id}/children` | `n/a` | [Groups::create()](src/Resource/Groups.php) |
121+
| `DELETE /admin/realms/{realm}/groups` | `n/a` | [Groups::delete()](src/Resource/Groups.php) |
120122

121123
### [Realms Admin](https://www.keycloak.org/docs-api/23.0.0/rest-api/index.html#_realms_admin_resource)
122124
| Endpoint | Response | API |

src/Representation/Group.php

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method Map|null getClientRoles()
1515
* @method string|null getId()
1616
* @method string|null getName()
17+
* @method string|null getParentId()
1718
* @method string|null getPath()
1819
* @method string[]|null getRealmRoles()
1920
* @method int|null getSubGroupCount()
@@ -23,6 +24,7 @@
2324
* @method self withClientRoles(?Map $clientRoles)
2425
* @method self withId(?string $id)
2526
* @method self withName(?string $name)
27+
* @method self withParentId(?string $parentId)
2628
* @method self withPath(?string $path)
2729
* @method self withRealmRoles(?array $realmRoles)
2830
* @method self withSubGroupCount(?int $subGroupCount)
@@ -38,6 +40,8 @@ public function __construct(
3840
protected ?Map $clientRoles = null,
3941
protected ?string $id = null,
4042
protected ?string $name = null,
43+
#[Since('23.0.0')]
44+
protected ?string $parentId = null,
4145
protected ?string $path = null,
4246
/** @var string[]|null */
4347
protected ?array $realmRoles = null,

src/Resource/Groups.php

+30
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public function all(string $realm, ?Criteria $criteria = null): GroupCollection
2727
);
2828
}
2929

30+
public function children(string $realm, string $groupId, ?Criteria $criteria = null): GroupCollection
31+
{
32+
return $this->queryExecutor->executeQuery(
33+
new Query(
34+
'/admin/realms/{realm}/groups/{groupId}/children',
35+
GroupCollection::class,
36+
[
37+
'realm' => $realm,
38+
'groupId' => $groupId,
39+
],
40+
$criteria
41+
)
42+
);
43+
}
44+
3045
public function get(string $realm, string $groupId): Group
3146
{
3247
return $this->queryExecutor->executeQuery(
@@ -55,6 +70,21 @@ public function create(string $realm, Group $group): void
5570
);
5671
}
5772

73+
public function createChild(string $realm, Group $group, string $parentGroupId): void
74+
{
75+
$this->commandExecutor->executeCommand(
76+
new Command(
77+
'/admin/realms/{realm}/groups/{groupId}/children',
78+
Method::POST,
79+
[
80+
'realm' => $realm,
81+
'groupId' => $parentGroupId,
82+
],
83+
$group
84+
)
85+
);
86+
}
87+
5888
public function update(string $realm, string $groupId, Group $updatedGroup): void
5989
{
6090
$this->commandExecutor->executeCommand(

tests/Integration/Resource/GroupsTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,28 @@ public function testImportSearchUpdateDeleteGroup(): void
5454
static::assertSame(404, $e->getCode());
5555
}
5656
}
57+
58+
public function testCreateChildGroup(): void
59+
{
60+
$this->skipIfKeycloakVersionIsLessThan('23.0.0');
61+
62+
$importedGroupName = Uuid::uuid4()->toString();
63+
$childGroupName = Uuid::uuid4()->toString();
64+
65+
$groups = $this->getKeycloak()->groups();
66+
67+
// Create group
68+
$groups->create('master', new Group(name: $importedGroupName));
69+
$group = $groups->all('master')->first();
70+
static::assertInstanceOf(Group::class, $group);
71+
72+
// Create child group
73+
$groups->createChild('master', new Group(name: $childGroupName), $group->getId());
74+
$childGroups = $groups->children('master', $group->getId());
75+
static::assertCount(1, $childGroups);
76+
77+
$childGroup = $childGroups->first();
78+
static::assertInstanceOf(Group::class, $childGroup);
79+
static::assertSame($childGroupName, $childGroup->getName());
80+
}
5781
}

tests/Unit/Resource/GroupsTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,39 @@ public function testGetAllGroups(): void
5050
static::assertSame('group-1', $groups->first()->getId());
5151
}
5252

53+
public function testGetGroupChildren(): void
54+
{
55+
$query = new Query(
56+
'/admin/realms/{realm}/groups/{groupId}/children',
57+
GroupCollection::class,
58+
[
59+
'realm' => 'realm-with-groups',
60+
'groupId' => 'child-group-id',
61+
],
62+
);
63+
64+
$queryExecutor = $this->createMock(QueryExecutor::class);
65+
$queryExecutor->expects(static::once())
66+
->method('executeQuery')
67+
->with($query)
68+
->willReturn(
69+
new GroupCollection([
70+
new Group(id: 'group-1'),
71+
new Group(id: 'group-2'),
72+
]),
73+
);
74+
75+
$groups = new Groups(
76+
$this->createMock(CommandExecutor::class),
77+
$queryExecutor,
78+
);
79+
$groups = $groups->children('realm-with-groups', 'child-group-id');
80+
81+
static::assertCount(2, $groups);
82+
static::assertInstanceOf(Group::class, $groups->first());
83+
static::assertSame('group-1', $groups->first()->getId());
84+
}
85+
5386
public function testGetGroup(): void
5487
{
5588
$query = new Query(
@@ -102,6 +135,33 @@ public function testCreateGroup(): void
102135
$groups->create('realm-with-groups', $group);
103136
}
104137

138+
public function testCreateChildGroup(): void
139+
{
140+
$group = new Group(name: 'child-group');
141+
142+
$command = new Command(
143+
'/admin/realms/{realm}/groups/{groupId}/children',
144+
Method::POST,
145+
[
146+
'realm' => 'realm-with-groups',
147+
'groupId' => 'parent-group-id',
148+
],
149+
$group
150+
);
151+
152+
$commandExecutor = $this->createMock(CommandExecutor::class);
153+
$commandExecutor->expects(static::once())
154+
->method('executeCommand')
155+
->with($command);
156+
157+
$groups = new Groups(
158+
$commandExecutor,
159+
$this->createMock(QueryExecutor::class),
160+
);
161+
162+
$groups->createChild('realm-with-groups', $group, 'parent-group-id');
163+
}
164+
105165
public function testUpdateGroup(): void
106166
{
107167
$group = new Group(id: 'group-id', name: 'updated-group');

0 commit comments

Comments
 (0)