Skip to content

Commit 75d2d67

Browse files
committed
feat: new api v2 and get user by id v2 endpoint
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent 71f869e commit 75d2d67

7 files changed

Lines changed: 170 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,25 @@ public function get($id)
332332
}
333333
}
334334

335+
/**
336+
* @param $id
337+
* @return \Illuminate\Http\JsonResponse|mixed
338+
*/
339+
public function getV2($id)
340+
{
341+
return $this->processRequest(function() use($id) {
342+
$user = $this->repository->getById(intval($id));
343+
if (is_null($user)) {
344+
throw new EntityNotFoundException();
345+
}
346+
return $this->ok(SerializerRegistry::getInstance()
347+
->getSerializer($user, SerializerRegistry::SerializerType_Private)
348+
->serialize(
349+
Request::input("expand", '')
350+
));
351+
});
352+
}
353+
335354
/**
336355
* @param $user_id
337356
* @return JsonResponse|mixed

app/Http/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class Kernel extends HttpKernel
5353
'ssl',
5454
'oauth2.endpoint',
5555
],
56+
'api_v2' => [
57+
'ssl',
58+
'oauth2.endpoint',
59+
],
5660
];
5761

5862
/**

app/Providers/RouteServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ protected function mapApiRoutes()
115115
->namespace('App\Http\Controllers\Api\OAuth2')
116116
->prefix('api/v1')
117117
->group(base_path('routes/api.php'));
118+
119+
Route::middleware('api_v2')
120+
->namespace('App\Http\Controllers\Api\OAuth2')
121+
->prefix('api/v2')
122+
->group(base_path('routes/api_v2.php'));
118123
}
119124

120125
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 App\libs\Auth\Models\IGroupSlugs;
16+
use App\libs\OAuth2\IGroupScopes;
17+
use Auth\Group;
18+
use Database\Seeders\SeedUtils;
19+
use Doctrine\Migrations\AbstractMigration;
20+
use Doctrine\DBAL\Schema\Schema as Schema;
21+
use LaravelDoctrine\ORM\Facades\EntityManager;
22+
/**
23+
* Class Version20250807173401
24+
* @package Database\Migrations
25+
*/
26+
class Version20250807173401 extends AbstractMigration
27+
{
28+
/**
29+
* @param Schema $schema
30+
*/
31+
public function up(Schema $schema):void
32+
{
33+
SeedUtils::seedApiEndpoints('users', [
34+
[
35+
'name' => 'get-user-by-id-v2',
36+
'active' => true,
37+
'route' => '/api/v2/users/{id}',
38+
'http_method' => 'GET',
39+
'scopes' => [
40+
\App\libs\OAuth2\IUserScopes::ReadAll
41+
],
42+
],
43+
]);
44+
}
45+
46+
/**
47+
* @param Schema $schema
48+
*/
49+
public function down(Schema $schema):void
50+
{
51+
52+
}
53+
}

database/seeds/ApiEndpointSeeder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ private function seedUsersEndpoints()
8585
\App\libs\OAuth2\IUserScopes::ReadAll
8686
],
8787
],
88+
[
89+
'name' => 'get-user-by-id-v2',
90+
'active' => true,
91+
'route' => '/api/v2/users/{id}',
92+
'http_method' => 'GET',
93+
'scopes' => [
94+
\App\libs\OAuth2\IUserScopes::ReadAll
95+
],
96+
],
8897
[
8998
'name' => 'update-my-user',
9099
'active' => true,

routes/api_v2.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
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 Illuminate\Support\Facades\Route;
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| OAuth2 Protected API
20+
|--------------------------------------------------------------------------
21+
|
22+
| Here is where you can register API routes for your application. These
23+
| routes are loaded by the RouteServiceProvider within a group which
24+
| is assigned the "api" middleware group. Enjoy building your API!
25+
|
26+
*/
27+
28+
Route::group(['prefix' => 'users'], function () {
29+
Route::group(['prefix' => '{id}'], function () {
30+
Route::get('', 'OAuth2UserApiController@getV2');
31+
});
32+
});
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Class OAuth2UserServiceApiTest
2323
*/
24-
final class OAuth2UserServiceApiTest extends OAuth2ProtectedServiceAppApiTestCase {
24+
final class OAuth2UserApiTest extends OAuth2ProtectedServiceAppApiTestCase {
2525

2626
public function testUpdateMe(){
2727

@@ -72,6 +72,53 @@ public function testGetInfo(){
7272
$user_info = json_decode($content);
7373
}
7474

75+
public function testGetUserByIdV1(){
76+
$repo = EntityManager::getRepository(User::class);
77+
$user = $repo->getAll()[0];
78+
79+
$params = [
80+
'id' => $user->getId()
81+
];
82+
83+
$response = $this->action(
84+
"GET",
85+
"Api\OAuth2\OAuth2UserApiController@get",
86+
$params,
87+
[],
88+
[],
89+
[],
90+
array("HTTP_Authorization" => " Bearer " .$this->access_token));
91+
92+
$this->assertResponseStatus(200);
93+
$content = $response->getContent();
94+
$user = json_decode($content);
95+
$this->assertNotNull($user);
96+
}
97+
98+
public function testGetUserByIdV2(){
99+
$repo = EntityManager::getRepository(User::class);
100+
$user = $repo->getAll()[0];
101+
102+
$params = [
103+
'id' => $user->getId(),
104+
'expand' => 'groups'
105+
];
106+
107+
$response = $this->action(
108+
"GET",
109+
"Api\OAuth2\OAuth2UserApiController@getV2",
110+
$params,
111+
[],
112+
[],
113+
[],
114+
array("HTTP_Authorization" => " Bearer " .$this->access_token));
115+
116+
$this->assertResponseStatus(200);
117+
$content = $response->getContent();
118+
$user = json_decode($content);
119+
$this->assertNotNull($user);
120+
}
121+
75122
public function testGetInfoCORS(){
76123
$response = $this->action("OPTIONS", "Api\OAuth2\OAuth2UserApiController@me",
77124
[],

0 commit comments

Comments
 (0)