blob: 1160dba62b85dc95e5a8454b546f7b463da58b9a (
plain)
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
|
<?php
class users {
public static function add(string $username, string $password): int {
$db = getDB();
$db->insert('users', [
'username' => $username,
'password' => pwhash($password)
]);
return $db->insertId();
}
public static function exists(string $username): bool {
$db = getDB();
$count = (int)$db->querySingle("SELECT COUNT(*) FROM users WHERE username=?", $username);
return $count > 0;
}
public static function validatePassword(string $username, string $password): bool {
$db = getDB();
$row = $db->querySingleRow("SELECT * FROM users WHERE username=?", $username);
if (!$row)
return false;
return $row['password'] == pwhash($password);
}
public static function getUserByPwhash(string $pwhash): ?User {
$db = getDB();
$data = $db->querySingleRow("SELECT * FROM users WHERE password=?", $pwhash);
return $data ? new User($data) : null;
}
public static function setPassword(int $id, string $new_password) {
getDB()->exec("UPDATE users SET password=? WHERE id=?", pwhash($new_password), $id);
}
}
|