Skip to content

Commit b431f9c

Browse files
committed
feat: add user to group endpoint
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent 56325c7 commit b431f9c

6 files changed

Lines changed: 126 additions & 0 deletions

File tree

app/Http/Controllers/Api/OAuth2/OAuth2GroupApiController.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
**/
1414

1515
use App\Http\Controllers\GetAllTrait;
16+
use App\Http\Controllers\Traits\RequestProcessor;
1617
use App\libs\Auth\Repositories\IGroupRepository;
1718
use App\ModelSerializers\SerializerRegistry;
19+
use App\Services\Auth\IGroupService;
20+
use Auth\Group;
21+
use Illuminate\Http\JsonResponse;
1822
use OAuth2\IResourceServerContext;
1923
use Utils\Services\ILogService;
2024

@@ -26,21 +30,28 @@ final class OAuth2GroupApiController extends OAuth2ProtectedController
2630
{
2731
use GetAllTrait;
2832

33+
use RequestProcessor;
34+
35+
private IGroupService $service;
36+
2937
/**
3038
* OAuth2UserApiController constructor.
3139
* @param IGroupRepository $repository
40+
* @param IGroupService $service
3241
* @param IResourceServerContext $resource_server_context
3342
* @param ILogService $log_service
3443
*/
3544
public function __construct
3645
(
3746
IGroupRepository $repository,
47+
IGroupService $service,
3848
IResourceServerContext $resource_server_context,
3949
ILogService $log_service,
4050
)
4151
{
4252
parent::__construct($resource_server_context, $log_service);
4353
$this->repository = $repository;
54+
$this->service = $service;
4455
}
4556

4657
protected function getAllSerializerType(): string
@@ -76,4 +87,20 @@ public function getOrderRules(): array
7687
'slug'
7788
];
7889
}
90+
91+
/**
92+
* @param $group_id
93+
* @param $user_id
94+
* @return JsonResponse|mixed
95+
*/
96+
public function addUserToGroup($group_id, $user_id): mixed
97+
{
98+
return $this->processRequest(function() use($group_id, $user_id) {
99+
$group = $this->repository->getById(intval($group_id));
100+
if (!$group instanceof Group)
101+
return $this->error404();
102+
$this->service->addUser2Group($group, intval($user_id));
103+
return $this->updated();
104+
});
105+
}
79106
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"Database\\Seeders\\": "database/seeders/"
9090
},
9191
"files": [
92+
"app/Utils/helpers.php",
9293
"app/libs/Utils/Html/HtmlHelpers.php"
9394
]
9495
},
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php namespace Database\Migrations;
2+
/**
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use Database\Seeders\SeedUtils;
16+
use Doctrine\Migrations\AbstractMigration;
17+
use Doctrine\DBAL\Schema\Schema as Schema;
18+
/**
19+
* Class Version20250805084926
20+
* @package Database\Migrations
21+
*/
22+
class Version20250805084926 extends AbstractMigration
23+
{
24+
/**
25+
* @param Schema $schema
26+
*/
27+
public function up(Schema $schema):void
28+
{
29+
SeedUtils::seedApiEndpoints('groups', [
30+
[
31+
'name' => 'add-user-to-groups',
32+
'active' => true,
33+
'route' => '/api/v1/groups/{id}/users/{user_id}',
34+
'http_method' => 'PUT',
35+
'scopes' => [
36+
\App\libs\OAuth2\IGroupScopes::Write
37+
],
38+
],
39+
]);
40+
}
41+
42+
/**
43+
* @param Schema $schema
44+
*/
45+
public function down(Schema $schema):void
46+
{
47+
48+
}
49+
}

database/seeds/ApiEndpointSeeder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ private function seedGroupEndpoints(){
198198
\App\libs\OAuth2\IGroupScopes::ReadAll
199199
],
200200
],
201+
[
202+
'name' => 'add-user-to-groups',
203+
'active' => true,
204+
'route' => '/api/v1/groups/{id}/users/{user_id}',
205+
'http_method' => 'PUT',
206+
'scopes' => [
207+
\App\libs\OAuth2\IGroupScopes::Write
208+
],
209+
],
201210
]);
202211
}
203212

routes/api.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555

5656
Route::group(['prefix' => 'groups'], function () {
5757
Route::get('', 'OAuth2GroupApiController@getAll');
58+
Route::group(array('prefix' => '{id}'), function () {
59+
Route::group(['prefix' => 'users'], function () {
60+
Route::group(['prefix' => '{user_id}'], function () {
61+
Route::put('', 'OAuth2GroupApiController@addUserToGroup');
62+
});
63+
});
64+
});
5865
});
5966

6067
// 3rd Party SSO integrations

tests/OAuth2GroupApiTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
**/
1515

1616
use App\libs\OAuth2\IGroupScopes;
17+
use Auth\Group;
18+
use Auth\User;
19+
use LaravelDoctrine\ORM\Facades\EntityManager;
1720
use Tests\OAuth2ProtectedApiTestCase;
1821

1922
/**
@@ -42,6 +45,36 @@ public function testGetAll(){
4245
$this->assertTrue($page->total > 0);
4346
}
4447

48+
public function testAddUserToGroup(){
49+
$repo = EntityManager::getRepository(Group::class);
50+
$group = $repo->getOneBySlug('sponsors-services');
51+
52+
$repo = EntityManager::getRepository(User::class);
53+
$user = $repo->getAll()[0];
54+
55+
$params = [
56+
'id' => $group->getId(),
57+
'user_id' => $user->getId()
58+
];
59+
60+
$former_groups_count = count($user->getGroups());
61+
62+
$this->action(
63+
"PUT",
64+
"Api\\OAuth2\\OAuth2GroupApiController@addUserToGroup",
65+
$params,
66+
[],
67+
[],
68+
[],
69+
array("HTTP_Authorization" => " Bearer " .$this->access_token));
70+
71+
$this->assertResponseStatus(201);
72+
73+
$user = $repo->getById($user->getId());
74+
$this->assertNotNull($user);
75+
$this->assertCount($former_groups_count + 1, $user->getGroups());
76+
}
77+
4578
protected function getScopes()
4679
{
4780
return [

0 commit comments

Comments
 (0)