实现更新域名功能

develop
clumxc 5 months ago
parent 72a25da663
commit 9d562f3f73

@ -36,6 +36,7 @@ $userRepo = new UserRepository();
$mailboxRepo = new MailboxRepository();
$message = '';
$error = '';
$domain = $settingsRepo->get('domain', 'test.com');
// 处理系统设置更新
if (isset($_POST['update_settings'])) {
@ -61,10 +62,19 @@ if (isset($_POST['update_settings'])) {
// 域名
if (isset($_POST['domain'])) {
$domain = trim($_POST['domain']);
if (!empty($domain)) {
$settingsRepo->set('domain', $domain);
}
$oldDomain = $settingsRepo->get('domain', 'test.com'); // 改前
$newDomain = trim($_POST['domain']);
if (!empty($newDomain) && $newDomain !== $oldDomain) {
$settingsRepo->set('domain', $newDomain);
// 新增:如果勾选了“同步域名”,调用一次性脚本
require_once __DIR__ . '/../src/admin/SyncDomainService.php';
$sync = new SyncDomainService();
$count = $sync->run($oldDomain, $newDomain);
$message .= " 已同步 $count 个用户邮箱后缀。";
}
}
// 默认邮箱大小限制
@ -222,9 +232,10 @@ $users = $userRepo->getAll();
<form method="POST">
<div class="form-group">
<label>服务器域名默认test.com</label>
<input type="text" name="domain" value="<?php echo htmlspecialchars($settings['domain'] ?? 'test.com'); ?>" required>
<input type="text" name="domain" value="<?php echo htmlspecialchars($settings['domain'] ?? 'test.com'); ?>" >
<small>邮件服务器域名,用户邮箱必须使用此域名</small>
</div>
<button type="submit" name="update_settings" class="btn btn-primary">保存域名设置</button>
</form>
</div>

@ -4,6 +4,7 @@ require_once __DIR__ . '/../src/storage/Database.php';
require_once __DIR__ . '/../src/storage/UserRepository.php';
require_once __DIR__ . '/../src/utils/Validator.php';
require_once __DIR__ . '/../src/utils/Security.php';
require_once __DIR__ . '/../src/storage/SystemSettingsRepository.php';
session_start();
@ -13,10 +14,9 @@ if (!isset($_SESSION['user_id'])) {
exit;
}
// 检查管理员权限
if (!$_SESSION['is_admin']) {
die('权限不足:只有管理员可以访问此页面');
}
$settingsRepo = new SystemSettingsRepository();
// 获取域名设置(放在函数定义之前)
$domain = $settingsRepo->get('domain', 'test.com');
$userRepo = new UserRepository();
$message = '';
@ -33,8 +33,8 @@ if (isset($_POST['create_user'])) {
if (!$usernameValidation['valid']) {
$error = implode('<br>', $usernameValidation['errors']);
} else {
if (!Validator::validateEmailDomain($username, 'test.com')) {
$error = "邮箱域名必须是 @test.com";
if (!Validator::validateEmailDomain($username, $domain)) {
$error = "邮箱域名必须是 @".$domain;
} else {
$passwordValidation = Validator::validatePassword($password, 6);
if (!$passwordValidation['valid']) {
@ -179,7 +179,7 @@ $users = $userRepo->getAll();
<form method="POST" class="form-inline">
<div class="form-group">
<label>邮箱地址</label>
<input type="email" name="username" placeholder="user@test.com" required>
<input type="email" name="username" placeholder="user@<?= htmlspecialchars($domain) ?>" required>
</div>
<div class="form-group">
<label>密码</label>

@ -1 +0,0 @@
//处理管理请求

@ -0,0 +1,32 @@
<?php
require_once __DIR__ . '/../storage/Database.php';
/**
* 一次性把 users.username 中 @oldDomain 批量替换成 @newDomain
*/
class SyncDomainService
{
private $db;
public function __construct()
{
$this->db = Database::getInstance();
}
/**
* @return int 实际被更新的用户数
*/
public function run(string $oldDomain, string $newDomain): int
{
// 防止注入,直接用 REPLACE 函数最省事
$sql = "UPDATE users
SET username = REPLACE(username, :old, :new)
WHERE username LIKE :like";
$stmt = $this->db->prepare($sql);
$stmt->execute([
':old' => '@' . $oldDomain,
':new' => '@' . $newDomain,
':like' => '%@' . $oldDomain
]);
return $stmt->rowCount();
}
}

@ -1 +0,0 @@
// 管理后台网页" - 网页界面
Loading…
Cancel
Save