fix mock seed root account reuse
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
package seed
|
package seed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
@@ -437,34 +438,38 @@ func MockData(database *gorm.DB) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ensureMockRoot(tx, string(passwordHash))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensureMockRoot 复用已有 root 账号,仅在账号不存在时写入默认 root 用户。
|
||||||
|
func ensureMockRoot(database *gorm.DB, passwordHash string) error {
|
||||||
role := models.PlatformRole{
|
role := models.PlatformRole{
|
||||||
Entity: entity(45, common.StatusEnable), RoleCode: "mock_operator",
|
Entity: entity(45, common.StatusEnable), RoleCode: "root",
|
||||||
Name: "模拟运营人员", LocationScope: "standard", IsSystem: false,
|
Name: "系统管理员", LocationScope: "precise", IsSystem: true,
|
||||||
}
|
}
|
||||||
if err := put(tx, &role); err != nil {
|
if err := database.Where("role_code = ?", role.RoleCode).FirstOrCreate(&role).Error; err != nil {
|
||||||
return err
|
return fmt.Errorf("seed root platform role: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
platformAccount := models.PlatformAccount{
|
var account models.PlatformAccount
|
||||||
Entity: entity(46, common.StatusEnable), Username: "mock_operator",
|
err := database.Where("username = ?", "root").First(&account).Error
|
||||||
DisplayName: "模拟运营人员", PasswordHash: string(passwordHash),
|
if err == nil {
|
||||||
PlatformRoleCode: role.RoleCode, Phone: "13600000001",
|
return nil
|
||||||
}
|
}
|
||||||
if err := put(tx, &platformAccount); err != nil {
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return err
|
return fmt.Errorf("find root platform account: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
roleMenu := models.PlatformRoleMenu{
|
account = models.PlatformAccount{
|
||||||
PlatformRoleID: role.ID, MenuIdentity: "dashboard_overview",
|
Entity: entity(46, common.StatusEnable), Username: "root",
|
||||||
|
DisplayName: "平台根管理员", PasswordHash: passwordHash,
|
||||||
|
PlatformRoleCode: role.RoleCode,
|
||||||
}
|
}
|
||||||
if err := tx.Where(
|
if err := database.Create(&account).Error; err != nil {
|
||||||
"platform_role_id = ? AND menu_identity = ?",
|
return fmt.Errorf("seed root platform account: %w", err)
|
||||||
role.ID, roleMenu.MenuIdentity,
|
|
||||||
).FirstOrCreate(&roleMenu).Error; err != nil {
|
|
||||||
return fmt.Errorf("seed platform_role_menu: %w", err)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func entity(sequence int, status int) models.Entity {
|
func entity(sequence int, status int) models.Entity {
|
||||||
|
|||||||
Reference in New Issue
Block a user