fix(admin): standardize resource initialization and lists
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
// Package initdb 提供应用启动后的基础数据初始化。
|
||||
package initdb
|
||||
|
||||
import "gorm.io/gorm"
|
||||
// New 是初始化入口,按依赖顺序在同一事务中编排所有必需的幂等初始化任务。
|
||||
func New() error {
|
||||
if err := InitPlatformAccess(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// New 在同一事务中初始化平台基础数据。
|
||||
func New(database *gorm.DB) error {
|
||||
return database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := InitPlatformAccess(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return InitPlatformRoot(tx)
|
||||
})
|
||||
if err := InitPlatformRoot(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -11,16 +12,13 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// PlatformRootUsername 是平台总后台的内置根账号名称。
|
||||
PlatformRootUsername = "root"
|
||||
// PlatformRootPassword 是仅用于首次启动的初始密码,首次登录后必须修改。
|
||||
PlatformRootPassword = "Heqi@Root2026"
|
||||
// PlatformRootRoleCode 表示根账号的平台角色。
|
||||
PlatformRootRoleCode = "root"
|
||||
)
|
||||
|
||||
// InitPlatformAccess 幂等初始化 root 角色;菜单定义位于逻辑层静态数据中。
|
||||
func InitPlatformAccess(database *gorm.DB) error {
|
||||
// InitPlatformAccess 幂等初始化平台根角色;菜单定义位于逻辑层静态数据中。
|
||||
func InitPlatformAccess() error {
|
||||
rootRole := models.PlatformRole{
|
||||
Entity: models.Entity{Identity: models.NewIdentity(), Status: common.StatusEnable},
|
||||
RoleCode: PlatformRootRoleCode,
|
||||
@@ -28,13 +26,13 @@ func InitPlatformAccess(database *gorm.DB) error {
|
||||
LocationScope: "precise",
|
||||
IsSystem: true,
|
||||
}
|
||||
return database.Where("role_code = ?", rootRole.RoleCode).FirstOrCreate(&rootRole).Error
|
||||
return impl.DBService.Where("role_code = ?", rootRole.RoleCode).FirstOrCreate(&rootRole).Error
|
||||
}
|
||||
|
||||
// InitPlatformRoot 幂等创建平台总后台 root 账号。
|
||||
func InitPlatformRoot(database *gorm.DB) error {
|
||||
// InitPlatformRoot 幂等创建平台总后台 root 账户。
|
||||
func InitPlatformRoot() error {
|
||||
var account models.PlatformAccount
|
||||
err := database.Where("username = ?", PlatformRootUsername).First(&account).Error
|
||||
err := impl.DBService.Where("username = ?", PlatformRootUsername).First(&account).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -55,7 +53,7 @@ func InitPlatformRoot(database *gorm.DB) error {
|
||||
PlatformRoleCode: PlatformRootRoleCode,
|
||||
Phone: "",
|
||||
}
|
||||
return database.Create(&account).Error
|
||||
return impl.DBService.Create(&account).Error
|
||||
}
|
||||
|
||||
// platformRootPassword 优先读取部署环境传入的 root 初始密码。
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
package initdb
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestInitPlatformAccessSeedsRootRole(t *testing.T) {
|
||||
sqlDatabase, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sqlDatabase.Close() })
|
||||
database, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDatabase}), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "platform_role" WHERE role_code = $1 AND "platform_role"."deleted_at" IS NULL ORDER BY "platform_role"."id" LIMIT $2`)).
|
||||
WithArgs("root", 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "status", "role_code", "name", "location_scope", "is_system"}).
|
||||
AddRow(uint64(1), "root-role", 1, "root", "Root", "precise", true))
|
||||
|
||||
if err := InitPlatformAccess(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformRootPassword(t *testing.T) {
|
||||
t.Run("accepts six character environment password", func(t *testing.T) {
|
||||
t.Setenv("HEQI_PLATFORM_ROOT_PASSWORD", "123456")
|
||||
if got := platformRootPassword(); got != "123456" {
|
||||
t.Fatalf("platformRootPassword() = %q, want environment password", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("falls back when environment password is too short", func(t *testing.T) {
|
||||
t.Setenv("HEQI_PLATFORM_ROOT_PASSWORD", "12345")
|
||||
if got := platformRootPassword(); got != PlatformRootPassword {
|
||||
t.Fatalf("platformRootPassword() = %q, want default password", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user