mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 17:50:12 +00:00
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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user