Skip to content

Commit 65d90a3

Browse files
Added user administration
You can now edit users within the admin panel, you can also ban them from the game. TODO: Temporary bans
1 parent 6d62f28 commit 65d90a3

File tree

10 files changed

+349
-22
lines changed

10 files changed

+349
-22
lines changed

init.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
$user = new user($_SESSION['userID']);
3232

3333
$user->updateTimer('laston', time());
34-
35-
36-
if (!$user->checkTimer('jail')) {
34+
35+
if ($user->info->U_userLevel == 0 && $_GET["page"] != "logout") {
36+
$page->loadPage('banned');
37+
} else if (!$user->checkTimer('jail')) {
3738
if ($jailPageCheck["accessInJail"]) {
3839
$page->loadPage($pageToLoad);
3940
} else {

modules/admin/admin.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class admin extends module {
77
public function constructModule() {
88

99
/* Redirect the user to the home page if they are a user */
10-
if ($this->user->info->U_userLevel == 1) {
10+
if ($this->user->info->U_userLevel != 2) {
1111
header("Location:?page=loggedin");
1212
exit;
1313
}

modules/admin/moduleInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
new hook("customMenus", function ($user) {
17-
if ($user && $user->info->U_userLevel != 1) {
17+
if ($user && $user->info->U_userLevel == 2) {
1818
return array(
1919
"title" => "Admin",
2020
"items" => array(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
class blank extends module {
3+
class banned extends module {
44

55
public $allowedMethods = array();
66

77
public $pageName = '';
88

99
public function constructModule() {
10-
10+
$this->html .= $this->page->buildElement("error", array("text" => "This account has been banned!"));
1111
}
1212

1313
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class blankTemplate extends template {
3+
class bannedTemplate extends template {
44

55
public $blankElement = '';
66

modules/banned/moduleInfo.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
$info = array(
3+
"name" => "Banned User",
4+
"version" => "1.0.0",
5+
"description" => "This page is shown when a banned user logs in",
6+
"author" => array(
7+
"name" => "Chris Day",
8+
"url" => "http://glscript.cdcoding.com"
9+
),
10+
"pageName" => "You Are Banned!",
11+
"accessInJail" => true,
12+
"requireLogin" => true
13+
);
14+
?>

modules/blank/moduleInfo.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

modules/users/moduleInfo.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
$info = array(
3+
"name" => "Users",
4+
"version" => "1.0.0",
5+
"description" => "This module allows a admin to edit users",
6+
"author" => array(
7+
"name" => "Chris Day",
8+
"url" => "http://glscript.cdcoding.com"
9+
),
10+
"pageName" => "User Administration",
11+
"accessInJail" => false,
12+
"requireLogin" => true,
13+
"admin" => array(
14+
array(
15+
"text" => "Find User",
16+
"method" => "view",
17+
),
18+
array(
19+
"hide" => true,
20+
"text" => "Edit User",
21+
"method" => "edit",
22+
),
23+
array(
24+
"hide" => true,
25+
"text" => "Delete User",
26+
"method" => "delete",
27+
)
28+
)
29+
);
30+
31+
?>

modules/users/users.admin.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
class adminModule {
4+
5+
private function getUser($userID, $search = false) {
6+
7+
if ($userID === false) {
8+
return array();
9+
}
10+
11+
if ($search) {
12+
$add = " WHERE U_id = :id OR U_name LIKE :search";
13+
} else {
14+
$add = " WHERE U_id = :id";
15+
}
16+
17+
$user = $this->db->prepare("
18+
SELECT
19+
U_id as 'id',
20+
U_name as 'name',
21+
U_userLevel as 'userLevel',
22+
(U_userLevel = 1) as 'isUser',
23+
(U_userLevel = 2) as 'isAdmin',
24+
(U_userLevel = 0) as 'isBanned',
25+
US_money as 'money',
26+
US_exp as 'exp',
27+
US_bank as 'bank',
28+
US_credits as 'credits',
29+
US_bullets as 'bullets',
30+
US_bio as 'bio',
31+
US_pic as 'pic'
32+
FROM users
33+
INNER JOIN userStats ON US_id = U_id
34+
" . $add . "
35+
ORDER BY U_name"
36+
);
37+
$user->bindParam(":id", $userID);
38+
39+
if ($search) {
40+
$searchTerm = "%".$userID."%";
41+
$user->bindParam(":search", $searchTerm);
42+
$user->execute();
43+
return $user->fetchAll(PDO::FETCH_ASSOC);
44+
} else {
45+
$user->execute();
46+
return $user->fetch(PDO::FETCH_ASSOC);
47+
}
48+
}
49+
50+
private function validateUser($user) {
51+
$errors = array();
52+
53+
if (strlen($user["name"]) < 6) {
54+
$errors[] = "User name is to short, this must be atleast 5 characters";
55+
}
56+
57+
return $errors;
58+
59+
}
60+
61+
public function method_edit () {
62+
63+
if (!isset($this->methodData->id)) {
64+
return $this->html = $this->page->buildElement("error", array("text" => "No user ID specified"));
65+
}
66+
67+
$user = $this->getUser($this->methodData->id);
68+
69+
if (isset($this->methodData->submit)) {
70+
$user = (array) $this->methodData;
71+
$errors = $this->validateUser($user);
72+
73+
if (count($errors)) {
74+
foreach ($errors as $error) {
75+
$this->html .= $this->page->buildElement("error", array("text" => $error));
76+
}
77+
} else {
78+
$update = $this->db->prepare("
79+
UPDATE users SET U_name = :name, U_userLevel = :userLevel WHERE U_id = :id;
80+
UPDATE userStats SET US_pic = :pic, US_bio = :bio, US_bullets = :bullets, US_credits = :credits, US_bank = :bank, US_exp = :exp, US_money = :money WHERE US_id = :id;
81+
");
82+
$update->bindParam(":name", $this->methodData->name);
83+
$update->bindParam(":userLevel", $this->methodData->userLevel);
84+
$update->bindParam(":pic", $this->methodData->pic);
85+
$update->bindParam(":bio", $this->methodData->bio);
86+
$update->bindParam(":bullets", $this->methodData->bullets);
87+
$update->bindParam(":credits", $this->methodData->credits);
88+
$update->bindParam(":bank", $this->methodData->bank);
89+
$update->bindParam(":exp", $this->methodData->exp);
90+
$update->bindParam(":money", $this->methodData->money);
91+
$update->bindParam(":id", $this->methodData->id);
92+
$update->execute();
93+
94+
$this->html .= $this->page->buildElement("success", array("text" => "This user has been updated"));
95+
96+
}
97+
98+
}
99+
100+
$user["editType"] = "edit";
101+
$this->html .= $this->page->buildElement("userForm", $user);
102+
}
103+
104+
public function method_delete () {
105+
106+
if (!isset($this->methodData->id)) {
107+
return $this->html = $this->page->buildElement("error", array("text" => "No user ID specified"));
108+
}
109+
110+
$user = $this->getUser($this->methodData->id);
111+
112+
if (!isset($user["id"])) {
113+
return $this->html = $this->page->buildElement("error", array("text" => "This user does not exist"));
114+
}
115+
116+
if (isset($this->methodData->commit)) {
117+
$delete = $this->db->prepare("
118+
DELETE FROM users WHERE C_id = :id;
119+
");
120+
$delete->bindParam(":id", $this->methodData->id);
121+
$delete->execute();
122+
123+
header("Location: ?page=admin&module=users");
124+
125+
}
126+
127+
128+
$this->html .= $this->page->buildElement("userDelete", $user);
129+
}
130+
131+
public function method_view () {
132+
133+
if (!isset($this->methodData->user)) {
134+
$this->methodData->user = false;
135+
}
136+
137+
$this->html .= $this->page->buildElement("userList", array(
138+
"users" => $this->getUser($this->methodData->user, true)
139+
));
140+
141+
}
142+
143+
}

0 commit comments

Comments
 (0)