This repository was archived by the owner on Aug 23, 2025. It is now read-only.
forked from GravitLauncher/HttpMethodExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
64 lines (55 loc) · 1.96 KB
/
User.php
File metadata and controls
64 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Gravita\Http;
use Gravita\Http\Database;
use \PDO;
class User {
public $id;
public $username;
public $uuid;
private $password_hash;
public function __construct($id = null, $username = null, $uuid = null, $password_hash = null) {
$this->id = $id;
$this->username = $username;
$this->uuid = $uuid;
$this->password_hash = $password_hash;
}
public function verify_password($password) {
return password_verify($password, $this->password_hash);
}
public function to_response() {
return [
"username" => $this->username,
"uuid" => $this->uuid,
"roles" => [],
"permissions" => [],
"assets" => (object) [], // You can implement assets
"properties" => (object) []
];
}
public static function get_by_id(Database $db, $id) : User|null {
$stmt = $db->getPDO()->prepare("SELECT * FROM users WHERE id=:id");
$stmt->execute(['id' => $id]);
return User::read_from_row($stmt->fetch(PDO::FETCH_ASSOC));
}
public static function get_by_uuid(Database $db, $uuid) : User|null {
$stmt = $db->getPDO()->prepare("SELECT * FROM users WHERE uuid=:uuid");
$stmt->execute(['uuid' => $uuid]);
return User::read_from_row($stmt->fetch(PDO::FETCH_ASSOC));
}
public static function get_by_username(Database $db, $username) : User|null {
$stmt = $db->getPDO()->prepare("SELECT * FROM users WHERE username=:username");
$stmt->execute(['username' => $username]);
return User::read_from_row($stmt->fetch(PDO::FETCH_ASSOC));
}
public static function read_from_row($row) : User|null {
if(!$row) {
return null;
}
$user = new User();
$user->id = $row["id"];
$user->username = $row["username"];
$user->uuid = $row["uuid"];
$user->password_hash = $row["password"];
return $user;
}
}