Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ protected function curateUpdatePayload(array $payload): array
]);
}

private function _create(){
try {

if(!Request::isJson()) return $this->error400();

$payload = Request::json()->all();
// Creates a Validator instance and validates the data.
$validation = Validator::make($payload, UserValidationRulesFactory::build($payload));
if ($validation->fails()) {
$ex = new ValidationException();
throw $ex->setMessages($validation->messages()->toArray());
}

$user = $this->openid_user_service->create($payload);

return $this->created(SerializerRegistry::getInstance()->getSerializer($user, SerializerRegistry::SerializerType_Private)->serialize());
}
catch (ValidationException $ex1)
{
Log::warning($ex1);
return $this->error412($ex1->getMessages());
}
catch (EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(['message' => $ex2->getMessage()]);
}
catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}

private function _update($id){
try {

Expand Down Expand Up @@ -193,6 +226,10 @@ private function _update($id){
}
}

public function create(){
return $this->_create();
}

public function updateMe(){
return $this->_update($this->resource_server_context->getCurrentUserId());
}
Expand Down
9 changes: 9 additions & 0 deletions database/seeds/ApiEndpointSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ private function seedUsersEndpoints()
\App\libs\OAuth2\IUserScopes::MeWrite
],
],
[
'name' => 'create-user',
'active' => true,
'route' => '/api/v1/users',
'http_method' => 'POST',
'scopes' => [
\App\libs\OAuth2\IUserScopes::Write
],
],
[
'name' => 'update-user',
'active' => true,
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

Route::group(['prefix' => 'users'], function () {
Route::get('', 'OAuth2UserApiController@getAll');
Route::post('', 'OAuth2UserApiController@create');
Route::group(['prefix' => '{id}'], function () {
Route::get('', 'OAuth2UserApiController@get');
Route::put('', 'OAuth2UserApiController@update');
Expand Down
43 changes: 43 additions & 0 deletions tests/OAuth2UserUpdateApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\libs\Auth\Models\IGroupSlugs;
use App\libs\OAuth2\IUserScopes;
use Auth\Group;
use Auth\User;
use LaravelDoctrine\ORM\Facades\EntityManager;

Expand All @@ -22,6 +25,46 @@

final class OAuth2UserUpdateApiTest extends OAuth2ProtectedApiTest
{
public function testUserCreate()
{
$group_name = IGroupSlugs::RawUsersGroup;
$group = EntityManager::getRepository(Group::class)->findOneBy(['name' => $group_name]);
$first_name = 'test_'. str_random(16);

$data = [
'first_name' => $first_name,
'last_name' => 'test_'. str_random(16),
'email' => 'test_'. str_random(16) . '@test.com',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to set groups and check if its works

'company' => 'test_'. str_random(16),
'groups' => [$group->getId()],
];

$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];

$response = $this->action
(
"POST",
"Api\\OAuth2\\OAuth2UserApiController@create",
[],
[],
[],
[],
$headers,
json_encode($data)
);

$this->assertResponseStatus(201);

$content = $response->getContent();
$response = json_decode($content);
$this->assertTrue($response->first_name == $first_name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check groups and rest of the set fields

$this->assertCount(1, $response->groups);
$this->assertEquals($group_name, $response->groups[0]);
}

public function testUserUpdate()
{
$user = EntityManager::getRepository(User::class)->findOneBy(['identifier' => 'sebastian.marcet']);
Expand Down
Loading