summaryrefslogtreecommitdiff
path: root/src/Models/User.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Models/User.php')
-rw-r--r--src/Models/User.php67
1 files changed, 59 insertions, 8 deletions
diff --git a/src/Models/User.php b/src/Models/User.php
index c0c73f8..a30bee0 100644
--- a/src/Models/User.php
+++ b/src/Models/User.php
@@ -3,19 +3,28 @@
namespace App\Models;
use App\Database;
+use App\Errors\AppException;
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
{
- public function __construct(private string $id)
+ private string $deviceId;
+
+ public function __construct(
+ private string $id,
+ private string $name,
+ )
{}
public static function fromDatabase(array $row): self
{
return new self(
$row["id"],
+ $row["name"],
);
}
@@ -55,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();
@@ -64,23 +73,42 @@ 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): 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
{
$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)) {
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 +116,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 +137,32 @@ class User implements ConnectsToDatabase
return $this->id;
}
+ public function getName(): string
+ {
+ 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;
}
+
/**
* @return Device[]
*/