fix: 将管理员 user_id 从动态 admin_{username} 改为固定 admin

根因:admin user_id 由 admin_ + req.Username 动态拼接,
当 .env 中 ADMIN_USERNAME 更改时,新登录会生成不同的 user_id,
导致旧会话成为孤儿且消息历史不可见。

修复方案 (Plan A):
- auth_handler.go: Login 时 userID 固定为 admin
- auth.go: IsAdminKey 从 HasPrefix(admin_) 改为 == admin
- chat_handler.go: 主对话管理员检查改为 userID == admin
- memory_handler.go: 3处 admin_ 前缀检查改为 == admin
- briefing_handler.go: 3处 admin_ 前缀检查改为 != admin
- sessionStore.ts: isAdminUser 从 startsWith 改为 ===
- MessageBubble.tsx: UserAvatar 管理员判断改为 ===
- main.go: 添加旧管理员用户清理逻辑 (ListUsers+DeleteUser)
- user_store.go: 新增 ListUsers 和 DeleteUser 函数
- ai-core/main.go: adminUserID 从 admin_admin 改为 admin
- memory-service/store.go: 默认 user_id 改为 admin
- memory-service/memory_service.go: 默认 UserID 改为 admin
- devtools/src/index.js: URL 参数 user_id=admin

验证: Go build 通过 (gateway/ai-core/memory-service),
tsc --noEmit 通过, vite build 通过
This commit is contained in:
2026-05-20 22:13:21 +08:00
parent 76ef31e153
commit 1fc2b41d36
13 changed files with 77 additions and 22 deletions
+1 -1
View File
@@ -122,7 +122,7 @@ func main() {
// 初始化后台思考器(增强版:支持工具调用和记忆管理)
thinkerCfg := background.DefaultThinkerConfig()
adminUserID := "admin_admin"
adminUserID := "admin"
adminSessionID := "admin-session-main"
// 创建记忆服务 HTTP 客户端(用于持久化思考日志到 memory-service
+15
View File
@@ -83,6 +83,21 @@ func main() {
log.Println("✅ 管理员用户已存在")
}
// 清理旧的管理员用户 (is_admin=true 但 username 与当前 ADMIN_USERNAME 不同)
// 当 .env 中 ADMIN_USERNAME 变更时,旧的 admin 用户会成为孤立的会话持有者
if allUsers, err := store.ListUsers(s.DB()); err != nil {
log.Printf("⚠ 查询所有用户失败: %v", err)
} else {
for _, u := range allUsers {
if u.IsAdmin && u.Username != cfg.AdminUsername {
log.Printf("🗑 清理旧管理员用户: %s (id=%d)", u.Username, u.ID)
if err := store.DeleteUser(s.DB(), u.ID); err != nil {
log.Printf("⚠ 删除旧管理员用户失败: %s, err=%v", u.Username, err)
}
}
}
}
// 初始化提醒存储(复用同一数据库连接)
if rs, err := store.NewReminderStore(s.DB()); err != nil {
log.Printf("⚠ 提醒存储初始化失败: %v", err)
@@ -146,7 +146,7 @@ func (h *AuthHandler) Login(c *gin.Context) {
if authenticated {
// 用户存在于 users 表中且密码验证通过
if req.Username == h.cfg.AdminUsername {
userID = "admin_" + req.Username
userID = "admin"
} else {
userID = "user_" + req.Username
}
@@ -167,14 +167,14 @@ func (h *AuthHandler) Login(c *gin.Context) {
log.Println("✅ 管理员已迁移到 users 表")
}
}
userID = "admin_" + req.Username
userID = "admin"
} else if req.Username == h.cfg.AdminUsername {
// 数据库不可用时的回退:使用配置中的管理员密码
if req.Password != h.cfg.AdminPassword {
c.JSON(http.StatusUnauthorized, gin.H{"error": "管理员密码错误"})
return
}
userID = "admin_" + req.Username
userID = "admin"
} else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"})
return
@@ -52,7 +52,7 @@ func (h *BriefingHandler) GetBriefing(c *gin.Context) {
userID := c.Query("user_id")
date := c.Query("date")
if !strings.HasPrefix(authUserID, "admin_") || userID == "" {
if authUserID != "admin" || userID == "" {
userID = authUserID
}
@@ -88,7 +88,7 @@ func (h *BriefingHandler) GetLatestBriefings(c *gin.Context) {
authUserID := middleware.GetUserID(c)
userID := c.Query("user_id")
if !strings.HasPrefix(authUserID, "admin_") || userID == "" {
if authUserID != "admin" || userID == "" {
userID = authUserID
}
@@ -128,7 +128,7 @@ func (h *BriefingHandler) Generate(c *gin.Context) {
}
// 非管理员只能为自己生成
if !strings.HasPrefix(authUserID, "admin_") {
if authUserID != "admin" {
req.UserID = authUserID
}
@@ -69,11 +69,11 @@ func (h *ChatHandler) HandleWebSocket(c *gin.Context) {
}
// 主对话仅限管理员访问
if !strings.HasPrefix(userID, "admin_") {
if userID != "admin" {
c.JSON(http.StatusForbidden, gin.H{
"error": "主对话仅限管理员使用",
"errorType": "admin_only",
"hint": "请使用管理员账号 (admin_ 前缀) 登录以访问主对话功能",
"hint": "请使用管理员账号登录以访问主对话功能",
})
return
}
@@ -8,7 +8,6 @@ import (
"io"
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
@@ -39,7 +38,7 @@ func (h *MemoryHandler) Query(c *gin.Context) {
userID := c.Query("user_id")
// 非管理员只能查询自己的记忆;管理员可通过查询参数指定目标用户
if !strings.HasPrefix(authUserID, "admin_") || userID == "" {
if authUserID != "admin" || userID == "" {
userID = authUserID
}
@@ -90,7 +89,7 @@ func (h *MemoryHandler) List(c *gin.Context) {
userID := c.Query("user_id")
// 非管理员只能查询自己的记忆;管理员可通过查询参数指定目标用户
if !strings.HasPrefix(authUserID, "admin_") || userID == "" {
if authUserID != "admin" || userID == "" {
userID = authUserID
}
@@ -141,7 +140,7 @@ func (h *MemoryHandler) Add(c *gin.Context) {
// 管理员可通过请求体指定目标用户,否则使用认证用户
userID := authUserID
if strings.HasPrefix(authUserID, "admin_") && req.UserID != "" {
if authUserID == "admin" && req.UserID != "" {
userID = req.UserID
}
+2 -2
View File
@@ -41,8 +41,8 @@ func JWTAuth(cfg *config.Config) gin.HandlerFunc {
// 将userID注入上下文
c.Set(UserIDKey, userID)
// 设置管理员标记 (admin 用户 ID "admin_" 为前缀)
c.Set(IsAdminKey, strings.HasPrefix(userID, "admin_"))
// 设置管理员标记 (admin 用户 ID 为固定值 "admin")
c.Set(IsAdminKey, userID == "admin")
c.Next()
}
}
@@ -67,6 +67,47 @@ func GetUserByUsername(db *sql.DB, username string) (*User, error) {
return &u, nil
}
// ListUsers 列出所有用户
func ListUsers(db *sql.DB) ([]User, error) {
rows, err := db.Query(
`SELECT id, username, password_hash, is_admin, created_at, updated_at
FROM users ORDER BY id`,
)
if err != nil {
return nil, fmt.Errorf("查询用户列表失败: %w", err)
}
defer rows.Close()
var users []User
for rows.Next() {
var u User
if err := rows.Scan(&u.ID, &u.Username, &u.PasswordHash, &u.IsAdmin, &u.CreatedAt, &u.UpdatedAt); err != nil {
return nil, fmt.Errorf("扫描用户行失败: %w", err)
}
users = append(users, u)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("遍历用户行出错: %w", err)
}
if users == nil {
users = []User{}
}
return users, nil
}
// DeleteUser 按 ID 删除用户
func DeleteUser(db *sql.DB, userID int) error {
result, err := db.Exec(`DELETE FROM users WHERE id = $1`, userID)
if err != nil {
return fmt.Errorf("删除用户失败: %w", err)
}
affected, _ := result.RowsAffected()
if affected == 0 {
return fmt.Errorf("用户不存在 (id=%d)", userID)
}
return nil
}
// CreateUser 创建新用户
func CreateUser(db *sql.DB, username, passwordHash string, isAdmin bool) (*User, error) {
now := time.Now()
@@ -300,7 +300,7 @@ func (svc *MemoryService) SaveThinkingLog(ctx context.Context, tl *model.Thinkin
return fmt.Errorf("content 不能为空")
}
if tl.UserID == "" {
tl.UserID = "admin_admin"
tl.UserID = "admin"
}
return svc.store.SaveThinkingLog(ctx, tl)
}
@@ -182,7 +182,7 @@ func (s *Store) migrate() error {
`CREATE INDEX IF NOT EXISTS idx_me_category_importance ON memory_entries(category, importance DESC)`,
`CREATE TABLE IF NOT EXISTS thinking_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id VARCHAR(64) NOT NULL DEFAULT 'admin_admin',
user_id VARCHAR(64) NOT NULL DEFAULT 'admin',
content TEXT NOT NULL,
tool_calls TEXT DEFAULT '[]',
tool_call_count INT DEFAULT 0,
@@ -670,7 +670,7 @@ func (s *Store) SaveThinkingLog(ctx context.Context, log *model.ThinkingLog) err
}
if log.UserID == "" {
log.UserID = "admin_admin"
log.UserID = "admin"
}
if log.ToolCalls == "" {
log.ToolCalls = "[]"
+1 -1
View File
@@ -211,7 +211,7 @@ app.get('/api/dashboard', async (_req, res) => {
try {
const token = await getGatewayToken();
if (token) {
const memResp = await fetch(`${GATEWAY_URL}/api/v1/memory?user_id=admin_admin`, {
const memResp = await fetch(`${GATEWAY_URL}/api/v1/memory?user_id=admin`, {
headers: { 'Authorization': `Bearer ${token}` },
signal: AbortSignal.timeout(5000),
});
@@ -323,7 +323,7 @@ function ImageThumbnail({
function UserAvatar() {
const [imgError, setImgError] = useState(false);
const userId = useAuthStore((s) => s.userId);
const isAdmin = userId?.startsWith('admin_') ?? false;
const isAdmin = userId === 'admin';
const avatarSrc = isAdmin
? '/images/User_Avatar/Admin_Avatar.jpg'
: '/images/User_Avatar/Default_Avatar.png';
+2 -2
View File
@@ -21,9 +21,9 @@ function randomID(n: number = 12): string {
return `session_${result}`;
}
/** 判断是否为管理员用户 (user_id "admin_" 开头) */
/** 判断是否为管理员用户 (user_id 为固定值 "admin") */
export function isAdminUser(userId: string | null): boolean {
return userId?.startsWith('admin_') ?? false;
return userId === 'admin';
}
interface SessionStore {