mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
- 系统设置 → 注册设置新增「邮箱黑名单(精确匹配)」,弹窗编辑、批量粘贴,上限 20000 条, 名单存于 settings 键 registration_email_blacklist,由专用接口整体读写;运行时读 60s TTL 进程内缓存,保存后本实例立即生效 - 按收件箱身份匹配(复用 NormalizeEmailForAliasDedup):Gmail 点号、googlemail.com 与 +后缀 变体视为同一邮箱 - 邮箱注册、发验证码、第三方登录补建账号、绑定/换绑邮箱命中时返回 EMAIL_BLOCKED; 已验证邮箱的 OAuth 登录只在新建账号时拦截,已有账号是否可用由账号状态决定 - 保存后对新增项查找仍启用的普通用户并弹窗确认批量禁用(跳过管理员、已禁用与已删除用户), 与单个禁用一样在同一事务内吊销 MCP 授权,禁用后清理认证缓存;也可随时对整份名单手动检查
112 lines
3.8 KiB
Go
112 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
|
)
|
|
|
|
// MaxBatchDisableUsers 是单次批量禁用的用户数上限。
|
|
const MaxBatchDisableUsers = MaxRegistrationEmailBlacklistSize
|
|
|
|
// UserEmailBlacklistRepository 是精确邮箱黑名单关联已注册用户所需的仓储能力。
|
|
type UserEmailBlacklistRepository interface {
|
|
// ListActiveUsersByEmailAliasProbeKeys 返回未删除、状态为 active、非管理员且邮箱折叠后
|
|
// 命中任一 EmailAliasProbeKey 的用户。结果会过度匹配,调用方需按收件箱身份复核。
|
|
ListActiveUsersByEmailAliasProbeKeys(ctx context.Context, keys []string) ([]User, error)
|
|
// BatchDisableUsers 把未删除、非管理员且尚未禁用的用户置为 disabled,并在同一事务内吊销这些用户的
|
|
// 全部 MCP 授权,返回实际变更的用户 ID。
|
|
BatchDisableUsers(ctx context.Context, userIDs []int64) ([]int64, error)
|
|
}
|
|
|
|
// EmailAliasProbeKey 把探针拼成仓储层比对用的 "本地部分@域名" 键(两侧均已去点)。
|
|
func EmailAliasProbeKey(probe EmailAliasProbe) string {
|
|
return probe.Local + "@" + probe.Domain
|
|
}
|
|
|
|
// FindActiveUsersByBlacklistedEmails 返回收件箱身份命中给定邮箱的未禁用普通用户;
|
|
// 格式不合法的邮箱忽略。
|
|
func (s *adminServiceImpl) FindActiveUsersByBlacklistedEmails(ctx context.Context, emails []string) ([]User, error) {
|
|
repo, ok := s.userRepo.(UserEmailBlacklistRepository)
|
|
if !ok {
|
|
return nil, ErrServiceUnavailable
|
|
}
|
|
|
|
normalized := make([]string, 0, len(emails))
|
|
for _, raw := range emails {
|
|
if email, valid := NormalizeRegistrationEmailBlacklistEntry(raw); valid {
|
|
normalized = append(normalized, email)
|
|
}
|
|
}
|
|
identities := RegistrationEmailBlacklistIdentities(normalized)
|
|
if len(identities) == 0 {
|
|
return []User{}, nil
|
|
}
|
|
|
|
keySet := make(map[string]struct{}, 2*len(normalized))
|
|
keys := make([]string, 0, 2*len(normalized))
|
|
for _, email := range normalized {
|
|
for _, probe := range EmailAliasDedupProbes(email) {
|
|
key := EmailAliasProbeKey(probe)
|
|
if _, dup := keySet[key]; dup {
|
|
continue
|
|
}
|
|
keySet[key] = struct{}{}
|
|
keys = append(keys, key)
|
|
}
|
|
}
|
|
|
|
candidates, err := repo.ListActiveUsersByEmailAliasProbeKeys(ctx, keys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
matched := make([]User, 0, len(candidates))
|
|
for _, candidate := range candidates {
|
|
if _, hit := identities[NormalizeEmailForAliasDedup(candidate.Email)]; hit {
|
|
matched = append(matched, candidate)
|
|
}
|
|
}
|
|
return matched, nil
|
|
}
|
|
|
|
// BatchDisableUsers 批量禁用普通用户(管理员与已禁用用户跳过),返回实际禁用的人数。
|
|
func (s *adminServiceImpl) BatchDisableUsers(ctx context.Context, userIDs []int64) (int, error) {
|
|
cleaned := make([]int64, 0, len(userIDs))
|
|
seen := make(map[int64]struct{}, len(userIDs))
|
|
for _, userID := range userIDs {
|
|
if userID <= 0 {
|
|
continue
|
|
}
|
|
if _, dup := seen[userID]; dup {
|
|
continue
|
|
}
|
|
seen[userID] = struct{}{}
|
|
cleaned = append(cleaned, userID)
|
|
}
|
|
if len(cleaned) == 0 {
|
|
return 0, nil
|
|
}
|
|
if len(cleaned) > MaxBatchDisableUsers {
|
|
return 0, fmt.Errorf("user_ids cannot exceed %d", MaxBatchDisableUsers)
|
|
}
|
|
|
|
repo, ok := s.userRepo.(UserEmailBlacklistRepository)
|
|
if !ok {
|
|
return 0, ErrServiceUnavailable
|
|
}
|
|
disabled, err := repo.BatchDisableUsers(ctx, cleaned)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if s.authCacheInvalidator != nil {
|
|
// 状态已落库,缓存失效不随请求取消中断,否则未清到的用户在缓存 TTL 内仍能用 API Key。
|
|
invalidateCtx := context.WithoutCancel(ctx)
|
|
for _, userID := range disabled {
|
|
s.authCacheInvalidator.InvalidateAuthCacheByUserID(invalidateCtx, userID)
|
|
}
|
|
}
|
|
logger.LegacyPrintf("service.admin", "audit: batch disable users requested=%d disabled=%d", len(cleaned), len(disabled))
|
|
return len(disabled), nil
|
|
}
|