-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-management.php
More file actions
254 lines (225 loc) · 10.4 KB
/
Copy pathuser-management.php
File metadata and controls
254 lines (225 loc) · 10.4 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
<?php
require_once 'includes/config.php';
require_once 'includes/auth-functions.php';
require_once 'includes/user-functions.php';
require_once 'includes/log-functions.php';
require_once 'includes/validation-functions.php';
// Start session and check admin authentication
session_start();
require_authentication();
require_admin_role();
$current_user = get_current_user();
$action = $_GET['action'] ?? 'list';
$edit_user_id = $_GET['edit'] ?? '';
$success_message = '';
$error_message = '';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form_action = $_POST['action'] ?? '';
if ($form_action === 'create_user') {
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? 'user';
// Validate input
if (empty($username) || empty($password)) {
$error_message = "Username and password are required.";
} elseif (user_exists($username)) {
$error_message = "Username already exists.";
} else {
$result = create_user($username, $password, $email, $role);
if ($result['success']) {
$success_message = "User created successfully.";
log_admin_action("User created: " . $username . " by admin: " . $current_user['username']);
} else {
$error_message = $result['message'];
}
}
} elseif ($form_action === 'update_user') {
$user_id = $_POST['user_id'] ?? '';
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$role = $_POST['role'] ?? 'user';
$new_password = $_POST['new_password'] ?? '';
if (!empty($user_id)) {
$update_data = [
'username' => $username,
'email' => $email,
'role' => $role
];
if (!empty($new_password)) {
$update_data['password'] = $new_password;
}
$result = update_user($user_id, $update_data);
if ($result['success']) {
$success_message = "User updated successfully.";
log_admin_action("User updated: " . $username . " by admin: " . $current_user['username']);
} else {
$error_message = $result['message'];
}
}
}
}
// Get user for editing
$edit_user = null;
if (!empty($edit_user_id)) {
$edit_user = get_user_by_id($edit_user_id);
$action = 'edit';
}
// Get all users for listing
$all_users = get_all_users();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Management - FileServer</title>
<link rel="stylesheet" href="assets/css/main.css">
<link rel="stylesheet" href="assets/css/admin.css">
<link rel="stylesheet" href="assets/css/forms.css">
</head>
<body>
<?php include 'templates/header.html'; ?>
<?php include 'templates/navigation.html'; ?>
<div class="container">
<div class="page-header">
<h1>User Management</h1>
<div class="page-actions">
<a href="admin.php?action=users" class="btn btn-secondary">Back to Admin</a>
<?php if ($action !== 'create'): ?>
<a href="?action=create" class="btn btn-primary">Add New User</a>
<?php endif; ?>
</div>
</div>
<?php if (!empty($success_message)): ?>
<div class="alert alert-success">
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<?php if (!empty($error_message)): ?>
<div class="alert alert-error">
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php endif; ?>
<?php if ($action === 'create'): ?>
<!-- Create User Form -->
<div class="form-section">
<h2>Create New User</h2>
<form method="POST" class="user-form">
<input type="hidden" name="action" value="create_user">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required class="form-control"
placeholder="Enter username">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" class="form-control"
placeholder="Enter email (optional)">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required class="form-control"
placeholder="Enter password">
</div>
<div class="form-group">
<label for="role">Role:</label>
<select id="role" name="role" class="form-control">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Create User</button>
<a href="?action=list" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<?php elseif ($action === 'edit' && $edit_user): ?>
<!-- Edit User Form -->
<div class="form-section">
<h2>Edit User: <?php echo htmlspecialchars($edit_user['username']); ?></h2>
<form method="POST" class="user-form">
<input type="hidden" name="action" value="update_user">
<input type="hidden" name="user_id" value="<?php echo htmlspecialchars($edit_user['id']); ?>">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required class="form-control"
value="<?php echo htmlspecialchars($edit_user['username']); ?>">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" class="form-control"
value="<?php echo htmlspecialchars($edit_user['email'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="new_password">New Password:</label>
<input type="password" id="new_password" name="new_password" class="form-control"
placeholder="Leave blank to keep current password">
</div>
<div class="form-group">
<label for="role">Role:</label>
<select id="role" name="role" class="form-control">
<option value="user" <?php echo $edit_user['role'] === 'user' ? 'selected' : ''; ?>>User</option>
<option value="admin" <?php echo $edit_user['role'] === 'admin' ? 'selected' : ''; ?>>Admin</option>
</select>
</div>
<div class="user-stats">
<h3>User Statistics</h3>
<div class="stats-grid">
<div class="stat-item">
<span class="stat-label">User ID:</span>
<span class="stat-value"><?php echo htmlspecialchars($edit_user['id']); ?></span>
</div>
<div class="stat-item">
<span class="stat-label">Created:</span>
<span class="stat-value"><?php echo date('M j, Y g:i A', strtotime($edit_user['created_at'])); ?></span>
</div>
<div class="stat-item">
<span class="stat-label">Files:</span>
<span class="stat-value"><?php echo count(get_user_files($edit_user['id'])); ?></span>
</div>
<div class="stat-item">
<span class="stat-label">Storage:</span>
<span class="stat-value"><?php echo format_file_size(calculate_user_storage($edit_user['id'])); ?></span>
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Update User</button>
<a href="?action=list" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<?php else: ?>
<!-- List Users -->
<div class="users-section">
<h2>All Users</h2>
<div class="user-stats-summary">
<div class="summary-stat">
<span class="summary-number"><?php echo count($all_users); ?></span>
<span class="summary-label">Total Users</span>
</div>
<div class="summary-stat">
<span class="summary-number">
<?php echo count(array_filter($all_users, function($u) { return $u['role'] === 'admin'; })); ?>
</span>
<span class="summary-label">Administrators</span>
</div>
<div class="summary-stat">
<span class="summary-number">
<?php echo count(array_filter($all_users, function($u) { return $u['role'] === 'user'; })); ?>
</span>
<span class="summary-label">Regular Users</span>
</div>
</div>
<?php include 'templates/user-list.html'; ?>
</div>
<?php endif; ?>
</div>
<?php include 'templates/footer.html'; ?>
<script src="assets/js/main.js"></script>
<script src="assets/js/admin.js"></script>
</body>
</html>