Skip to content

Commit 0bb1642

Browse files
committed
feat: user creation endpoint
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent a86516b commit 0bb1642

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,39 @@ protected function curateUpdatePayload(array $payload): array
160160
]);
161161
}
162162

163+
private function _create(){
164+
try {
165+
166+
if(!Request::isJson()) return $this->error400();
167+
168+
$payload = Request::json()->all();
169+
// Creates a Validator instance and validates the data.
170+
$validation = Validator::make($payload, UserValidationRulesFactory::build($payload));
171+
if ($validation->fails()) {
172+
$ex = new ValidationException();
173+
throw $ex->setMessages($validation->messages()->toArray());
174+
}
175+
176+
$user = $this->openid_user_service->create($payload);
177+
178+
return $this->created(SerializerRegistry::getInstance()->getSerializer($user, SerializerRegistry::SerializerType_Private)->serialize());
179+
}
180+
catch (ValidationException $ex1)
181+
{
182+
Log::warning($ex1);
183+
return $this->error412($ex1->getMessages());
184+
}
185+
catch (EntityNotFoundException $ex2)
186+
{
187+
Log::warning($ex2);
188+
return $this->error404(['message' => $ex2->getMessage()]);
189+
}
190+
catch (Exception $ex) {
191+
Log::error($ex);
192+
return $this->error500($ex);
193+
}
194+
}
195+
163196
private function _update($id){
164197
try {
165198

@@ -193,6 +226,10 @@ private function _update($id){
193226
}
194227
}
195228

229+
public function create(){
230+
return $this->_create();
231+
}
232+
196233
public function updateMe(){
197234
return $this->_update($this->resource_server_context->getCurrentUserId());
198235
}

database/seeds/ApiEndpointSeeder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ private function seedUsersEndpoints()
9292
\App\libs\OAuth2\IUserScopes::MeWrite
9393
],
9494
],
95+
[
96+
'name' => 'create-user',
97+
'active' => true,
98+
'route' => '/api/v1/users',
99+
'http_method' => 'POST',
100+
'scopes' => [
101+
\App\libs\OAuth2\IUserScopes::Write
102+
],
103+
],
95104
[
96105
'name' => 'update-user',
97106
'active' => true,

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
Route::group(['prefix' => 'users'], function () {
2929
Route::get('', 'OAuth2UserApiController@getAll');
30+
Route::post('', 'OAuth2UserApiController@create');
3031
Route::group(['prefix' => '{id}'], function () {
3132
Route::get('', 'OAuth2UserApiController@get');
3233
Route::put('', 'OAuth2UserApiController@update');

tests/OAuth2UserUpdateApiTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,42 @@
2222

2323
final class OAuth2UserUpdateApiTest extends OAuth2ProtectedApiTest
2424
{
25+
public function testUserCreate()
26+
{
27+
$first_name = 'test_'. str_random(16);
28+
29+
$data = [
30+
'first_name' => $first_name,
31+
'last_name' => 'test_'. str_random(16),
32+
'email' => 'test_'. str_random(16) . '@test.com',
33+
'company' => 'test_'. str_random(16),
34+
'groups' => [323]
35+
];
36+
37+
$headers = [
38+
"HTTP_Authorization" => " Bearer " . $this->access_token,
39+
"CONTENT_TYPE" => "application/json"
40+
];
41+
42+
$response = $this->action
43+
(
44+
"POST",
45+
"Api\\OAuth2\\OAuth2UserApiController@create",
46+
[],
47+
[],
48+
[],
49+
[],
50+
$headers,
51+
json_encode($data)
52+
);
53+
54+
$this->assertResponseStatus(201);
55+
56+
$content = $response->getContent();
57+
$response = json_decode($content);
58+
$this->assertTrue($response->first_name == $first_name);
59+
}
60+
2561
public function testUserUpdate()
2662
{
2763
$user = EntityManager::getRepository(User::class)->findOneBy(['identifier' => 'sebastian.marcet']);

0 commit comments

Comments
 (0)