-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-security-users.php
More file actions
470 lines (433 loc) · 18.3 KB
/
Copy pathadmin-security-users.php
File metadata and controls
470 lines (433 loc) · 18.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
include 'security-check.php';
include 'db_connect.php';
require_once 'uppercase-sanitizer.php';
// Admin-only access
if (!isset($_SESSION['staff_user']) || $_SESSION['role'] != 'Admin') {
header("Location: login.php"); exit();
}
// FIX: Removed runtime CREATE TABLE — DDL belongs in a one-time install/migration script,
// not in request-handling code. The security_users table already exists in the schema.
// ── ADD SECURITY USER ─────────────────────────────────────────
if (isset($_POST['add_security'])) {
// FIX: Prepared statements — no more real_escape_string + string interpolation
$username = trim($_POST['username'] ?? '');
$password = trim($_POST['password'] ?? '');
$full_name = trim($_POST['full_name'] ?? '');
$gate = trim($_POST['gate_location'] ?? 'Main Gate');
if ($username && $password && $full_name) {
$check = executeQuery($conn, "SELECT id FROM security_users WHERE username = ?", 's', [$username]);
if ($check && mysqli_num_rows($check) > 0) {
echo "<script>showToast('❌ Username already exists!', 'error', 3000);</script>";
} else {
$hashed = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
$ins = mysqli_prepare($conn, "INSERT INTO security_users (username, password, full_name, gate_location) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($ins, 'ssss', $username, $hashed, $full_name, $gate);
mysqli_stmt_execute($ins);
mysqli_stmt_close($ins);
echo "<script>showToast('✅ Security User Added!', 'success', 3000); setTimeout(() => window.location='admin-security-users.php', 3000);</script>";
}
} else {
echo "<script>showToast('❌ Please fill all required fields!', 'error', 3000);</script>";
}
}
// ── UPDATE PASSWORD ───────────────────────────────────────────
if (isset($_POST['update_password'])) {
// FIX: Prepared statement
$id = intval($_POST['user_id']);
$new_pass = trim($_POST['new_password'] ?? '');
$hashed = password_hash($new_pass, PASSWORD_BCRYPT, ['cost' => 12]);
$stmt = mysqli_prepare($conn, "UPDATE security_users SET password = ? WHERE id = ?");
mysqli_stmt_bind_param($stmt, 'si', $hashed, $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo "<script>showToast('✅ Password Updated!', 'success', 3000);</script>";
}
// ── TOGGLE ACTIVE STATUS ──────────────────────────────────────
if (isset($_POST['toggle_status'])) {
// FIX: Prepared statement
$id = intval($_POST['user_id']);
$status = intval($_POST['current_status']) ? 0 : 1;
$stmt = mysqli_prepare($conn, "UPDATE security_users SET is_active = ? WHERE id = ?");
mysqli_stmt_bind_param($stmt, 'ii', $status, $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("Location: admin-security-users.php"); exit();
}
// ── DELETE USER ───────────────────────────────────────────────
if (isset($_POST['delete_user'])) {
// FIX: Prepared statement
$id = intval($_POST['user_id']);
$stmt = mysqli_prepare($conn, "DELETE FROM security_users WHERE id = ?");
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("Location: admin-security-users.php"); exit();
}
// Fetch all security users
$users = mysqli_query($conn, "SELECT * FROM security_users ORDER BY created_at DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Security Users - Admin</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="toast.css">
<script src="toast.js"></script>
<style>
/* ── Microsoft Fluent Override ── */
:root {
--ms-blue: #0078d4;
--ms-bg: #faf9f7;
--ms-card: #ffffff;
--ms-border: #e1dfdd;
--ms-text: #201f1e;
--ms-muted: #8a8886;
--ms-shadow: 0 1.6px 3.6px rgba(0,0,0,.07), 0 0.3px 0.9px rgba(0,0,0,.05);
}
body {
background: var(--ms-bg) !important;
color: var(--ms-text) !important;
font-family: 'Segoe UI', 'DM Sans', sans-serif !important;
}
.card, .action-card, .staff-card, .role-card, .stat-card, .choice-card {
background: var(--ms-card) !important;
border: 1px solid var(--ms-border) !important;
border-radius: 8px !important;
box-shadow: var(--ms-shadow) !important;
color: var(--ms-text) !important;
}
.card:hover, .action-card:hover, .staff-card:hover {
box-shadow: 0 3.2px 7.2px rgba(0,0,0,.09) !important;
transform: translateY(-2px) !important;
}
.welcome-banner, .dashboard-header {
background: linear-gradient(135deg, #0f3460, #16213e) !important;
}
thead tr, thead th {
background: #0078d4 !important;
}
.btn-primary, button[type="submit"]:not(.btn-danger):not(.btn-warning) {
background: #0078d4 !important;
border-color: #0078d4 !important;
}
.btn-primary:hover, button[type="submit"]:not(.btn-danger):not(.btn-warning):hover {
background: #106ebe !important;
}
a { color: #0078d4; }
a:hover { color: #005a9e; }
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #f0fdf4; font-family: 'Segoe UI', Arial, sans-serif; }
.container { max-width: 1100px; margin: 30px auto; padding: 0 20px; }
h2 {
color: #166534;
font-size: 22px;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
/* Add Form Card */
.add-card {
background: white;
border-radius: 12px;
padding: 24px;
margin-bottom: 24px;
box-shadow: 0 2px 10px rgba(0,0,0,0.08);
border-top: 4px solid #166534;
}
.add-card h3 {
color: #166534;
font-size: 16px;
margin-bottom: 18px;
}
.form-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 16px;
margin-bottom: 16px;
}
.form-group label {
display: block;
font-weight: 600;
font-size: 13px;
color: #555;
margin-bottom: 6px;
}
.form-group input, .form-group select {
width: 100%;
padding: 10px 12px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 14px;
}
.form-group input:focus {
outline: none;
border-color: #166534;
}
.password-wrapper {
position: relative;
}
.password-wrapper input { padding-right: 40px; }
.eye-icon {
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
color: #888;
}
.btn-add {
width: 100%;
padding: 12px;
background: #166534;
color: white;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 700;
cursor: pointer;
}
.btn-add:hover { background: #14532d; }
/* Users Table */
.table-card {
background: white;
border-radius: 12px;
padding: 24px;
box-shadow: 0 2px 10px rgba(0,0,0,0.08);
}
.table-card h3 {
color: #166534;
font-size: 16px;
margin-bottom: 16px;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 14px;
}
th {
background: #f0fdf4;
color: #166534;
padding: 12px;
text-align: left;
font-size: 12px;
text-transform: uppercase;
letter-spacing: 1px;
}
td {
padding: 12px;
border-bottom: 1px solid #f3f4f6;
vertical-align: middle;
}
tr:hover td { background: #fafafa; }
.badge-active { background: #dcfce7; color: #166534; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 700; }
.badge-inactive { background: #fee2e2; color: #dc2626; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 700; }
/* Action buttons */
.btn-sm {
padding: 6px 12px;
border: none;
border-radius: 6px;
font-size: 12px;
font-weight: 600;
cursor: pointer;
margin: 2px;
}
.btn-toggle { background: #fef3c7; color: #92400e; }
.btn-pass { background: #dbeafe; color: #1d4ed8; }
.btn-delete { background: #fee2e2; color: #dc2626; }
.back-btn {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 20px;
background: #166534;
color: white;
text-decoration: none;
border-radius: 8px;
font-size: 13px;
font-weight: 600;
margin-bottom: 20px;
}
/* Password Modal */
.modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 9999; align-items: center; justify-content: center; }
.modal.open { display: flex; }
.modal-box { background: white; border-radius: 12px; padding: 28px; width: 90%; max-width: 380px; }
.modal-box h3 { color: #166534; margin-bottom: 20px; }
.modal-box input { width: 100%; padding: 10px; border: 2px solid #d1d5db; border-radius: 8px; font-size: 14px; margin-bottom: 12px; }
.modal-box input:focus { outline: none; border-color: #166534; }
@media (max-width: 600px) {
.form-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<?php include 'navbar.php'; ?>
<?php include 'lang-switcher.php'; ?>
<div id="toast-container"></div>
<div class="container">
<a href="admin-dashboard.php" class="back-btn">
<i class="fas fa-arrow-left"></i> Back to Dashboard
</a>
<h2><i class="fas fa-shield-alt"></i> Security Users Management</h2>
<!-- Add Security User Form -->
<div class="add-card">
<h3><i class="fas fa-user-plus"></i> Add New Security User</h3>
<form method="POST">
<div class="form-grid">
<div class="form-group">
<label>Full Name <span style="color:red;">*</span></label>
<input type="text" name="full_name" placeholder="e.g. Ramesh Kumar" required>
</div>
<div class="form-group">
<label>Username <span style="color:red;">*</span></label>
<input type="text" name="username" placeholder="e.g. gate1" required>
</div>
<div class="form-group">
<label>Password <span style="color:red;">*</span></label>
<div class="password-wrapper">
<input type="password" name="password" id="newPass" placeholder="Enter password" required>
<span class="eye-icon" onclick="toggleEye('newPass', this)">
<i class="fas fa-eye"></i>
</span>
</div>
</div>
<div class="form-group">
<label>Gate Location</label>
<input type="text" name="gate_location" placeholder="e.g. Main Gate, North Gate" value="Main Gate">
</div>
</div>
<button type="submit" name="add_security" class="btn-add">
<i class="fas fa-plus"></i> Add Security User
</button>
</form>
</div>
<!-- Users List -->
<div class="table-card">
<h3><i class="fas fa-list"></i> All Security Users (<?= mysqli_num_rows($users) ?>)</h3>
<div style="overflow-x:auto;">
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Username</th>
<th>Gate Location</th>
<th>Status</th>
<th>Last Login</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while ($u = mysqli_fetch_assoc($users)):
?>
<tr>
<td><?= $i++ ?></td>
<td><strong><?= htmlspecialchars($u['full_name']) ?></strong></td>
<td><code><?= htmlspecialchars($u['username']) ?></code></td>
<td><i class="fas fa-map-marker-alt" style="color:#166534;"></i> <?= htmlspecialchars($u['gate_location']) ?></td>
<td>
<?php if ($u['is_active']): ?>
<span class="badge-active">✅ Active</span>
<?php else: ?>
<span class="badge-inactive">❌ Inactive</span>
<?php endif; ?>
</td>
<td style="font-size:12px; color:#64748b;">
<?= $u['last_login'] ? date('d-M-Y h:i A', strtotime($u['last_login'])) : 'Never' ?>
</td>
<td>
<!-- Toggle Active -->
<form method="POST" style="display:inline;">
<input type="hidden" name="user_id" value="<?= $u['id'] ?>">
<input type="hidden" name="current_status" value="<?= $u['is_active'] ?>">
<button type="submit" name="toggle_status" class="btn-sm btn-toggle"
title="<?= $u['is_active'] ? 'Deactivate' : 'Activate' ?>">
<i class="fas fa-<?= $u['is_active'] ? 'ban' : 'check' ?>"></i>
<?= $u['is_active'] ? 'Deactivate' : 'Activate' ?>
</button>
</form>
<!-- Change Password -->
<button class="btn-sm btn-pass" onclick="openPassModal(<?= $u['id'] ?>, '<?= htmlspecialchars($u['full_name']) ?>')">
<i class="fas fa-key"></i> Password
</button>
<!-- Delete -->
<form method="POST" style="display:inline;"
onsubmit="return confirm('Delete <?= htmlspecialchars($u['full_name']) ?>? This cannot be undone!')">
<input type="hidden" name="user_id" value="<?= $u['id'] ?>">
<button type="submit" name="delete_user" class="btn-sm btn-delete">
<i class="fas fa-trash"></i> Delete
</button>
</form>
</td>
</tr>
<?php endwhile; ?>
<?php if (mysqli_num_rows($users) == 0): ?>
<tr>
<td colspan="7" style="text-align:center; padding:30px; color:#64748b;">
<i class="fas fa-shield-alt" style="font-size:30px; margin-bottom:10px; display:block;"></i>
No security users yet. Add one above!
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Password Modal -->
<div id="passModal" class="modal">
<div class="modal-box">
<h3><i class="fas fa-key"></i> Change Password</h3>
<p id="modal-username" style="font-size:13px; color:#64748b; margin-bottom:16px;"></p>
<form method="POST">
<input type="hidden" name="user_id" id="modal-user-id">
<div class="password-wrapper" style="margin-bottom:12px;">
<input type="password" name="new_password" id="modalPass" placeholder="New password" required>
<span class="eye-icon" onclick="toggleEye('modalPass', this)">
<i class="fas fa-eye"></i>
</span>
</div>
<button type="submit" name="update_password" class="btn-add">
<i class="fas fa-save"></i> Update Password
</button>
</form>
<button onclick="closeModal()" style="width:100%; margin-top:10px; padding:10px; background:#e5e7eb; border:none; border-radius:8px; cursor:pointer; font-weight:600;">
Cancel
</button>
</div>
</div>
<script>
// Toggle password visibility
function toggleEye(inputId, icon) {
const input = document.getElementById(inputId);
const i = icon.querySelector('i');
if (input.type === 'password') {
input.type = 'text';
i.className = 'fas fa-eye-slash';
} else {
input.type = 'password';
i.className = 'fas fa-eye';
}
}
// Password modal
function openPassModal(userId, name) {
document.getElementById('modal-user-id').value = userId;
document.getElementById('modal-username').textContent = 'User: ' + name;
document.getElementById('passModal').classList.add('open');
}
function closeModal() {
document.getElementById('passModal').classList.remove('open');
}
// Close modal on outside click
document.getElementById('passModal').addEventListener('click', function(e) {
if (e.target === this) closeModal();
});
</script>
<?php include 'staff-footer.php'; ?>
</body>
</html>