diff options
Diffstat (limited to 'src/Models/User.php')
| -rw-r--r-- | src/Models/User.php | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/Models/User.php b/src/Models/User.php index c92f5b3..a30bee0 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -4,14 +4,16 @@ namespace App\Models; use App\Database; use App\Errors\AppException; -use App\Errors\ErrorCode; use App\Errors\UnauthorizedError; use App\Support\ConnectsToDatabase; +use Matrix\Enums\ErrorCode; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class User implements ConnectsToDatabase { + private string $deviceId; + public function __construct( private string $id, private string $name, @@ -62,7 +64,7 @@ class User implements ConnectsToDatabase public static function fetchWithAccessToken(string $accessToken): ?self { $row = Database::getInstance()->query(<<<SQL - select users.* from users left join tokens on tokens.user_id = users.id where tokens.access_token=:access_token + select users.*, tokens.device_id from users left join tokens on tokens.user_id = users.id where tokens.access_token=:access_token SQL, [ "access_token" => $accessToken, ])->fetch(); @@ -71,7 +73,10 @@ class User implements ConnectsToDatabase return null; } - return self::fromDatabase($row); + $user = self::fromDatabase($row); + $user->setDeviceId($row["device_id"]); + + return $user; } public static function new(string $id, string $name): self @@ -82,6 +87,11 @@ class User implements ConnectsToDatabase public static function authenticateWithRequest(Request $request): self { $accessToken = str_replace("Bearer ", "", $request->headers->get("authorization") ?: ""); + + if (empty($accessToken)) { + throw new AppException(ErrorCode::UNAUTHORIZED, "Missing access token", Response::HTTP_UNAUTHORIZED); + } + $user = self::fetchWithAccessToken($accessToken); if (empty($user)) { @@ -132,9 +142,25 @@ class User implements ConnectsToDatabase return $this->name; } + public function setDeviceId(string $id): void + { + $this->deviceId = $id; + } + + public function getDeviceId(): string + { + return $this->deviceId; + } + public function fetchDevice(string $id): ?Device { - return Device::fetch($id, $this->id); + $device = Device::fetch($id, $this->id); + + if ($device) { + $this->setDeviceId($device->getId()); + } + + return $device; } /** |
