Compare commits

..

No commits in common. "f6d1376dbb229b183dd5d6d77095f201cd894765" and "ec485fb03df8d8de8754ffd4def6976505649008" have entirely different histories.

2 changed files with 3 additions and 115 deletions

View File

@ -1,112 +0,0 @@
<?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
{
parent::handle();
if ($this->response->getStatus() !== ServerStatus::OK) {
return;
}
if ($this->requestBody === null) {
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
$this->response->setParameter('success', false);
$this->response->setMessage('No JSON body with changed parametesrs found!');
}
try {
$json = json_decode($this->requestBody, true);
$user = new User($this->userId);
$hasChanged = $this->handleUserData($user, $json);
$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, array $json): bool
{
if (isset($json['username'])) {
$hasChanged = $user->getUsername() !== $json['username'];
$user->setUsername($json['username']);
return $hasChanged;
}
return false;
}
private function setPassword(User $user, array $json): bool
{
if (isset($json['password'])) {
$hasChanged = !Password::IsValid($json['password'], $user->getPassword());
$user->setPassword(Password::GetHash($json['password']));
return $hasChanged;
}
return false;
}
private function setEmail(User $user, array $json): bool
{
if (isset($json['email'])) {
$hasChanged = $user->getEmail() !== $json['email'];
$user->setEmail($json['email']);
return $hasChanged;
}
return false;
}
private function setJabberAddress(User $user, array $json): bool
{
if (isset($json['jabberAddress'])) {
$hasChanged = $user->getJabberAddress() !== $json['jabberAddress'];
$user->setJabberAddress($json['jabberAddress']);
return $hasChanged;
}
return false;
}
private function handleUserData(User $user, array $json): bool
{
$hasChanged = $this->setUsername($user, $json) || false;
$hasChanged = $this->setPassword($user, $json) || $hasChanged;
$hasChanged = $this->setEmail($user, $json) || $hasChanged;
$hasChanged = $this->setJabberAddress($user, $json) || $hasChanged;
return $hasChanged;
}
}

View File

@ -21,7 +21,7 @@ CREATE TABLE Fingerprint (
PRIMARY KEY (FingerprintId), PRIMARY KEY (FingerprintId),
UNIQUE KEY Fingerprint (Fingerprint), UNIQUE KEY Fingerprint (Fingerprint),
KEY User (UserId), KEY User (UserId),
CONSTRAINT User FOREIGN KEY (UserId) REFERENCES User (UserId) ON DELETE CASCADE CONSTRAINT User FOREIGN KEY (UserId) REFERENCES User (UserId)
); );
CREATE TABLE Sharing ( CREATE TABLE Sharing (
@ -31,8 +31,8 @@ CREATE TABLE Sharing (
PRIMARY KEY (SharingId), PRIMARY KEY (SharingId),
UNIQUE KEY User (User, UserShared), UNIQUE KEY User (User, UserShared),
KEY UserSharedId (UserShared), KEY UserSharedId (UserShared),
CONSTRAINT UserId FOREIGN KEY (User) REFERENCES User (UserId) ON DELETE CASCADE, CONSTRAINT UserId FOREIGN KEY (User) REFERENCES User (UserId),
CONSTRAINT UserSharedId FOREIGN KEY (UserShared) REFERENCES User (UserId) ON DELETE CASCADE CONSTRAINT UserSharedId FOREIGN KEY (UserShared) REFERENCES User (UserId)
); );
COMMIT; COMMIT;