blob: bc3da7087ffe7df0afd5d96ed103e98534ccf2a4 (
plain) (
blame)
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
|
<?php
namespace Controller;
use Database;
use Exception;
function on_post() {
global $user_status;
$user_status = "";
try {
Database\User::fromDB($_POST["username"]);
$user_status = "User \"" . $_POST["username"] . "\" already exists!";
return;
}
catch(Exception $e) {}
try {
Database\User::create($_POST["username"], $_POST["password"], "User");
}
catch(Exception $e) {
$user_status = $e;
}
}
function on_patch() {
global $TOKEN;
global $METHOD;
global $username_status;
global $password_status;
$username_status = "";
$password_status = "";
$status = null;
switch ($METHOD['type']) {
case 'username': $status = 'username_status'; break;
case 'password': $status = 'password_status'; break;
default: throw new Exception('Invalid patch type ' . $METHOD['type']);
}
$user = null;
try {
$user = Database\Cookie::fromDB($TOKEN);
}
catch(Exception $e) {
$$status = "Couldn't retrieve user!";
return;
}
switch ($METHOD['type']) {
case 'username':
$user->update($METHOD['username']);
header('Location: /user/' . $METHOD['username']);
break;
case 'password':
$user->update($user->Username, $METHOD['password']);
header('Location: /user/' . $user->Username);
break;
}
exit();
}
function on_delete() {
global $TOKEN;
global $METHOD;
global $user_status;
$user_status = "";
$user = null;
try {
$user = Database\Cookie::fromDB($TOKEN);
}
catch (Exception $e) {
$user_status = 'Invalid token!';
return;
}
$to_delete = null;
try {
$to_delete = Database\User::fromDBuid($METHOD['uid']);
}
catch(Exception $e) {
$list_status = "The user you're trying to delete doesn't exist!";
return;
}
if ($user->UID !== $to_delete->UID && $user->Role !== 'Admin') {
$list_status = 'You have no permission to delete this user!';
return;
}
$to_delete->delete();
header('Location: /');
exit();
}
|