ringfinger/backend/classes/controller/UserEmailPutController.php

45 lines
991 B
PHP

<?php
declare(strict_types=1);
final class UserEmailPutController extends AbstractController
{
protected string $route = '/api/v1/user/{userId}/email';
protected array $mandatoryAttributes = [
'email',
];
private int $userId;
public function __construct(string $url)
{
parent::__construct($url);
$this->userId = (int)$this->getUrlParamInt('userId');
}
public function handle(): void
{
if (!$this->isUserLoggedIn() || !$this->hasUserPermission($this->userId)) {
return;
}
parent::handle();
$this->response = new ApiJsonResponse();
try {
$json = json_decode($this->requestBody);
$user = new User($this->userId);
if ($user->getEmail() !== $json->email) {
$user->setEmail($json->email);
$user->Save();
}
} catch (Throwable $e) {
$this->response->setStatus($e->getCode() !== 0 ? $e->getCode() : ServerStatus::BAD_REQUEST);
$this->response->setParameter('success', false);
$this->response->setMessage($e->getMessage());
}
}
}