-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserManager.class.php
More file actions
287 lines (237 loc) · 10.3 KB
/
UserManager.class.php
File metadata and controls
287 lines (237 loc) · 10.3 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
session_start();
require_once "../userManagement/config.php"; // TODO: This might be a problem later, we have to solve. I think we should specify, that the user has to migrate some settings into his config file in his root folder.
require "../userManagement/PermissionManager.class.php";
class UserManager {
// A dbWrapper instance
private $DB;
private $timezone;
// This functions needs to be given a dbWrapper instance
public function __construct ($DB, $timezone = "Europe/Berlin") {
// Check if a session exists - if it does, get the users Data.
// Check if session is still valid by the token and if the max time is exceeded
// Apply the given dbWrapper instance
$this->DB = $DB;
$this->timezone = new DateTimeZone($timezone); // TODO: Parametrize this
}
public function login ($username, $password) {
// generate new token
// create session
// return the userdata
$parameters = Array();
$parameters[":username"] = $username;
$parameters[":password"] = $this->hash($password);
// Get the user data
$userdata = $this->DB->getRow("SELECT " . USER_TABLE . ".*, ".
"EXISTS(SELECT " . USER_TABLE . ".ID FROM " . ADMIN_TABLE . " WHERE " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID) AS isAdmin, " .
"EXISTS(SELECT " . USER_TABLE . ".ID FROM " . ADMIN_TABLE . " WHERE " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID AND " . ADMIN_TABLE . ".Deleteable = 1) AS isDeleteable " .
"FROM " . USER_TABLE ." WHERE Name = :username AND PasswordHash = :password", $parameters);
// If userdata was found, apply it to the session
if ($userdata) {
if ($userdata["Suspended"] == 1) {
throw new Exception("User suspended");
}
$userdata["Token"] = $this->updateToken($userdata["ID"]);
$_SESSION["userdata"] = $userdata;
return $userdata;
} else {
return false;
}
}
public function logout () {
// remove the session
// remove token from DB
if ($_SESSION["userdata"]) {
// set a new token (which invalidates the old one)
$this->updateToken($_SESSION["userdata"]["ID"]);
// and remove the userdata from the session
unset($_SESSION["userdata"]);
}
}
public function createUser ($username, $fullname, $password, $email, $gravatarEmail) {
// check if the username or email adress is already registered
$parameters = Array();
$parameters[":username"] = $username;
$parameters[":fullname"] = $fullname;
$parameters[":passwordHash"] = $this->hash($password);
$parameters[":email"] = $email;
$parameters[":gravatarEmail"] = $gravatarEmail;
$this->DB->query("INSERT INTO " . USER_TABLE . " (Name, Fullname, PasswordHash, Email, GravatarEmail) VALUES (:username, :fullname, :passwordHash, :email, :gravatarEmail)", $parameters);
}
public function changePassword ($userID, $newPassword) {
$parameters = Array();
$parameters[":userID"] = $userID;
$parameters[":passwordHash"] = $this->hash($newPassword);
$this->DB->query("UPDATE " . USER_TABLE . " SET PasswordHash = :passwordHash WHERE ID = :userID");
}
public function changeUsername ($userID, $newUsername) {
// TODO: Evaluate if the name is already taken!
$parameters = Array();
$parameters[":userID"] = $userID;
$parameters[":username"] = $newUsername; // TODO: html entities?!
$this->DB->query("UPDATE " . USER_TABLE . " SET Name = :username WHERE ID = :userID");
}
public function confirmPassword($pass) {
$data = Array();
$data[":id"] = $this->getSession()["ID"];
$pass2 = $this->DB->query("SELECT PasswordHash FROM " . USER_TABLE . " WHERE ID = :id", $data);
return $pass == $pass2;
}
public function activateUser ($username) {
$parameters = Array();
$parameters[":username"] = $username;
$this->DB->query("UPDATE " . USER_TABLE . " SET Suspended = 0 WHERE Name = :username", $parameters);
// Returning the affected user's ID
return $this->getUserID($username);
}
public function getUserID ($username) {
$parameters = Array();
$parameters[":Username"] = $username;
$result = $this->DB->getRow("SELECT ID FROM " . USER_TABLE . " WHERE Name = :Username", $parameters);
if (isset($result["ID"]))
return $result["ID"];
else
return false;
}
public function getLoginState () {
return $this->checkLoginState();
}
public function getAllActiveUsers() {
return $this->DB->getList("SELECT ID, Name, Fullname, Email, GravatarEmail, Deleteable, (" . ADMIN_TABLE . ".Deleteable IS NOT NULL) AS Admin FROM " . USER_TABLE . " LEFT JOIN " . ADMIN_TABLE . " ON " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID WHERE Suspended=0");
}
public function getAllSuspendedUsers() {
return $this->DB->getList("SELECT ID, Name, Fullname, Email, GravatarEmail, (" . ADMIN_TABLE . ".Deleteable IS NOT NULL) AS Admin FROM " . USER_TABLE . " LEFT JOIN " . ADMIN_TABLE . " ON " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID WHERE Suspended=1");
}
public function getSession () {
if ($this->checkLoginState())
return $_SESSION["userdata"];
else
return false;
}
/// Internal functions ///
// TODO: Can we replace this hash function by something better?
private function hash ($value) {
// Hash the given value with the md5 algorithm
return md5($value);
}
private function generateToken () {
// Generate a token
return uniqid('', true);
}
private function updateToken ($userID) {
$parameters = Array();
$parameters[":userID"] = $userID;
$parameters[":token"] = $this->generateToken();
$parameters[":expiration"] = $this->computeExpiration();
if ($this->tokenExists($userID))
$this->DB->query("UPDATE " . SESSION_TABLE . " SET Token = :token, Expiration = :expiration WHERE UserID = :userID", $parameters);
else
$this->DB->query("INSERT INTO " . SESSION_TABLE . " (UserID, Token, Expiration) VALUES (:userID, :token, :expiration)", $parameters);
return $parameters[":token"];
}
private function tokenExists ($userID) {
$parameters = Array();
$parameters[":userID"] = $userID;
$result = $this->DB->getRow("SELECT EXISTS(SELECT * FROM Session WHERE UserID = :userID)", $parameters);
return array_values($result)[0];
}
private function computeExpiration () {
$date = new DateTime("now", $this->timezone);
$date->add(new DateInterval('PT10H')); // Add 10 hours
return $date->format("Y-m-d H:i:s");
}
private function checkExpiration ($date) {
if (gettype($date) == "string")
// IF $date is a string, convert it into da DateTime Object
$date = new DateTime($date, $this->timezone);
$now = new DateTime("now", $this->timezone);
return (($now->getTimestamp() - $date->getTimestamp()) < 0); // negative value if $date is in the future
}
public function getUserData ($userID) {
$parameters = Array();
$parameters[":userID"] = $userID;
return $this->DB->getRow("SELECT *, ".
"EXISTS(SELECT " . USER_TABLE . ".ID FROM " . ADMIN_TABLE . " WHERE " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID) AS isAdmin, " .
"EXISTS(SELECT " . USER_TABLE . ".ID FROM " . ADMIN_TABLE . " WHERE " . USER_TABLE . ".ID = " . ADMIN_TABLE . ".UserID AND " . ADMIN_TABLE . ".Deleteable = 1) AS isDeleteable " .
"FROM " . USER_TABLE . " WHERE ID = :userID", $parameters);
}
private function checkLoginState () {
// TODO
// compare session token and db token
// see if token is expired (ONLY BY DB, NOT THE SESSION)
if (isset($_SESSION["userdata"], $_SESSION["userdata"]["Token"])) {
// TODO: is the token still valid?
$parameters = Array();
$parameters[":userID"] = $_SESSION["userdata"]["ID"];
$result = $this->DB->getRow("SELECT Token, Expiration FROM " . SESSION_TABLE . " WHERE UserID = :userID", $parameters);
if (is_array($result) && $result["Token"] == $_SESSION["userdata"]["Token"] && $this->checkExpiration($result["Expiration"])) {
return true;
}
}
return false;
}
//Checks if the current user is an administrator
public function checkAdmin() {
return (bool) $this->getSession()["isAdmin"];
}
//Promotes or demotes a user according to the given role(0=user, 1=admin, 2=god)
public function changeRole($userID, $role) {
$currentUserID = $this->getSession()["ID"];
$parameters = Array();
$parameters[":userID"] = $userID;
if (PermissionManager::isSuperior($currentUserID, $userID)) {
switch ($role) {
case 0:
if (PermissionManager::isAdmin($userID) && PermissionManager::isDeleteable($userID)) {
$this->DB->query("DELETE FROM " . ADMIN_TABLE . " WHERE UserID = :userID", $parameters);
return true;
} else {
return false;
}
case 1:
if (!PermissionManager::isAdmin($userID)) {
$this->DB->query("INSERT INTO " . ADMIN_TABLE . "(UserID, Deleteable) VALUES (:userID, 1)", $parameters);
return true;
} else {
return false;
}
case 2:
if (!PermissionManager::isDeleteable($currentUserID)) {
if (PermissionManager::isAdmin($userID)) {
$this->DB->query("UPDATE " . ADMIN_TABLE . " SET Deleteable = 0 WHERE UserID = :userID", $parameters);
} else {
$this->DB->query("INSERT INTO " . ADMIN_TABLE . " (UserID, Deleteable) VALUES (:userID, 0)", $parameters);
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}
public function updateUser($userID, $action, $newValue) {
$parameters = Array();
$parameters[":userID"] = $userID;
//$parameters[":action"] = $action;
$parameters[":newValue"] = $newValue;
switch($action) {
case "Email":
//TODO: Getting a magical syntax error when I write :action instead of Email. Fixing that would make this method a lot easier and prettier
$result = $this->DB->query("UPDATE " . USER_TABLE . " SET Email = :newValue WHERE ID = :userID", $parameters);
break;
case "GravatarEmail":
$result = $this->DB->query("UPDATE " . USER_TABLE . " SET GravatarEmail = :newValue WHERE ID = :userID", $parameters);
break;
case "Password":
$result = $this->DB->query("UPDATE " . USER_TABLE . " SET PasswordHash = :newValue WHERE ID = :userID", $parameters);
}
if($result) {
return true;
} else {
return false;
}
}
}
?>