You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.9 KiB

<?php
require_once __DIR__ . '/../src/storage/UserRepository.php';
use MailServer\Storage\UserRepository;
session_start();
// 简单身份验证
if (!isset($_SESSION['admin_logged_in'])) {
header('Location: login.php');
exit;
}
$repo = new UserRepository();
$action = $_GET['action'] ?? '';
$message = '';
// 处理操作
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
switch ($_POST['action']) {
case 'create':
if ($repo->create($_POST['username'], $_POST['password'],
isset($_POST['is_admin']), $_POST['mailbox_size'])) {
$message = '用户创建成功';
}
break;
case 'toggle':
$repo->toggleActive($_POST['user_id'], $_POST['active']);
break;
case 'delete':
$repo->delete($_POST['user_id']);
break;
}
}
$users = $repo->getAll();
?>
<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<style>
body { font-family: Arial; margin: 20px; }
.success { color: green; padding: 10px; background: #e8f5e8; }
table { border-collapse: collapse; width: 100%; margin: 20px 0; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background: #f5f5f5; }
.form-group { margin: 10px 0; }
input, select { padding: 5px; }
</style>
</head>
<body>
<h1> 邮件服务器用户管理</h1>
<?php if ($message): ?>
<div class="success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<h2>创建新用户</h2>
<form method="POST">
<input type="hidden" name="action" value="create">
<div class="form-group">
<input type="email" name="username" placeholder="邮箱地址" required>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="密码" required>
</div>
<div class="form-group">
<label>
<input type="checkbox" name="is_admin"> 管理员
</label>
</div>
<div class="form-group">
<input type="number" name="mailbox_size" value="100" min="10" max="10240"> MB
</div>
<button type="submit">创建用户</button>
</form>
<h2>用户列表</h2>
<table>
<thead>
<tr>
<th>ID</th><th>邮箱</th><th>类型</th><th>状态</th><th>容量</th><th>创建时间</th><th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo htmlspecialchars($user['username']); ?></td>
<td><?php echo $user['is_admin'] ? '管理员' : '普通'; ?></td>
<td><?php echo $user['is_active'] ? '激活' : '禁用'; ?></td>
<td><?php echo $user['max_mailbox_size']; ?> MB</td>
<td><?php echo $user['created_at']; ?></td>
<td>
<form method="POST" style="display:inline;">
<input type="hidden" name="action" value="toggle">
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
<input type="hidden" name="active" value="<?php echo $user['is_active'] ? '0' : '1'; ?>">
<button type="submit"><?php echo $user['is_active'] ? '禁用' : '启用'; ?></button>
</form>
<form method="POST" style="display:inline;">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
<button type="submit" onclick="return confirm('确定删除?')">删除</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>