feat: apply user settings to effect
This commit is contained in:
@@ -58,11 +58,6 @@ func LoadConfig(path string) (*AppConfig, error) {
|
||||
constant.TablePrefix = Config.Database.TablePrefix
|
||||
}
|
||||
|
||||
// 设置 JWT 密钥
|
||||
if Config.Security.JWTSecret != "" {
|
||||
constant.JWTSecret = Config.Security.JWTSecret
|
||||
}
|
||||
|
||||
return Config, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,62 +1,78 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"baihu/internal/logger"
|
||||
)
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
|
||||
const (
|
||||
InitSection = "system"
|
||||
InitKey = "initialized"
|
||||
InitValue = "true"
|
||||
"baihu/internal/constant"
|
||||
"baihu/internal/logger"
|
||||
)
|
||||
|
||||
type InitService struct {
|
||||
settingsService *SettingsService
|
||||
userService *UserService
|
||||
}
|
||||
|
||||
func NewInitService(settingsService *SettingsService, userService *UserService) *InitService {
|
||||
func NewInitService(settingsService *SettingsService) *InitService {
|
||||
return &InitService{
|
||||
settingsService: settingsService,
|
||||
userService: userService,
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize 执行初始化,如果已初始化则跳过
|
||||
func (s *InitService) Initialize() {
|
||||
//if s.IsInitialized() {
|
||||
// logger.Info("系统已初始化,跳过")
|
||||
// return
|
||||
//}
|
||||
|
||||
// Initialize 执行系统初始化,返回 UserService
|
||||
func (s *InitService) Initialize() *UserService {
|
||||
logger.Info("开始初始化系统...")
|
||||
|
||||
// 创建管理员账号
|
||||
s.createAdminUser()
|
||||
|
||||
// 初始化默认设置(每次启动都检查)
|
||||
// 初始化默认设置
|
||||
if err := s.settingsService.InitSettings(); err != nil {
|
||||
logger.Warnf("初始化设置失败: %v", err)
|
||||
}
|
||||
|
||||
//// 标记为已初始化
|
||||
//s.settingsService.Set(InitSection, InitKey, InitValue)
|
||||
//logger.Info("系统初始化完成")
|
||||
// 初始化 JWT Secret(也用作密码 salt,必须在创建 UserService 之前)
|
||||
s.initJWTSecret()
|
||||
|
||||
// 创建 UserService(依赖 settingsService 获取 salt)
|
||||
userService := NewUserService(s.settingsService)
|
||||
// 创建管理员账号
|
||||
s.initializeAdmin(userService)
|
||||
|
||||
return userService
|
||||
}
|
||||
|
||||
// IsInitialized 检查是否已初始化
|
||||
func (s *InitService) IsInitialized() bool {
|
||||
return s.settingsService.Get(InitSection, InitKey) == InitValue
|
||||
}
|
||||
|
||||
// createAdminUser 创建管理员账号
|
||||
func (s *InitService) createAdminUser() {
|
||||
existingUser := s.userService.GetUserByUsername("admin")
|
||||
// initializeAdmin 创建管理员账号
|
||||
func (s *InitService) initializeAdmin(userService *UserService) {
|
||||
existingUser := userService.GetUserByUsername("admin")
|
||||
if existingUser != nil {
|
||||
logger.Info("管理员账号已存在,跳过创建")
|
||||
return
|
||||
}
|
||||
|
||||
s.userService.CreateUser("admin", "123456", "admin@local", "admin")
|
||||
userService.CreateUser("admin", "123456", "admin@local", "admin")
|
||||
logger.Info("管理员账号创建成功: admin / 123456")
|
||||
}
|
||||
|
||||
// IsInitialized 检查是否已初始化
|
||||
func (s *InitService) IsInitialized() bool {
|
||||
return s.settingsService.Get(constant.SectionSystem, constant.KeyInitialized) == "true"
|
||||
}
|
||||
|
||||
// initJWTSecret 初始化 JWT Secret,如果不存在则生成随机值
|
||||
func (s *InitService) initJWTSecret() {
|
||||
existing := s.settingsService.Get(constant.SectionSystem, constant.KeyJWTSecret)
|
||||
if existing != "" {
|
||||
return
|
||||
}
|
||||
|
||||
// 生成 32 字节随机密钥
|
||||
bytes := make([]byte, 32)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
logger.Warnf("生成 JWT Secret 失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
secret := hex.EncodeToString(bytes)
|
||||
if err := s.settingsService.Set(constant.SectionSystem, constant.KeyJWTSecret, secret); err != nil {
|
||||
logger.Warnf("保存 JWT Secret 失败: %v", err)
|
||||
return
|
||||
}
|
||||
logger.Info("JWT Secret 已生成")
|
||||
}
|
||||
|
||||
@@ -4,21 +4,22 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"baihu/internal/constant"
|
||||
"baihu/internal/database"
|
||||
"baihu/internal/models"
|
||||
)
|
||||
|
||||
type UserService struct{}
|
||||
type UserService struct {
|
||||
settingsService *SettingsService
|
||||
}
|
||||
|
||||
func NewUserService() *UserService {
|
||||
return &UserService{}
|
||||
func NewUserService(settingsService *SettingsService) *UserService {
|
||||
return &UserService{settingsService: settingsService}
|
||||
}
|
||||
|
||||
func (us *UserService) hashPassword(password string) string {
|
||||
salt := ""
|
||||
if Config != nil {
|
||||
salt = Config.Security.PasswordSalt
|
||||
}
|
||||
// 使用 JWT Secret 作为密码 salt
|
||||
salt := us.settingsService.Get(constant.SectionSystem, constant.KeyJWTSecret)
|
||||
hash := sha256.Sum256([]byte(password + salt))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user