refactor(instance): remove redundant instance ownership checks

The ownership validation is now handled at an earlier stage in the request processing, making these checks redundant. This change simplifies the code by removing duplicate validation logic.
This commit is contained in:
2026-05-08 12:30:31 +08:00
parent 00319d790d
commit a78e715293
+18 -18
View File
@@ -537,7 +537,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
instance, err := database.DBQueryFrpcInstanceByID(instanceID) // instance, err := database.DBQueryFrpcInstanceByID(instanceID)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found") utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start a not existed instance: %d", userID, instanceID)) postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start a not existed instance: %d", userID, instanceID))
@@ -549,11 +549,11 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if instance.UserID != userID { // if instance.UserID != userID {
utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found") // utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found")
postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start instance %d that does not belong to them", userID, instanceID)) // postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start instance %d that does not belong to them", userID, instanceID))
return // return
} // }
initType := sys.GetInitSystem() initType := sys.GetInitSystem()
sysName, err := database.GetServiceNameByInstanceID(instanceID) sysName, err := database.GetServiceNameByInstanceID(instanceID)
@@ -665,7 +665,7 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
instance, err := database.DBQueryFrpcInstanceByID(instanceID) // instance, err := database.DBQueryFrpcInstanceByID(instanceID)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found") utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop a not existed instance: %d", userID, instanceID)) postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop a not existed instance: %d", userID, instanceID))
@@ -677,11 +677,11 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if instance.UserID != userID { // if instance.UserID != userID {
utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found") // utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found")
postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop instance %d that does not belong to them", userID, instanceID)) // postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop instance %d that does not belong to them", userID, instanceID))
return // return
} // }
initType := sys.GetInitSystem() initType := sys.GetInitSystem()
serviceName, err := database.GetServiceNameByInstanceID(instanceID) serviceName, err := database.GetServiceNameByInstanceID(instanceID)
@@ -781,7 +781,7 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
instance, err := database.DBQueryFrpcInstanceByID(instanceID) // instance, err := database.DBQueryFrpcInstanceByID(instanceID)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found") utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart a not existed instance: %d", userID, instanceID)) postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart a not existed instance: %d", userID, instanceID))
@@ -793,11 +793,11 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if instance.UserID != userID { // if instance.UserID != userID {
utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found") // utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found")
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart instance %d that does not belong to them", userID, instanceID)) // postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart instance %d that does not belong to them", userID, instanceID))
return // return
} // }
initType := sys.GetInitSystem() initType := sys.GetInitSystem()
sysName, err := database.GetServiceNameByInstanceID(instanceID) sysName, err := database.GetServiceNameByInstanceID(instanceID)