fix(controller): enforce ownership on notification group, NAT server, and batch move

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
naiba
2026-05-17 10:24:19 +08:00
co-authored by naiba/CloudCode
parent 58f98db1a0
commit 2a65a7db80
6 changed files with 109 additions and 32 deletions
+51
View File
@@ -0,0 +1,51 @@
package controller
import (
"github.com/gin-gonic/gin"
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/service/singleton"
)
func callerIsAdmin(c *gin.Context) bool {
auth, ok := c.Get(model.CtxKeyAuthorizedUser)
if !ok {
return false
}
user, ok := auth.(*model.User)
if !ok || user == nil {
return false
}
return user.Role.IsAdmin()
}
func userCanViewServer(c *gin.Context, server *model.Server) bool {
if server == nil {
return false
}
if callerIsAdmin(c) {
return true
}
if _, isMember := c.Get(model.CtxKeyAuthorizedUser); isMember {
if server.HasPermission(c) {
return true
}
return !server.HideForGuest
}
return !server.HideForGuest
}
func assertOwnsNotificationGroup(c *gin.Context, groupID uint64) error {
if groupID == 0 {
return nil
}
var ng model.NotificationGroup
if err := singleton.DB.First(&ng, groupID).Error; err != nil {
return singleton.Localizer.ErrorT("notification group id %d does not exist", groupID)
}
if !ng.HasPermission(c) {
return singleton.Localizer.ErrorT("permission denied")
}
return nil
}