-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.class.php
128 lines (108 loc) · 3.42 KB
/
user.class.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types = 1);
class User {
public int $id;
public string $firstLastName;
public string $username;
public string $password;
public string $userAddress;
public int $phoneNumber;
public int $restaurant;
public function __construct(int $id, string $firstLastName, string $username, string $userAddress, int $phoneNumber, int $restaurant)
{
$this->id = $id;
$this->firstLastName = $firstLastName;
$this->username = $username;
$this->userAddress = $userAddress;
$this->phoneNumber = $phoneNumber;
$this->restaurant = $restaurant;
}
function username() {
return $this->username;
}
static function getUserWithPassword(PDO $db, string $username, string $password) : ?User {
$stmt = $db->prepare('
SELECT UserId, FirstLastName, Username, UserAddress, PhoneNumber, RestaurantId
FROM Users
WHERE lower(username) = ? AND password = ?
');
$stmt->execute(array(strtolower($username), sha1($password)));
if ($user = $stmt->fetch()) {
return new User(
intval($user['UserId']),
$user['FirstLastName'],
$user['Username'],
$user['UserAddress'],
intval($user['PhoneNumber']),
intval($user['RestaurantId'])
);
}
return null;
}
static function getUser(PDO $db, int $id) : User {
$stmt = $db->prepare('
SELECT UserId, FirstLastName, Username, UserAddress, PhoneNumber, RestaurantId
FROM Users
WHERE UserId = ?
');
$stmt->execute(array($id));
$user = $stmt->fetch();
return new User(
intval($user['UserId']),
$user['FirstLastName'],
$user['Username'],
$user['UserAddress'],
intval($user['PhoneNumber']),
intval($user['RestaurantId'])
);
}
function save(PDO $db) {
$stmt = $db->prepare('
UPDATE Users SET FirstLastName = ?, Username = ?, UserAddress = ?, PhoneNumber = ?
WHERE UserId = ?
');
$stmt->execute(array($this->firstLastName, $this->username, $this->userAddress, $this->phoneNumber, $this->id));
}
static function registerUser($db, $userid, $firstLastName, $username, $password, $userAddress, $phoneNumber, $restaurantId){
$stmt = $db->prepare('
INSERT INTO Users(UserId, FirstLastName, Username, Password, UserAddress, PhoneNumber, RestaurantId) VALUES (?, ?, ?, ?, ?, ?, ?)
');
$stmt->execute(array($userid, $firstLastName, $username, $password, $userAddress, $phoneNumber, $restaurantId));
}
static function maxid(PDO $db){
$stmt = $db->prepare('
SELECT MAX(UserId)
FROM Users
');
$stmt->execute(array());
$max = $stmt->fetch();
return $max['MAX(UserId)']+1;
}
static function verifyAdmin(PDO $db, int $id){
$stmt = $db->prepare('
SELECT UserId
FROM Administrator
WHERE UserId = ?
');
$stmt->execute(array($id));
$admin = $stmt->fetch();
if($admin != NULL){
return True;
}
return False;
}
static function verifyOrders(PDO $db, int $id){
$stmt = $db->prepare('
SELECT OrderId
FROM Orders
WHERE UserId = ?
');
$stmt->execute(array($id));
$orders = $stmt->fetch();
if($orders != NULL){
return True;
}
return False;
}
}
?>