特性:优化注册与改名卡逻辑,在触发敏感词或拦截重名时明确提示具体是触发了哪个词汇

This commit is contained in:
2026-04-02 16:38:17 +08:00
parent 63292ab810
commit c4edda8b4e
3 changed files with 45 additions and 26 deletions
+29 -22
View File
@@ -40,34 +40,41 @@ class UsernameBlacklist extends Model
// ──────────────────────────────────────────
/**
* 判断给定名称是否被禁止使用
* 获取拦截该名称的具体黑名单记录(如果有)
*
* 满足以下任一条件时返回 true
* 1. 存在 type=permanent 的永久禁用记录
* 2. 存在 type=temp reserved_until 尚未过期的临时保留记录
* @param string $username 要检测的用户名
* @return static|null
*/
public static function getBlockingRecord(string $username): ?self
{
// 1. 检查是否存在精确匹配且未过期的“临时改名保留”名称
$tempRecord = static::where('type', 'temp')
->where('username', $username)
->where('reserved_until', '>', now())
->first();
if ($tempRecord) {
return $tempRecord;
}
// 2. 检查永久禁用词,改为【模糊匹配】(只要新注册的名字中包含禁用词,拦截)
// 比如数据库禁用了 "admin",那么 "admin123" 也会触发拦截
$permanentRecord = static::where('type', 'permanent')
->where('username', '!=', '')
->whereRaw('? LIKE CONCAT("%", username, "%")', [$username])
->first();
return $permanentRecord;
}
/**
* 判断给定名称是否被禁止使用。
*
* @param string $username 要检测的用户名
*/
public static function isBlocked(string $username): bool
{
// 1. 检查是否存在精确匹配且未过期的“临时改名保留”名称
$hasTemp = static::where('type', 'temp')
->where('username', $username)
->where('reserved_until', '>', now())
->exists();
if ($hasTemp) {
return true;
}
// 2. 检查永久禁用词,改为【模糊匹配】(只要新注册的名字中包含禁用词,拦截)
// 比如数据库禁用了 "admin",那么 "admin123" 也会触发拦截
$hasPermanent = static::where('type', 'permanent')
->where('username', '!=', '')
->whereRaw('? LIKE CONCAT("%", username, "%")', [$username])
->exists();
return $hasPermanent;
return static::getBlockingRecord($username) !== null;
}
/**