From d08f4c83470c25d35d24594bd73e4effdac191a0 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 19 Aug 2025 15:50:42 +0200 Subject: database migrations and models for users, devices, tokens --- src/Models/Device.php | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Models/Tokens.php | 41 ++++++++++++++ src/Models/User.php | 88 +++++++++++++++++++++++++++++ 3 files changed, 279 insertions(+) create mode 100644 src/Models/Device.php create mode 100644 src/Models/Tokens.php create mode 100644 src/Models/User.php (limited to 'src/Models') diff --git a/src/Models/Device.php b/src/Models/Device.php new file mode 100644 index 0000000..2b09c6b --- /dev/null +++ b/src/Models/Device.php @@ -0,0 +1,150 @@ + $row The row from the database + */ + public static function fromDatabase(array $row): self + { + return new self( + $row["id"], + $row["user_id"], + $row["name"] + ); + } + + public static function fetch(string $id = "", string $userId = ""): ?self + { + if (! empty($id) && empty($userId)) { + throw new \InvalidArgumentException("Can't fetch device without user id."); + } + + $row = []; + + if (! empty($userId)) { + if (! empty($id)) { + $row = Database::getInstance()->query( + << $id, + "user_id" => $userId, + ] + )->fetch(); + } else { + $row = Database::getInstance()->query( + << $userId, + ] + )->fetch(); + } + } + + if (empty($row)) { + return null; + } + + return self::fromDatabase($row); + } + + public static function fetchAll(string $userId = ""): array + { + if (empty($userId)) { + throw new \InvalidArgumentException("missing user id"); + } + + $devices = []; + $rows = Database::getInstance()->query( + << $userId, + ] + )->fetchAll(); + + foreach ($rows as $row) { + $devices[] = Device::fromDatabase($row); + } + + return $devices; + } + + public static function new(string $userId, string $deviceId = "", string $initialDisplayName = ""): self + { + $deviceId = $deviceId ?: md5($userId . random_bytes(512)); + $initialDisplayName = $initialDisplayName ?: "capybara"; + + $accessToken = md5($userId . random_bytes(512)); + $refreshToken = md5($userId . random_bytes(512)); + + return new self( + $deviceId, + $userId, + $initialDisplayName, + $accessToken, + $refreshToken, + ); + } + + public function insert(): bool + { + return Database::getInstance()->query( + << $this->id, + "user_id" => $this->userId, + "name" => $this->name, + "access_token" => $this->accessToken, + "refresh_token" => $this->refreshToken, + ] + ); + } + + public function update(): bool + {} + + public function delete(): bool + {} + + public function getId(): string + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getAccessToken(): string + { + return $this->accessToken; + } + + public function getRefreshToken(): string + { + return $this->refreshToken; + } +} diff --git a/src/Models/Tokens.php b/src/Models/Tokens.php new file mode 100644 index 0000000..a94c876 --- /dev/null +++ b/src/Models/Tokens.php @@ -0,0 +1,41 @@ +query( + << $id, + ] + ); + } + + public static function fetchAll(): array + {} + + public static function fetchWithPassword(string $id, string $password): ?self + { + $row = Database::getInstance()->query("select * from users where id=:id and password=:password", [ + "id" => $id, + "password" => $password, + ])->fetch(); + + if (empty($row)) { + return null; + } + + return self::fromDatabase($row); + } + + public function insert(): bool + { + return Database::getInstance()->query( + << $this->id, + ] + ); + } + + public function update(): bool + {} + + public function delete(): bool + {} + + public function getId(): string + { + return $this->id; + } + + public function fetchDevice(string $id): ?Device + { + return Device::fetch($id, $this->id); + } + /** + * @return Device[] + */ + public function fetchDevices(): array + { + return Device::fetchAll($this->id); + } +} -- cgit v1.2.3