移除管理员数据库模型,改为通过配置管理管理员身份和认证逻辑,并更新相关逻辑和文档
This commit is contained in:
@@ -2,10 +2,9 @@ package admin
|
||||
|
||||
import (
|
||||
"FileRelay/internal/auth"
|
||||
"FileRelay/internal/bootstrap"
|
||||
"FileRelay/internal/config"
|
||||
"FileRelay/internal/model"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -43,27 +42,24 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var admin model.Admin
|
||||
if err := bootstrap.DB.First(&admin).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin not found"))
|
||||
passwordHash := config.GlobalConfig.Security.AdminPasswordHash
|
||||
if passwordHash == "" {
|
||||
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin password hash not configured"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(admin.PasswordHash), []byte(req.Password)); err != nil {
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(req.Password)); err != nil {
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Incorrect password"))
|
||||
return
|
||||
}
|
||||
|
||||
token, err := auth.GenerateToken(admin.ID)
|
||||
// 使用固定 ID 1 代表管理员(因为不再有数据库记录)
|
||||
token, err := auth.GenerateToken(1)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to generate token"))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新登录时间
|
||||
now := time.Now()
|
||||
bootstrap.DB.Model(&admin).Update("last_login", &now)
|
||||
|
||||
c.JSON(http.StatusOK, model.SuccessResponse(LoginResponse{
|
||||
Token: token,
|
||||
}))
|
||||
|
||||
@@ -34,7 +34,6 @@ func InitDB() {
|
||||
&model.FileBatch{},
|
||||
&model.FileItem{},
|
||||
&model.APIToken{},
|
||||
&model.Admin{},
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to migrate database: %v", err)
|
||||
@@ -74,31 +73,28 @@ func ReloadStorage() error {
|
||||
}
|
||||
|
||||
func initAdmin() {
|
||||
var count int64
|
||||
DB.Model(&model.Admin{}).Count(&count)
|
||||
if count == 0 {
|
||||
passwordHash := config.GlobalConfig.Security.AdminPasswordHash
|
||||
if passwordHash == "" {
|
||||
// 生成随机密码
|
||||
password := generateRandomPassword(12)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate password hash: %v", err)
|
||||
}
|
||||
passwordHash = string(hash)
|
||||
fmt.Printf("**************************************************\n")
|
||||
fmt.Printf("NO ADMIN PASSWORD CONFIGURED. GENERATED RANDOM PASSWORD:\n")
|
||||
fmt.Printf("Password: %s\n", password)
|
||||
fmt.Printf("Please save this password or configure admin_password_hash in config.yaml\n")
|
||||
fmt.Printf("**************************************************\n")
|
||||
passwordHash := config.GlobalConfig.Security.AdminPasswordHash
|
||||
if passwordHash == "" {
|
||||
// 生成随机密码
|
||||
password := generateRandomPassword(12)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate password hash: %v", err)
|
||||
}
|
||||
passwordHash = string(hash)
|
||||
fmt.Printf("**************************************************\n")
|
||||
fmt.Printf("NO ADMIN PASSWORD CONFIGURED. GENERATED RANDOM PASSWORD:\n")
|
||||
fmt.Printf("Password: %s\n", password)
|
||||
fmt.Printf("Please save this password or configure admin_password_hash in config.yaml\n")
|
||||
fmt.Printf("**************************************************\n")
|
||||
|
||||
admin := &model.Admin{
|
||||
PasswordHash: passwordHash,
|
||||
// 将生成的哈希保存回配置文件
|
||||
config.GlobalConfig.Security.AdminPasswordHash = passwordHash
|
||||
if err := config.SaveConfig(); err != nil {
|
||||
fmt.Printf("Warning: Failed to save generated password hash to config: %v\n", err)
|
||||
}
|
||||
DB.Create(admin)
|
||||
fmt.Println("Admin account initialized.")
|
||||
}
|
||||
fmt.Println("Admin authentication initialized via config.")
|
||||
}
|
||||
|
||||
func generateRandomPassword(length int) string {
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
PasswordHash string `json:"-"`
|
||||
LastLogin *time.Time `json:"last_login"`
|
||||
// AdminSession 管理员会话信息 (不再存库,仅用于 JWT 或 API 交互)
|
||||
type AdminSession struct {
|
||||
ID uint `json:"id"`
|
||||
LastLogin *time.Time `json:"last_login"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user