-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
278 lines (211 loc) · 7.36 KB
/
dashboard.php
File metadata and controls
278 lines (211 loc) · 7.36 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
<?php //dashboard.php the dashboard page
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// --- Database credentials ---
$host = 'localhost';
$db = 'acc_db'; //name of the sql db
$user = 'user'; //name of sql db user that has perms to acces the db
$pass = 'passw0rd'; //password for the user
$charset = 'utf8mb4';
// --- PDO setup ---
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Check if the user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
// If not logged in, redirect to the login page
header("Location: login.php");
exit; // Stop further script execution
}
// Get the logged-in user's email from the session
$email = $_SESSION['email'];
$id = $_SESSION['id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<link rel="stylesheet" href="color.css">
<style>
/* Simple styling for the top-right user display and sign out button */
.logout-btn {
background-color: #f44336; /* Red color for the sign-out button */
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 5px;
}
.logout-btn:hover {
background-color: #d32f2f; /* Darker red when hovered */
}
</style>
</head>
<body>
<!-- Displaying the logged-in user's email at the top right -->
<div class="div" style="width:auto;min-width:200px;position: absolute; top: 0px; right: 0px">
<p>Logged in as: <br><strong><?= htmlspecialchars($email) ?></strong></p>
<button style="width:60%;margin-left:20%;margin-right:20%" class="logout-btn" onclick="confirmLogout()">Sign Out</button>
</div>
<!-- Displaying the Admin Panel -->
<?php
// Check if the user is allowed to view the admin panel
// Assuming $perms is set to the logged-in user's permission level
$loggedInUserPerms = $_SESSION['perms']; // Adjust this based on how your session works
if ($loggedInUserPerms != 0) {
// If the user doesn't have perms == 0, hide the admin panel
echo '<div style="position: absolute; right: 32px; top: 50%;">You do not have permission to view the admin panel.</div>';
} else {
// Admin Panel visible to users with perms == 0
?>
<div style="position: fixed; right: 16px; top: 25%; width: 35%; padding: 8px;background-color: #f5f5f5; border: 2px solid #000;">
<h3>Admin Panel</h3>
<?php
// Handle POST requests for adding, deleting, and editing rows
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check what action is being performed
if (isset($_POST['add_user'])) {
// Add a new user
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$perms = $_POST['perms'];
$stmt = $pdo->prepare("INSERT INTO users (email, password, perms) VALUES (?, ?, ?)");
if ($stmt->execute([$email, $password, $perms])) {
echo "<p style='color: green;'>User added successfully!</p>";
} else {
echo "<p style='color: red;'>Failed to add user.</p>";
}
}
if (isset($_POST['delete_user'])) {
// Get the user ID to delete
$user_id = $_POST['id'];
if ($user_id == $id) {
echo "<p style='color: red;'>Cannot delete your own account ID: {$user_id}. Deletion failed.</p>";
} else {
// Prepare the DELETE query
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
// Execute the query
$stmt->execute([$user_id]);
// Check how many rows were affected
if ($stmt->rowCount() > 0) {
echo "<p style='color: green;'>User deleted successfully!</p>";
} else {
echo "<p style='color: red;'>No user found with ID {$user_id}. Deletion failed.</p>";
}
}
}
if (isset($_POST['edit_perms'])) {
// Get the user ID and the new permissions
$user_id = $_POST['id'];
$new_perms = $_POST['perms'];
if ($user_id == $id) {
echo "<p style='color: red;'>Cannot edit your own account ID: {$user_id}. Permissions not updated.</p>";
} else {
// Prepare the UPDATE query
$stmt = $pdo->prepare("UPDATE users SET perms = ? WHERE id = ?");
// Execute the query
$stmt->execute([$new_perms, $user_id]);
// Check how many rows were affected
if ($stmt->rowCount() > 0) {
echo "<p style='color: green;'>Permissions updated successfully!</p>";
} else {
echo "<p style='color: red;'>No user found with ID: {$user_id}. Permissions not updated.</p>";
}
}
}
}
// Display the form for adding a new user
?>
<div class="mycontainer">
<div>
<h4>Add User</h4>
<form method="POST">
<label for="email">Email:</labe><br>
<input type="email" name="email" placeholder="user@example.com" required><br><br>
<label for="password">Password:</labe><br>
<input type="password" name="password" placeholder="passw0rd" required><br><br>
<label for="perms">Permissions (0 or 1):</label><br>
<input type="number" name="perms" min="0" max="1" placeholder="1" required><br><br>
<button type="submit" name="add_user">Add User</button>
</form>
</div>
<div>
<h4>Delete User</h4>
<form method="POST">
<label for="id">ID:</labe><br>
<input type="id" name="id" placeholder="2" required><br><br>
<button type="submit" name="delete_user">Delete User</button>
</form>
</div>
<div>
<h4>Edit User Perms</h4>
<form method="POST">
<label for="id">ID:</labe><br>
<input type="id" name="id" placeholder="2" required><br><br>
<label for="perms">Permissions (0 or 1):</label><br>
<input type="number" name="perms" min="0" max="1" placeholder="1" required><br><br>
<button type="submit" name="edit_perms">Edit User Perms</button>
</form>
</div>
</div> <!--mycontainer-->
</div>
<?php
}
?>
<h2>Dashboard</h2>
<?php
//Debugging output
//echo '<pre style="">';
//print_r($_SESSION);
//echo '</pre>';
$perms = $_SESSION['perms'];
// Fetch users from the database
$stmt = $pdo->query('SELECT * FROM users');
// Check if there are any results
if ($stmt->rowCount() > 0) {
// Output table header
echo "<table border='1' cellpadding='8' cellspacing='0' style='width:60%;border-collapse: collapse;'>";
echo "<thead><tr><th>ID</th><th>Email</th>";
// Show password and perms columns only if logged-in user has perms 0
if ($perms === 0) {
echo "<th>Password (Hashed)</th><th>Perms</th>";
}
echo "</tr></thead>";
echo "<tbody>";
// Loop through the results and output each row
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['email']}</td>";
// Show password and perms only if logged-in user has perms 0
if ($perms === 0) {
echo "<td>{$row['password']}</td>";
echo "<td>{$row['perms']}</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
} else {
echo "No users found.";
}
?>
<script>
function confirmLogout() {
if (confirm("Are you sure you want to log out?")) {
window.location.href = "logout.php";
}
}
</script>
</body>
</html>