fix platform authorization and workflow integrity

This commit is contained in:
david
2026-07-29 15:54:20 +08:00
parent a7d318a013
commit 969d7271f9
26 changed files with 566 additions and 161 deletions

View File

@@ -18,8 +18,40 @@ type staffCredentialRequest struct {
ExpiredAt *time.Time `json:"expired_at"`
}
func ListStaffCredential(ctx *gin.Context) { common.ListPage[models.StaffCredential](ctx) }
func GetStaffCredential(ctx *gin.Context) { common.GetByIdentity[models.StaffCredential](ctx) }
func ListStaffCredential(ctx *gin.Context) {
staffIdentity := ctx.Query("staff_account_identity")
if staffIdentity == "" {
common.ListPage[models.StaffCredential](ctx)
return
}
staffID, err := common.ResolveIdentityID(&models.StaffAccount{}, staffIdentity, true)
if err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
page, size := common.PageSize(ctx)
var list []models.StaffCredential
var total int64
query := common.ApplyKeywordFilter(ctx,
common.ActiveRecords(impl.DBService.Model(&models.StaffCredential{})).
Where("staff_account_id = ?", staffID),
&models.StaffCredential{})
if err := query.Count(&total).Error; err != nil {
infra.Response.Error(ctx, err)
return
}
if err := query.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
infra.Response.Error(ctx, err)
return
}
response, err := common.PublicResourceResponse(list)
if err != nil {
infra.Response.Error(ctx, err)
return
}
infra.Response.Success(ctx, gin.H{"total": total, "list": response})
}
func GetStaffCredential(ctx *gin.Context) { common.GetByIdentity[models.StaffCredential](ctx) }
func CreateStaffCredential(ctx *gin.Context) {
var request staffCredentialRequest
if err := ctx.ShouldBindJSON(&request); err != nil {
@@ -49,5 +81,15 @@ func UpdateStaffCredential(ctx *gin.Context) {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
var current models.StaffCredential
if err := common.ActiveRecords(impl.DBService).Select("staff_account_id").
Where("identity = ?", ctx.Param("identity")).First(&current).Error; err != nil {
common.RespondRecordError(ctx, err)
return
}
if current.StaffAccountID != staffAccountID {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
common.UpdateAllowedByIdentity(ctx, &models.StaffCredential{}, gin.H{"staff_account_id": staffAccountID, "credential_type": request.CredentialType, "credential_no": request.CredentialNo, "expired_at": request.ExpiredAt}, []string{"staff_account_id", "credential_type", "credential_no", "expired_at"})
}

View File

@@ -1,6 +1,8 @@
package staff
import (
"strings"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/infra"
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
@@ -10,7 +12,37 @@ import (
)
// ListStaff 查询服务人员分页列表。
func ListStaff(ctx *gin.Context) { common.ListPage[models.StaffAccount](ctx) }
func ListStaff(ctx *gin.Context) {
roleCode := strings.TrimSpace(ctx.Query("role_code"))
if roleCode == "" {
common.ListPage[models.StaffAccount](ctx)
return
}
if !validStaffRole(roleCode) {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
page, size := common.PageSize(ctx)
var list []models.StaffAccount
var total int64
query := common.ApplyKeywordFilter(ctx,
common.ActiveRecords(impl.DBService.Model(&models.StaffAccount{})).Where("role_code = ?", roleCode),
&models.StaffAccount{})
if err := query.Count(&total).Error; err != nil {
infra.Response.Error(ctx, err)
return
}
if err := query.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
infra.Response.Error(ctx, err)
return
}
response, err := common.PublicResourceResponse(list)
if err != nil {
infra.Response.Error(ctx, err)
return
}
infra.Response.Success(ctx, gin.H{"total": total, "list": common.ProtectPreciseLocation(ctx, &models.StaffAccount{}, response)})
}
// GetStaff 查询一个服务人员档案。
func GetStaff(ctx *gin.Context) { common.GetByIdentity[models.StaffAccount](ctx) }
@@ -28,7 +60,7 @@ func CreateStaff(ctx *gin.Context) {
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
WorkStatus string `json:"work_status" binding:"max=32"`
}
if err := ctx.ShouldBindJSON(&request); err != nil {
if err := ctx.ShouldBindJSON(&request); err != nil || !validStaffRole(request.RoleCode) {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
@@ -77,7 +109,7 @@ func UpdateStaff(ctx *gin.Context) {
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
WorkStatus string `json:"work_status" binding:"max=32"`
}
if err := ctx.ShouldBindJSON(&request); err != nil || !validWorkStatus(request.WorkStatus) {
if err := ctx.ShouldBindJSON(&request); err != nil || !validWorkStatus(request.WorkStatus) || !validStaffRole(request.RoleCode) {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
}
@@ -99,3 +131,6 @@ func UpdateStaff(ctx *gin.Context) {
}
func validWorkStatus(status string) bool { return status == "on_duty" || status == "off_duty" }
func validStaffRole(role string) bool {
return role == "installer" || role == "delivery" || role == "operations"
}

View File

@@ -10,3 +10,16 @@ func TestWorkStatusIsClosedEnumeration(t *testing.T) {
t.Fatal("unknown work status was accepted as available")
}
}
func TestStaffRoleIsClosedEnumeration(t *testing.T) {
for _, role := range []string{"installer", "delivery", "operations"} {
if !validStaffRole(role) {
t.Fatalf("supported staff role %q was rejected", role)
}
}
for _, role := range []string{"", "admin", "root", "delivery_admin"} {
if validStaffRole(role) {
t.Fatalf("unsupported staff role %q was accepted", role)
}
}
}