feat: 完善平台总后台认证与管理入口
This commit is contained in:
11
backend/api/internal/initdb/new.go
Normal file
11
backend/api/internal/initdb/new.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Package initdb 提供应用启动后的基础数据初始化。
|
||||
package initdb
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// New 在同一事务中初始化平台基础数据。
|
||||
func New(database *gorm.DB) error {
|
||||
return database.Transaction(func(tx *gorm.DB) error {
|
||||
return InitPlatformRoot(tx)
|
||||
})
|
||||
}
|
||||
57
backend/api/internal/initdb/platform.go
Normal file
57
backend/api/internal/initdb/platform.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package initdb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
// PlatformRootUsername 是平台总后台的内置根账号名称。
|
||||
PlatformRootUsername = "root"
|
||||
// PlatformRootPassword 是仅用于首次启动的初始密码,首次登录后必须修改。
|
||||
PlatformRootPassword = "Heqi@Root2026"
|
||||
// PlatformRootRoleCode 表示根账号的平台角色。
|
||||
PlatformRootRoleCode = "platform_root"
|
||||
)
|
||||
|
||||
// InitPlatformRoot 幂等创建平台总后台 root 账号。
|
||||
func InitPlatformRoot(database *gorm.DB) error {
|
||||
var account models.IdnAccount
|
||||
err := database.Where("username = ?", PlatformRootUsername).First(&account).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(platformRootPassword()), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
account = models.IdnAccount{
|
||||
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled"},
|
||||
Username: PlatformRootUsername,
|
||||
DisplayName: "平台根管理员",
|
||||
PasswordHash: string(passwordHash),
|
||||
RoleCode: PlatformRootRoleCode,
|
||||
MustChangePassword: true,
|
||||
Phone: "",
|
||||
AccountType: "operator",
|
||||
ServiceArea: "全国",
|
||||
}
|
||||
return database.Create(&account).Error
|
||||
}
|
||||
|
||||
// platformRootPassword 优先读取部署环境传入的 root 初始密码。
|
||||
func platformRootPassword() string {
|
||||
if password := os.Getenv("HEQI_PLATFORM_ROOT_PASSWORD"); len(password) >= 12 {
|
||||
return password
|
||||
}
|
||||
return PlatformRootPassword
|
||||
}
|
||||
Reference in New Issue
Block a user