forked from clarkezyz/kincircle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_user_management.php
More file actions
393 lines (345 loc) · 16.7 KB
/
Copy pathadmin_user_management.php
File metadata and controls
393 lines (345 loc) · 16.7 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
<?php
require_once "config.php";
requireLogin();
// Only existing admins can access this page
if (!isAdmin()) {
header('Location: index.php?error=admin_required');
exit;
}
$message = '';
$messageType = '';
// Handle admin toggle
if ($_POST['action'] ?? '' === 'toggle_admin') {
$userId = (int)$_POST['user_id'];
$makeAdmin = $_POST['make_admin'] === '1' ? 1 : 0;
try {
$stmt = $pdo->prepare("UPDATE users SET is_admin = ? WHERE id = ?");
$stmt->execute([$makeAdmin, $userId]);
$action = $makeAdmin ? 'granted' : 'removed';
$message = "Admin privileges {$action} successfully!";
$messageType = 'success';
} catch (Exception $e) {
$message = "Error updating admin status: " . $e->getMessage();
$messageType = 'error';
}
}
// Handle user deletion
if ($_POST['action'] ?? '' === 'delete_user') {
$userId = (int)$_POST['user_id'];
// Prevent admins from deleting themselves
if ($userId === $_SESSION['user_id']) {
$message = "You cannot delete your own account! Please have another admin do this if necessary.";
$messageType = 'error';
} else {
try {
// Delete user from all related tables
// Column names verified against rwdata_actual.sql (Dec 2025)
$pdo->beginTransaction();
// === TABLES WITH CASCADE DELETE (handled automatically, but explicit is safer) ===
// photo_tags: tagged_by_user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM photo_tags WHERE tagged_by_user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// content_tags: tagged_by_user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM content_tags WHERE tagged_by_user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// people: created_by_user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM people WHERE created_by_user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// event_commitments: user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM event_commitments WHERE user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// private_messages: sender_id, recipient_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM private_messages WHERE sender_id = ? OR recipient_id = ?");
$stmt->execute([$userId, $userId]);
} catch (Exception $e) { /* Table might not exist */ }
// family_inferences: person_user_id, related_user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM family_inferences WHERE person_user_id = ? OR related_user_id = ?");
$stmt->execute([$userId, $userId]);
} catch (Exception $e) { /* Table might not exist */ }
// family_relationships: claimer_user_id, claimed_user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM family_relationships WHERE claimer_user_id = ? OR claimed_user_id = ?");
$stmt->execute([$userId, $userId]);
} catch (Exception $e) { /* Table might not exist */ }
// relationship_notifications: user_id (has ON DELETE CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM relationship_notifications WHERE user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// === TABLES WITHOUT CASCADE (must handle manually) ===
// messages: posted_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM messages WHERE posted_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// message_replies: posted_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM message_replies WHERE posted_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// photo_submissions: uploader_id (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM photo_submissions WHERE uploader_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// memorial_submissions: submitted_by (NO CASCADE) - NOT submitter_id!
try {
$stmt = $pdo->prepare("DELETE FROM memorial_submissions WHERE submitted_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// memorials: created_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM memorials WHERE created_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// calendar_events: created_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM calendar_events WHERE created_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// event_submissions: submitter_id (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM event_submissions WHERE submitter_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// recipe_modifications: modified_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM recipe_modifications WHERE modified_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// recipes: created_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM recipes WHERE created_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// albums: created_by (NO CASCADE)
try {
$stmt = $pdo->prepare("DELETE FROM albums WHERE created_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// === SET NULL for reviewer/admin references ===
// admin_messages: resolved_by
try {
$stmt = $pdo->prepare("UPDATE admin_messages SET resolved_by = NULL WHERE resolved_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// pending_users: reviewed_by
try {
$stmt = $pdo->prepare("UPDATE pending_users SET reviewed_by = NULL WHERE reviewed_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// photo_submissions: reviewed_by
try {
$stmt = $pdo->prepare("UPDATE photo_submissions SET reviewed_by = NULL WHERE reviewed_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// event_submissions: reviewed_by
try {
$stmt = $pdo->prepare("UPDATE event_submissions SET reviewed_by = NULL WHERE reviewed_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// memorial_submissions: reviewed_by
try {
$stmt = $pdo->prepare("UPDATE memorial_submissions SET reviewed_by = NULL WHERE reviewed_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// family_relationships: revoked_by (has ON DELETE SET NULL, but be explicit)
try {
$stmt = $pdo->prepare("UPDATE family_relationships SET revoked_by = NULL WHERE revoked_by = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// relationship_notifications: claimer_user_id (has ON DELETE SET NULL)
try {
$stmt = $pdo->prepare("UPDATE relationship_notifications SET claimer_user_id = NULL WHERE claimer_user_id = ?");
$stmt->execute([$userId]);
} catch (Exception $e) { /* Table might not exist */ }
// Finally delete from users table
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
$stmt->execute([$userId]);
$pdo->commit();
$message = "User deleted successfully! All related content has been removed.";
$messageType = 'success';
} catch (Exception $e) {
$pdo->rollBack();
$message = "Error deleting user: " . $e->getMessage();
$messageType = 'error';
}
}
}
// Get all users
try {
$stmt = $pdo->query("SELECT id, username, display_name, email, is_admin, created_at FROM users ORDER BY created_at DESC");
$users = $stmt->fetchAll();
} catch (Exception $e) {
$message = "Error loading users: " . $e->getMessage();
$messageType = 'error';
$users = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin User Management - Reed & Weaver Family Hub</title>
<link rel="stylesheet" href="css/main.css">
<style>
.user-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
.user-table th,
.user-table td {
padding: 12px;
border: 1px solid var(--glass-border);
text-align: left;
}
.user-table th {
background: var(--glass-bg);
font-weight: bold;
}
.admin-badge {
background: #10b981;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
}
.regular-badge {
background: #6b7280;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
}
.toggle-form {
display: inline-block;
margin-left: 10px;
}
.message {
padding: 10px;
border-radius: 8px;
margin: 10px 0;
}
.message.success {
background: #d1fae5;
color: #065f46;
border: 1px solid #10b981;
}
.message.error {
background: #fee2e2;
color: #991b1b;
border: 1px solid #ef4444;
}
.current-user {
background: #fef3c7;
}
.warning {
background: #fef3c7;
color: #92400e;
padding: 15px;
border-radius: 8px;
margin: 20px 0;
border: 1px solid #f59e0b;
}
</style>
</head>
<body>
<main class="main-content">
<div class="container">
<h1 class="page-title">🔑 Admin User Management</h1>
<p>Manage admin privileges and user accounts for family members.</p>
<?php if ($message): ?>
<div class="message <?= $messageType ?>">
<?= htmlspecialchars($message) ?>
</div>
<?php endif; ?>
<div class="warning">
<strong>⚠️ Important:</strong> Only grant admin privileges to family members you trust completely.
Admins can add/remove content, manage users, and access all family data.
</div>
<table class="user-table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Display Name</th>
<th>Email</th>
<th>Admin Status</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr <?= $_SESSION['user_id'] == $user['id'] ? 'class="current-user"' : '' ?>>
<td><?= $user['id'] ?></td>
<td>
<?= htmlspecialchars($user['username']) ?>
<?php if ($_SESSION['user_id'] == $user['id']): ?>
<small>(You)</small>
<?php endif; ?>
</td>
<td><?= htmlspecialchars($user['display_name']) ?></td>
<td><?= htmlspecialchars($user['email'] ?? 'Not provided') ?></td>
<td>
<?php if ($user['is_admin']): ?>
<span class="admin-badge">Admin</span>
<?php else: ?>
<span class="regular-badge">Regular User</span>
<?php endif; ?>
</td>
<td><?= date('M j, Y', strtotime($user['created_at'])) ?></td>
<td>
<form method="POST" class="toggle-form" style="display: inline;">
<input type="hidden" name="action" value="toggle_admin">
<input type="hidden" name="user_id" value="<?= $user['id'] ?>">
<?php if ($user['is_admin']): ?>
<input type="hidden" name="make_admin" value="0">
<button type="submit" class="btn btn-outline"
<?= $_SESSION['user_id'] == $user['id'] ? 'onclick="return confirm(\'Remove your own admin privileges? You won\\\'t be able to access this page after!\')"' : '' ?>>
Remove Admin
</button>
<?php else: ?>
<input type="hidden" name="make_admin" value="1">
<button type="submit" class="btn btn-primary">
Make Admin
</button>
<?php endif; ?>
</form>
<?php if ($_SESSION['user_id'] != $user['id']): ?>
<form method="POST" class="toggle-form" style="display: inline; margin-left: 5px;">
<input type="hidden" name="action" value="delete_user">
<input type="hidden" name="user_id" value="<?= $user['id'] ?>">
<button type="submit" class="btn btn-outline"
style="background: #fee2e2; color: #991b1b; border-color: #ef4444;"
onclick="return confirm('Are you sure you want to delete <?= htmlspecialchars($user['display_name']) ?>? This will permanently delete all their content and cannot be undone!')">
Delete User
</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div style="margin-top: 20px;">
<a href="admin.php" class="btn btn-secondary">Back to Admin Dashboard</a>
<a href="index.php" class="btn btn-primary">Back to Main Dashboard</a>
</div>
</div>
</main>
<script src="js/header.js"></script>
</body>
</html>