Endpoint for user creation added
This commit is contained in:
parent
3a1a026e2f
commit
0a8c71b735
54
backend/classes/controller/UserPostController.php
Normal file
54
backend/classes/controller/UserPostController.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class UserPostController extends AbstractController
|
||||
{
|
||||
protected string $route = '/api/v1/user';
|
||||
protected array $mandatoryAttributes = [
|
||||
'username',
|
||||
'password',
|
||||
'email',
|
||||
'jabberAddress',
|
||||
];
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
parent::handle();
|
||||
|
||||
if ($this->response->getStatus() !== ServerStatus::OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
$json = json_decode($this->requestBody);
|
||||
|
||||
try {
|
||||
$user = new User();
|
||||
|
||||
$user->setUsername($json->username);
|
||||
$user->setPassword(Password::GetHash($json->password));
|
||||
$user->setEmail($json->email);
|
||||
$user->setJabberAddress($json->jabberAddress);
|
||||
$user->Save();
|
||||
|
||||
$this->response = new ApiJsonResponse();
|
||||
$this->response->setParameter('userId', $user->getUserId());
|
||||
} catch (DatabaseException $e) {
|
||||
$this->response = new ApiJsonResponse();
|
||||
$this->response->setParameter('success', false);
|
||||
$this->response->setMessage($e->getMessage());
|
||||
|
||||
switch ($e->getCode()) {
|
||||
case DatabaseException::CONNECTION_FAILED:
|
||||
$this->response->setStatus(ServerStatus::INTERNAL_ERROR);
|
||||
break;
|
||||
default:
|
||||
$this->response->setStatus(ServerStatus::INTERNAL_ERROR);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
|
||||
$this->response->setParameter('success', false);
|
||||
$this->response->setMessage($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user