This commit is contained in:
2026-08-11 09:50:42 +08:00
28 changed files with 555 additions and 76 deletions

View File

@@ -12,18 +12,33 @@ import (
"gorm.io/gorm"
)
// gasAdminRoleCode 是气站管理后台当前唯一允许登录的角色编码。
const gasAdminRoleCode = "admin"
// newGasAccount 构造平台创建的气站账户,并固定为气站管理员角色。
func newGasAccount(gasBasicID uint64, request accountRequest, passwordHash string) models.GasAccount {
return models.GasAccount{
Entity: common.NewEntity(common.StatusEnable),
GasBasicID: gasBasicID,
Username: request.Username,
DisplayName: request.DisplayName,
PasswordHash: passwordHash,
RoleCode: gasAdminRoleCode,
}
}
type accountRequest struct {
Username string `json:"username" binding:"required,max=64"`
Password string `json:"password" binding:"required"`
DisplayName string `json:"display_name" binding:"max=64"`
RoleCode string `json:"role_code" binding:"max=64"`
RoleCode string `json:"role_code" binding:"max=64"` // 兼容旧客户端,服务端固定使用 admin。
GasBasicIdentity string `json:"gas_basic_identity"`
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
}
type accountUpdateRequest struct {
DisplayName string `json:"display_name" binding:"max=64"`
RoleCode string `json:"role_code" binding:"max=64"`
RoleCode string `json:"role_code" binding:"max=64"` // 兼容旧客户端,更新时不采纳。
GasBasicIdentity string `json:"gas_basic_identity"`
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
}
@@ -63,7 +78,7 @@ func CreateGasAccount(ctx *gin.Context) {
infra.Response.Error(ctx, err)
return
}
account := models.GasAccount{Entity: common.NewEntity(common.StatusEnable), GasBasicID: gasBasicID, Username: request.Username, DisplayName: request.DisplayName, PasswordHash: hash, RoleCode: request.RoleCode}
account := newGasAccount(gasBasicID, request, hash)
if err := impl.DBService.Create(&account).Error; err != nil {
infra.Response.Error(ctx, err)
return
@@ -82,7 +97,7 @@ func UpdateGasAccount(ctx *gin.Context) {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
common.UpdateAllowedByIdentity(ctx, &models.GasAccount{}, gin.H{"gas_basic_id": gasBasicID, "display_name": request.DisplayName, "role_code": request.RoleCode}, []string{"gas_basic_id", "display_name", "role_code"})
common.UpdateAllowedByIdentity(ctx, &models.GasAccount{}, gin.H{"gas_basic_id": gasBasicID, "display_name": request.DisplayName}, []string{"gas_basic_id", "display_name"})
}
func splitOwnerIdentities(value string) []string {

View File

@@ -0,0 +1,19 @@
package gas
import "testing"
// TestNewGasAccountUsesAdminRole 验证旧客户端提交的角色不会改变气站管理员权限。
func TestNewGasAccountUsesAdminRole(t *testing.T) {
account := newGasAccount(
9,
accountRequest{
Username: "gas-admin",
DisplayName: "气站管理员",
RoleCode: "legacy-role",
},
"password-hash",
)
if account.RoleCode != gasAdminRoleCode {
t.Fatalf("气站管理员角色编码必须为 %q实际为 %q", gasAdminRoleCode, account.RoleCode)
}
}