This commit is contained in:
uubulb 2024-12-20 02:11:23 +08:00
parent 08f566271b
commit 84c3d4dc3d
2 changed files with 71 additions and 0 deletions

View File

@ -27,6 +27,10 @@ func (c *Common) GetID() uint64 {
return c.ID
}
func (c *Common) GetUserID() uint64 {
return c.UserID
}
func (c *Common) HasPermission(ctx *gin.Context) bool {
auth, ok := ctx.Get(CtxKeyAuthorizedUser)
if !ok {
@ -43,9 +47,21 @@ func (c *Common) HasPermission(ctx *gin.Context) bool {
type CommonInterface interface {
GetID() uint64
GetUserID() uint64
HasPermission(*gin.Context) bool
}
func FindUserID[S ~[]E, E CommonInterface](s S, uid uint64) []uint64 {
var list []uint64
for _, v := range s {
if v.GetUserID() == uid {
list = append(list, v.GetID())
}
}
return list
}
type Response struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`

View File

@ -4,6 +4,7 @@ import (
"sync"
"github.com/nezhahq/nezha/model"
"gorm.io/gorm"
)
var (
@ -46,9 +47,63 @@ func OnUserDelete(id []uint64) {
return
}
var (
cron bool
server bool
)
for _, uid := range id {
secret := UserIdToAgentSecret[uid]
delete(AgentSecretToUserId, secret)
delete(UserIdToAgentSecret, uid)
CronLock.RLock()
crons := model.FindUserID(CronList, uid)
CronLock.RUnlock()
cron = len(crons) > 0
if cron {
DB.Unscoped().Delete(&model.Cron{}, "id in (?)", crons)
OnDeleteCron(crons)
}
SortedServerLock.RLock()
servers := model.FindUserID(SortedServerList, uid)
SortedServerLock.RUnlock()
server = len(servers) > 0
if server {
DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil {
return err
}
if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_id in (?)", servers).Error; err != nil {
return err
}
return nil
})
AlertsLock.Lock()
for _, sid := range servers {
for _, alert := range Alerts {
if AlertsCycleTransferStatsStore[alert.ID] != nil {
delete(AlertsCycleTransferStatsStore[alert.ID].ServerName, sid)
delete(AlertsCycleTransferStatsStore[alert.ID].Transfer, sid)
delete(AlertsCycleTransferStatsStore[alert.ID].NextUpdate, sid)
}
}
}
DB.Unscoped().Delete(&model.Transfer{}, "server_id in (?)", servers)
AlertsLock.Unlock()
OnServerDelete(servers)
}
}
if cron {
UpdateCronList()
}
if server {
ReSortServer()
}
}