v4.1 管理员邮箱绑定与SMTP找回密码

1. users 表新增 email 列,管理员添加/编辑支持绑定邮箱,修改密码页支持自助绑定
2. 新增 SMTP 邮件发送工具与配置(FRONTEND_URL/SMTP_* 等)
3. 新增忘记密码/重置密码公开接口与页面:一次性令牌、30分钟有效、请求限流、重置后强制重新登录
4. 登录与 /api/auth/me 返回邮箱并同步 PHP Session
5. 版本升级至4.1(sql/upgrades/v4.1.sql)
This commit is contained in:
canglan committed 2026-09-17 20:49:15 +08:00
1 parent 3588b4c28c
commit 058dee9dc4
31 files changed
+762 -34

No files matched your search

+10 -1
View File
@@ -34,7 +34,7 @@ include __DIR__ . '/../includes/header.php';
<div class="table-wrapper">
<table class="table">
<thead>
<tr><th>用户名</th><th>姓名</th><th>权限组</th><th>加减分上限</th><th>状态</th><th>操作</th></tr>
<tr><th>用户名</th><th>姓名</th><th>绑定邮箱</th><th>权限组</th><th>加减分上限</th><th>状态</th><th>操作</th></tr>
</thead>
<tbody id="adminList"></tbody>
</table>
@@ -72,6 +72,11 @@ include __DIR__ . '/../includes/header.php';
<label>姓名</label>
<input type="text" id="adminRealName" required placeholder="真实姓名">
</div>
<div class="form-group">
<label>绑定邮箱</label>
<input type="email" id="adminEmail" maxlength="100" placeholder="选填,用于找回密码">
<small>绑定后可通过登录页"忘记密码"重置密码</small>
</div>
<div class="form-group">
<label>密码</label>
<input type="text" id="adminPassword" placeholder="留空则自动生成">
@@ -108,6 +113,10 @@ include __DIR__ . '/../includes/header.php';
<label>姓名</label>
<input type="text" id="editAdminRealName" required>
</div>
<div class="form-group">
<label>绑定邮箱</label>
<input type="email" id="editAdminEmail" maxlength="100" placeholder="留空解除绑定">
</div>
<div class="form-group">
<label>权限组</label>
<select id="editAdminGroupId" required>
+48 -1
View File
@@ -18,7 +18,8 @@ if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'admin') {
}
$page_title = '修改密码';
$role = $_SESSION['role'] ?? '';
$role = current_group_name();
$bound_email = $_SESSION['email'] ?? '';
include __DIR__ . '/../includes/header.php';
?>
@@ -44,6 +45,29 @@ include __DIR__ . '/../includes/header.php';
<button type="submit" class="btn btn-primary">确认修改</button>
</form>
</div>
<div class="card">
<div class="card-title">绑定邮箱(找回密码)</div>
<p style="font-size:13px; color:var(--color-text-secondary); margin-bottom:12px;">
绑定邮箱后,忘记密码时可在登录页通过“忘记密码”向该邮箱发送重置链接。
</p>
<div class="form-group">
<label>当前绑定邮箱</label>
<div id="currentEmail"><?php echo $bound_email ? htmlspecialchars($bound_email) : '未绑定'; ?></div>
</div>
<form id="emailForm">
<div class="form-group">
<label>新邮箱</label>
<input type="email" id="bindEmail" maxlength="100" placeholder="留空则解除绑定">
</div>
<div class="form-group">
<label>当前密码 <span style="color:red;">*</span></label>
<input type="password" id="emailPassword" required>
<small>安全验证:绑定邮箱需要输入当前登录密码</small>
</div>
<button type="submit" class="btn btn-primary">保存邮箱</button>
</form>
</div>
</div>
<script>
@@ -76,6 +100,29 @@ document.getElementById('passwordForm').addEventListener('submit', async (e) =>
showToast(res?.message || '密码修改失败', 'error');
}
});
document.getElementById('emailForm').addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('bindEmail').value.trim();
const password = document.getElementById('emailPassword').value;
if (!password) {
showToast('请输入当前密码', 'warning');
return;
}
const res = await apiPost('/api/auth/bind-email', {
password: password,
email: email
});
if (res && res.success) {
const bound = res.data && res.data.email ? res.data.email : '';
document.getElementById('currentEmail').textContent = bound || '未绑定';
document.getElementById('bindEmail').value = '';
document.getElementById('emailPassword').value = '';
showToast(res.message || '邮箱保存成功');
} else {
showToast(res?.message || '邮箱保存失败', 'error');
}
});
</script>
<?php include __DIR__ . '/../includes/footer.php'; ?>
+1
View File
@@ -159,6 +159,7 @@ $_SESSION['user_id'] = $apiUser['user_id'] ?? $data['user_id'];
$_SESSION['user_type'] = $apiUser['user_type'] ?? $data['user_type'];
$_SESSION['username'] = $apiUser['username'] ?? $data['username'];
$_SESSION['real_name'] = $apiUser['real_name'] ?? ($data['real_name'] ?? '');
$_SESSION['email'] = $apiUser['email'] ?? '';
$_SESSION['role'] = $apiUser['role'] ?? '';
$_SESSION['permissions'] = is_array($apiUser['permissions'] ?? null) ? $apiUser['permissions'] : [];
$_SESSION['group'] = $apiUser['group'] ?? null;
+7 -4
View File
@@ -118,11 +118,12 @@ async function loadAdmins() {
};
const canEdit = !admin.is_locked && admin.group_id;
const editItem = canEdit
? `<a onclick="showEditAdminModal(${admin.user_id}, '${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}', ${admin.group_id})">编辑</a>`
? `<a onclick="showEditAdminModal(${admin.user_id}, '${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}', ${admin.group_id}, '${escapeHtml(admin.email || '')}')">编辑</a>`
: '';
html += `<tr>
<td>${escapeHtml(admin.username)}</td>
<td>${escapeHtml(admin.real_name)}</td>
<td>${admin.email ? escapeHtml(admin.email) : '<span class="text-muted">未绑定</span>'}</td>
<td>${escapeHtml(admin.group_name || admin.role_type || '-')}${admin.is_locked ? ' <span class="subject-status subject-status-active">锁定</span>' : ''}</td>
<td>加${caps.add} / 扣${caps.subtract}</td>
<td><span class="subject-status ${statusClass}">${statusText}</span></td>
@@ -140,18 +141,19 @@ async function loadAdmins() {
</tr>`;
});
if (res.data.admins.length === 0) {
html = '<tr><td colspan="6" style="text-align:center;">暂无管理员</td></tr>';
html = '<tr><td colspan="7" style="text-align:center;">暂无管理员</td></tr>';
}
document.getElementById('adminList').innerHTML = html;
}
}
function showEditAdminModal(userId, username, realName, groupId) {
function showEditAdminModal(userId, username, realName, groupId, email) {
currentEditUserId = userId;
document.getElementById('editAdminUserId').value = userId;
document.getElementById('editAdminUsername').value = username;
document.getElementById('editAdminRealName').value = realName;
document.getElementById('editAdminGroupId').value = groupId || '';
document.getElementById('editAdminEmail').value = email || '';
document.getElementById('editAdminModal').style.display = 'flex';
}
@@ -166,7 +168,8 @@ async function submitEditAdmin() {
const res = await apiPut(`/api/admin/update/${currentEditUserId}`, {
real_name: document.getElementById('editAdminRealName').value.trim(),
group_id: groupId
group_id: groupId,
email: document.getElementById('editAdminEmail').value.trim()
});
if (res && res.success) {
+4 -1
View File
@@ -23,6 +23,7 @@
const realName = document.getElementById('adminRealName').value.trim();
const password = document.getElementById('adminPassword').value;
const groupId = parseInt(document.getElementById('adminGroupId').value);
const email = document.getElementById('adminEmail').value.trim();
if (!username || !realName || !groupId) {
showToast('请填写完整信息', 'warning');
@@ -33,7 +34,8 @@
username: username,
real_name: realName,
password: password || undefined,
group_id: groupId
group_id: groupId,
email: email || undefined
});
if (res && res.success) {
@@ -49,6 +51,7 @@
document.getElementById('adminRealName').value = '';
document.getElementById('adminPassword').value = '';
document.getElementById('adminGroupId').value = '';
document.getElementById('adminEmail').value = '';
loadAdmins();
} else {
showToast(res?.message || '添加失败', 'error');
+1
View File
@@ -120,6 +120,7 @@ function refresh_admin_permissions($force = false): void {
}
$info = $data['data'];
$_SESSION['real_name'] = $info['real_name'] ?? ($_SESSION['real_name'] ?? '');
$_SESSION['email'] = $info['email'] ?? '';
$_SESSION['role'] = $info['role'] ?? '';
$_SESSION['permissions'] = is_array($info['permissions'] ?? null) ? $info['permissions'] : [];
$_SESSION['group'] = $info['group'] ?? null;
+93
View File
@@ -0,0 +1,93 @@
<?php
/**
* 班级操行分管理系统 - 忘记密码
*
* 开发者: Canglan
* 联系方式: admin@sea-studio.top
* 版权归属: Sea Network Technology Studio
* 许可证: MIT License
*
* 版权所有 © Sea Network Technology Studio
*/
require_once __DIR__ . '/config.php';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title><?php echo SITE_NAME; ?> - 忘记密码</title>
<link rel="stylesheet" href="/assets/css/style.css">
</head>
<body>
<div class="login-container">
<div class="login-header">
<h1><?php echo SITE_NAME; ?></h1>
<p>管理员找回密码</p>
</div>
<form id="forgotForm" class="login-form">
<div class="form-group">
<label>管理员账号</label>
<input type="text" id="username" name="username" required autocomplete="off" placeholder="请输入管理员用户名">
<small>重置链接将发送到该账号绑定的邮箱</small>
</div>
<button type="submit" class="btn-login" id="submitBtn">发送重置链接</button>
<div id="tipMsg" class="error-msg" style="display: none;"></div>
<p style="text-align:center; margin-top:14px;">
<a href="/index.php" style="color:#667eea; font-size:14px;">返回登录</a>
</p>
</form>
<div class="login-footer">
<p>&copy; <?php echo date('Y'); ?> Sea Network Technology Studio</p>
<?php if (defined('ICP_ENABLED') && ICP_ENABLED && defined('ICP_NUMBER') && ICP_NUMBER): ?>
<p><a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer"><?php echo htmlspecialchars(ICP_NUMBER); ?></a></p>
<?php endif; ?>
</div>
</div>
<script>
window.API_BASE_URL = '<?php echo API_BASE_URL; ?>';
function showTip(msg, isError) {
const tip = document.getElementById('tipMsg');
tip.textContent = msg;
tip.style.display = 'block';
tip.style.color = isError ? '' : '#38a169';
}
document.getElementById('forgotForm').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('username').value.trim();
if (!username) {
showTip('请输入管理员账号', true);
return;
}
const btn = document.getElementById('submitBtn');
btn.disabled = true;
btn.textContent = '发送中...';
try {
const response = await fetch(`${window.API_BASE_URL}/api/auth/forgot-password`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username })
});
const data = await response.json();
if (data.success) {
showTip(data.message || '重置链接已发送,请查收邮箱', false);
} else {
showTip(data.message || '发送失败', true);
}
} catch (error) {
console.error('找回密码请求失败:', error);
showTip('网络错误,请稍后重试', true);
} finally {
btn.disabled = false;
btn.textContent = '发送重置链接';
}
});
</script>
</body>
</html>
+3
View File
@@ -47,6 +47,9 @@ if (isset($_SESSION['user_id']) && isset($_SESSION['user_type'])) {
<input type="password" id="password" name="password" required placeholder="请输入密码">
</div>
<button type="submit" class="btn-login">登 录</button>
<p style="text-align:center; margin-top:14px;">
<a href="/forgot_password.php" style="color:#667eea; font-size:14px;">忘记密码?(管理员)</a>
</p>
<div id="errorMsg" class="error-msg" style="display: none;"></div>
</form>
+140
View File
@@ -0,0 +1,140 @@
<?php
/**
* 班级操行分管理系统 - 重置密码
*
* 开发者: Canglan
* 联系方式: admin@sea-studio.top
* 版权归属: Sea Network Technology Studio
* 许可证: MIT License
*
* 版权所有 © Sea Network Technology Studio
*/
require_once __DIR__ . '/config.php';
$token = isset($_GET['token']) ? preg_replace('/[^A-Za-z0-9_\-]/', '', $_GET['token']) : '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title><?php echo SITE_NAME; ?> - 重置密码</title>
<link rel="stylesheet" href="/assets/css/style.css">
</head>
<body>
<div class="login-container">
<div class="login-header">
<h1><?php echo SITE_NAME; ?></h1>
<p>重置管理员密码</p>
</div>
<div id="loadingBox" class="login-form" style="text-align:center; color:#888;">正在校验链接...</div>
<form id="resetForm" class="login-form" style="display:none;">
<div class="form-group">
<label>账号</label>
<input type="text" id="accountName" disabled>
</div>
<div class="form-group">
<label>新密码 <span style="color:red;">*</span></label>
<input type="password" id="newPassword" required>
<small>密码长度6-20位,需包含大写字母、小写字母、数字、特殊符号中的至少3种</small>
</div>
<div class="form-group">
<label>确认新密码 <span style="color:red;">*</span></label>
<input type="password" id="confirmPassword" required>
</div>
<button type="submit" class="btn-login" id="submitBtn">确认重置</button>
<div id="tipMsg" class="error-msg" style="display: none;"></div>
</form>
<div id="invalidBox" class="login-form" style="display:none; text-align:center;">
<p style="color:#e53e3e; margin-bottom:16px;">重置链接无效或已过期,请重新申请。</p>
<a href="/forgot_password.php" style="color:#667eea;">重新申请</a>
</div>
<div class="login-footer">
<p>&copy; <?php echo date('Y'); ?> Sea Network Technology Studio</p>
<?php if (defined('ICP_ENABLED') && ICP_ENABLED && defined('ICP_NUMBER') && ICP_NUMBER): ?>
<p><a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer"><?php echo htmlspecialchars(ICP_NUMBER); ?></a></p>
<?php endif; ?>
</div>
</div>
<script>
window.API_BASE_URL = '<?php echo API_BASE_URL; ?>';
const RESET_TOKEN = '<?php echo htmlspecialchars($token, ENT_QUOTES, 'UTF-8'); ?>';
function showTip(msg, isError) {
const tip = document.getElementById('tipMsg');
tip.textContent = msg;
tip.style.display = 'block';
tip.style.color = isError ? '' : '#38a169';
}
(async function verifyToken() {
if (!RESET_TOKEN) {
document.getElementById('loadingBox').style.display = 'none';
document.getElementById('invalidBox').style.display = 'block';
return;
}
try {
const response = await fetch(`${window.API_BASE_URL}/api/auth/reset-token/check?token=${encodeURIComponent(RESET_TOKEN)}`);
const data = await response.json();
const info = data && data.success ? data.data : null;
document.getElementById('loadingBox').style.display = 'none';
if (info && info.valid) {
document.getElementById('accountName').value =
(info.real_name || '') + '(' + info.username + ')';
document.getElementById('resetForm').style.display = 'block';
} else {
document.getElementById('invalidBox').style.display = 'block';
}
} catch (error) {
console.error('令牌校验失败:', error);
document.getElementById('loadingBox').style.display = 'none';
document.getElementById('invalidBox').style.display = 'block';
}
})();
document.getElementById('resetForm').addEventListener('submit', async (e) => {
e.preventDefault();
const newPassword = document.getElementById('newPassword').value;
const confirmPassword = document.getElementById('confirmPassword').value;
if (newPassword !== confirmPassword) {
showTip('两次输入的密码不一致', true);
return;
}
if (newPassword.length < 6 || newPassword.length > 20) {
showTip('密码长度需为6-20位', true);
return;
}
const btn = document.getElementById('submitBtn');
btn.disabled = true;
btn.textContent = '提交中...';
try {
const response = await fetch(`${window.API_BASE_URL}/api/auth/reset-password`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: RESET_TOKEN, new_password: newPassword })
});
const data = await response.json();
if (data.success) {
showTip(data.message || '密码重置成功', false);
setTimeout(() => { window.location.href = '/index.php'; }, 1800);
} else {
showTip(data.message || '重置失败', true);
btn.disabled = false;
btn.textContent = '确认重置';
}
} catch (error) {
console.error('重置密码失败:', error);
showTip('网络错误,请稍后重试', true);
btn.disabled = false;
btn.textContent = '确认重置';
}
});
</script>
</body>
</html>