feat: delete never-activated licenses and bundle deployment verification keys
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-12 14:31:15 +08:00
parent 03a3b9f6a9
commit f4b99e50cd
9 changed files with 221 additions and 12 deletions

View File

@@ -173,6 +173,76 @@ func (a *BusinessAPI) issueLicense(c *gin.Context) {
c.JSON(201, gin.H{"data": rec, "idempotent_replay": false})
}
func (a *BusinessAPI) deleteLicense(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
apiError(c, 400, "invalid_request", "授权 ID 无效")
return
}
adminID := c.MustGet("admin_id")
tx, err := a.db.BeginTx(c, nil)
if err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
defer tx.Rollback()
var status, licenseKey string
if err = tx.QueryRowContext(c, "SELECT status,license_key FROM licenses WHERE id=? FOR UPDATE", id).Scan(&status, &licenseKey); errors.Is(err, sql.ErrNoRows) {
apiError(c, 404, "not_found", "授权不存在")
return
} else if err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
var activated int
if err = tx.QueryRowContext(c, `SELECT COUNT(*) FROM license_instances i WHERE i.license_id=? AND (i.status='activated' OR i.activated_at IS NOT NULL OR i.certificate_payload IS NOT NULL OR i.certificate_signature IS NOT NULL OR EXISTS (SELECT 1 FROM activation_operations o WHERE o.instance_id=i.id))`, id).Scan(&activated); err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
if activated > 0 {
apiError(c, 409, "license_activated", "该授权已激活,不能删除,请使用撤销")
return
}
var instances, orders, operations int
for _, check := range []struct {
query string
count *int
}{
{"SELECT COUNT(*) FROM license_instances WHERE license_id=?", &instances},
{"SELECT COUNT(*) FROM offline_orders WHERE issued_license_id=?", &orders},
{"SELECT COUNT(*) FROM license_operations WHERE license_id=?", &operations},
} {
if err = tx.QueryRowContext(c, check.query, id).Scan(check.count); err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
}
details, _ := json.Marshal(map[string]any{"license_id": id, "license_key": licenseKey, "status": status, "removed_instances": instances, "detached_orders": orders, "removed_operations": operations})
if _, err = tx.ExecContext(c, "INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,'license.deleted','license',?,?)", adminID, id, details); err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
for _, query := range []string{
"UPDATE offline_orders SET issued_license_id=NULL WHERE issued_license_id=?",
"DELETE FROM activation_operations WHERE instance_id IN (SELECT id FROM license_instances WHERE license_id=?)",
"DELETE FROM heartbeat_operations WHERE instance_id IN (SELECT id FROM license_instances WHERE license_id=?)",
"DELETE FROM instance_state_operations WHERE instance_id IN (SELECT id FROM license_instances WHERE license_id=?)",
"DELETE FROM license_instances WHERE license_id=?",
"DELETE FROM license_operations WHERE license_id=?",
"DELETE FROM licenses WHERE id=?",
} {
if _, err = tx.ExecContext(c, query, id); err != nil {
apiError(c, 500, "internal_error", "删除授权失败")
return
}
}
if err = tx.Commit(); err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用")
return
}
c.Status(204)
}
func (a *BusinessAPI) getLicense(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {