diff options
| author | Daniel Weipert <git@mail.dweipert.de> | 2026-03-05 14:57:28 +0100 |
|---|---|---|
| committer | Daniel Weipert <git@mail.dweipert.de> | 2026-03-05 14:57:28 +0100 |
| commit | bd11271621bd3759cfd194ed0119c0dc28155fd0 (patch) | |
| tree | 319b4790d4e2b05513abf023732ed6fb0717f603 /src/Models/User.php | |
| parent | 2386148b8f048ba40d9f26cc97898bdcdc778ea2 (diff) | |
Diffstat (limited to 'src/Models/User.php')
| -rw-r--r-- | src/Models/User.php | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/Models/User.php b/src/Models/User.php index c0c73f8..c92f5b3 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -3,19 +3,26 @@ namespace App\Models; use App\Database; +use App\Errors\AppException; +use App\Errors\ErrorCode; use App\Errors\UnauthorizedError; use App\Support\ConnectsToDatabase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; class User implements ConnectsToDatabase { - public function __construct(private string $id) + public function __construct( + private string $id, + private string $name, + ) {} public static function fromDatabase(array $row): self { return new self( $row["id"], + $row["name"], ); } @@ -67,9 +74,9 @@ class User implements ConnectsToDatabase return self::fromDatabase($row); } - public static function new(string $id): self + public static function new(string $id, string $name): self { - return new self($id); + return new self($id, $name); } public static function authenticateWithRequest(Request $request): self @@ -81,6 +88,17 @@ class User implements ConnectsToDatabase throw new UnauthorizedError(); } + $tokens = Tokens::fetchWithAccessToken($accessToken); + + if (empty($tokens) /*|| $tokens->isExpired()*/) { + throw new AppException( + ErrorCode::UNKNOWN_TOKEN, + "Soft logged out", + Response::HTTP_UNAUTHORIZED, + ["soft_logout" => true], + ); + } + return $user; } @@ -88,11 +106,12 @@ class User implements ConnectsToDatabase { return !! Database::getInstance()->query( <<<SQL - insert into users (id) - values (:id) + insert into users (id, name) + values (:id, :name) SQL, [ "id" => $this->id, + "name" => $this->name, ] ); } @@ -108,10 +127,16 @@ class User implements ConnectsToDatabase return $this->id; } + public function getName(): string + { + return $this->name; + } + public function fetchDevice(string $id): ?Device { return Device::fetch($id, $this->id); } + /** * @return Device[] */ |
