Allow forced server deletion

This commit is contained in:
npc0-hue
2026-08-04 15:03:49 +08:00
parent 96a1939f32
commit cf9af14eaf
17 changed files with 236 additions and 24 deletions
+9 -5
View File
@@ -23,6 +23,8 @@ var (
ErrForbidden = errors.New("forbidden")
)
const ServerDeletionForceConfirmation = "FORCE DELETE"
type ForbiddenError struct {
Reason string
}
@@ -102,7 +104,7 @@ type Core interface {
ListServerAdministratorCandidates(string, string) ([]domain.User, error)
AddServerAdministrator(string, string, string) (domain.ServerInstance, error)
RemoveServerAdministrator(string, string, string) (domain.ServerInstance, error)
DeleteServerInstanceForSession(string, string, string) (domain.ServerInstance, error)
DeleteServerInstanceForSession(string, string, domain.ServerDeletionRequest) (domain.ServerInstance, error)
GetPlatformResourceUsage() (domain.PlatformResourceUsage, error)
ListServerMetricsForSession(string) ([]domain.ServerMetrics, error)
GetProductionCapacityForSession(string) (domain.ProductionCapacitySummary, error)
@@ -2367,7 +2369,7 @@ func (svc *CoreService) RemoveServerAdministrator(sessionID string, serverInstan
return domain.CopyServerInstance(instance), nil
}
func (svc *CoreService) DeleteServerInstanceForSession(sessionID string, serverInstanceID string, password string) (domain.ServerInstance, error) {
func (svc *CoreService) DeleteServerInstanceForSession(sessionID string, serverInstanceID string, request domain.ServerDeletionRequest) (domain.ServerInstance, error) {
user, err := svc.GetCurrentUser(sessionID)
if err != nil {
return domain.ServerInstance{}, err
@@ -2379,14 +2381,16 @@ func (svc *CoreService) DeleteServerInstanceForSession(sessionID string, serverI
if !isPlatformAdmin(user) && instance.OwnerUserID != user.ID {
return domain.ServerInstance{}, ErrForbidden
}
if strings.TrimSpace(password) == "" {
if strings.TrimSpace(request.Password) == "" {
return domain.ServerInstance{}, validationError("password is required")
}
if !verifyPassword(user.PasswordHash, password) {
if !verifyPassword(user.PasswordHash, request.Password) {
return domain.ServerInstance{}, forbiddenError("password confirmation failed")
}
if instance.State == domain.ServerInstanceStateRunning || instance.State == domain.ServerInstanceStateInstalling {
return domain.ServerInstance{}, validationError("running or installing server instances must be stopped before delete")
if !request.Force || strings.TrimSpace(request.Confirmation) != ServerDeletionForceConfirmation {
return domain.ServerInstance{}, validationError("running or installing server instances require forced-delete confirmation")
}
}
if instance.State == domain.ServerInstanceStateDeleted {
return domain.CopyServerInstance(instance), nil