nezha/service/singleton/user.go

130 lines
2.6 KiB
Go
Raw Normal View History

2024-12-18 04:26:24 -05:00
package singleton
import (
"sync"
"github.com/nezhahq/nezha/model"
2024-12-19 13:11:23 -05:00
"gorm.io/gorm"
2024-12-18 04:26:24 -05:00
)
var (
UserIdToAgentSecret map[uint64]string
AgentSecretToUserId map[string]uint64
UserLock sync.RWMutex
)
func initUser() {
UserIdToAgentSecret = make(map[uint64]string)
AgentSecretToUserId = make(map[string]uint64)
var users []model.User
DB.Find(&users)
for _, u := range users {
UserIdToAgentSecret[u.ID] = u.AgentSecret
AgentSecretToUserId[u.AgentSecret] = u.ID
}
}
func OnUserUpdate(u *model.User) {
UserLock.Lock()
defer UserLock.Unlock()
if u == nil {
return
}
UserIdToAgentSecret[u.ID] = u.AgentSecret
AgentSecretToUserId[u.AgentSecret] = u.ID
}
2024-12-21 06:07:19 -05:00
func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) error {
2024-12-18 04:26:24 -05:00
UserLock.Lock()
defer UserLock.Unlock()
if len(id) < 1 {
2024-12-21 06:07:19 -05:00
return Localizer.ErrorT("user id not specified")
2024-12-18 04:26:24 -05:00
}
2024-12-19 13:11:23 -05:00
var (
2024-12-21 06:07:19 -05:00
cron, server bool
crons, servers []uint64
2024-12-19 13:11:23 -05:00
)
2024-12-18 04:26:24 -05:00
for _, uid := range id {
2024-12-21 06:07:19 -05:00
err := DB.Transaction(func(tx *gorm.DB) error {
CronLock.RLock()
2024-12-21 07:25:31 -05:00
crons = model.FindByUserID(CronList, uid)
2024-12-21 06:07:19 -05:00
CronLock.RUnlock()
cron = len(crons) > 0
if cron {
if err := tx.Unscoped().Delete(&model.Cron{}, "id in (?)", crons).Error; err != nil {
return err
}
}
2024-12-19 13:11:23 -05:00
2024-12-21 06:07:19 -05:00
SortedServerLock.RLock()
2024-12-21 07:25:31 -05:00
servers = model.FindByUserID(SortedServerList, uid)
2024-12-21 06:07:19 -05:00
SortedServerLock.RUnlock()
2024-12-19 13:11:23 -05:00
2024-12-21 06:07:19 -05:00
server = len(servers) > 0
if server {
2024-12-19 13:11:23 -05:00
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
}
2024-12-21 06:07:19 -05:00
}
if err := tx.Unscoped().Delete(&model.Transfer{}, "server_id in (?)", servers).Error; err != nil {
return err
}
if err := tx.Where("id IN (?)", id).Delete(&model.User{}).Error; err != nil {
return err
}
return nil
})
if err != nil {
return errorFunc("%v", err)
}
2024-12-19 13:11:23 -05:00
2024-12-21 06:07:19 -05:00
if cron {
OnDeleteCron(crons)
}
if server {
2024-12-19 13:11:23 -05:00
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)
}
}
}
AlertsLock.Unlock()
OnServerDelete(servers)
}
2024-12-21 06:07:19 -05:00
secret := UserIdToAgentSecret[uid]
delete(AgentSecretToUserId, secret)
delete(UserIdToAgentSecret, uid)
2024-12-19 13:11:23 -05:00
}
if cron {
UpdateCronList()
}
if server {
ReSortServer()
2024-12-18 04:26:24 -05:00
}
2024-12-21 06:07:19 -05:00
return nil
2024-12-18 04:26:24 -05:00
}