93 lines
2.1 KiB
PHP
93 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
final class UserPutController extends AbstractController
|
|
{
|
|
protected string $route = '/api/v1/user/{userId}';
|
|
|
|
private int $userId;
|
|
|
|
public function __construct(string $url)
|
|
{
|
|
parent::__construct($url);
|
|
|
|
$this->userId = (int)$this->getUrlParamInt('userId');
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
if (!$this->isUserLoggedIn()) {
|
|
return;
|
|
}
|
|
|
|
parent::handle();
|
|
|
|
if ($this->response->getStatus() !== ServerStatus::OK || !$this->hasUserPermission($this->userId)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$user = new User($this->userId);
|
|
|
|
$hasChanged = $this->handleUserData($user);
|
|
|
|
$this->response = new ApiJsonResponse();
|
|
|
|
if ($hasChanged) {
|
|
$user->Save();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->response->setMessage('No differing attributes found. Nothing changed.');
|
|
} catch (Throwable $e) {
|
|
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
|
|
$this->response->setParameter('success', false);
|
|
$this->response->setMessage($e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function setUsername(User $user): bool
|
|
{
|
|
$hasChanged = $user->getUsername() !== $this->jsonBody->username;
|
|
$user->setUsername($this->jsonBody->username);
|
|
|
|
return $hasChanged;
|
|
}
|
|
|
|
private function setPassword(User $user): bool
|
|
{
|
|
$hasChanged = !Password::IsValid($this->jsonBody->password, $user->getPassword());
|
|
$user->setPassword(Password::GetHash($this->jsonBody->password));
|
|
|
|
return $hasChanged;
|
|
}
|
|
|
|
|
|
private function setEmail(User $user): bool
|
|
{
|
|
$hasChanged = $user->getEmail() !== $this->jsonBody->email;
|
|
$user->setEmail($this->jsonBody->email);
|
|
|
|
return $hasChanged;
|
|
}
|
|
|
|
private function setJabberAddress(User $user): bool
|
|
{
|
|
$hasChanged = $user->getJabberAddress() !== $this->jsonBody->jabberAddress;
|
|
$user->setJabberAddress($this->jsonBody->jabberAddress);
|
|
|
|
return $hasChanged;
|
|
}
|
|
|
|
private function handleUserData(User $user): bool
|
|
{
|
|
$hasChanged = $this->setUsername($user) || false;
|
|
$hasChanged = $this->setPassword($user) || $hasChanged;
|
|
$hasChanged = $this->setEmail($user) || $hasChanged;
|
|
$hasChanged = $this->setJabberAddress($user) || $hasChanged;
|
|
|
|
return $hasChanged;
|
|
}
|
|
} |