some changes

This commit is contained in:
uubulb 2024-12-21 23:56:56 +08:00
parent ddd782d623
commit 00a0494124
6 changed files with 39 additions and 30 deletions

View File

@ -168,13 +168,11 @@ func validateRule(c *gin.Context, r *model.AlertRule) error {
if len(r.Rules) > 0 { if len(r.Rules) > 0 {
for _, rule := range r.Rules { for _, rule := range r.Rules {
singleton.ServerLock.RLock() singleton.ServerLock.RLock()
for s, enabled := range rule.Ignore { for s := range rule.Ignore {
if enabled { if server, ok := singleton.ServerList[s]; ok {
if server, ok := singleton.ServerList[s]; ok { if !server.HasPermission(c) {
if !server.HasPermission(c) { singleton.ServerLock.RUnlock()
singleton.ServerLock.RUnlock() return singleton.Localizer.ErrorT("permission denied")
return singleton.Localizer.ErrorT("permission denied")
}
} }
} }
} }

View File

@ -364,12 +364,10 @@ func validateServers(c *gin.Context, ss *model.Service) error {
singleton.ServerLock.RLock() singleton.ServerLock.RLock()
defer singleton.ServerLock.RUnlock() defer singleton.ServerLock.RUnlock()
for s, enabled := range ss.SkipServers { for s := range ss.SkipServers {
if enabled { if server, ok := singleton.ServerList[s]; ok {
if server, ok := singleton.ServerList[s]; ok { if !server.HasPermission(c) {
if !server.HasPermission(c) { return singleton.Localizer.ErrorT("permission denied")
return singleton.Localizer.ErrorT("permission denied")
}
} }
} }
} }

View File

@ -102,9 +102,11 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
if task.Cover == model.ServiceCoverIgnoreAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] { if task.Cover == model.ServiceCoverIgnoreAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
server := singleton.SortedServerList[workedServerIndex] server := singleton.SortedServerList[workedServerIndex]
singleton.UserLock.RLock() singleton.UserLock.RLock()
role, ok := singleton.UserRoleMap[server.UserID] var role uint8
if !ok { if u, ok := singleton.UserInfoMap[server.UserID]; !ok {
role = model.RoleMember role = model.RoleMember
} else {
role = u.Role
} }
singleton.UserLock.RUnlock() singleton.UserLock.RUnlock()
if task.UserID == server.UserID || role == model.RoleAdmin { if task.UserID == server.UserID || role == model.RoleAdmin {
@ -116,9 +118,11 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
if task.Cover == model.ServiceCoverAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] { if task.Cover == model.ServiceCoverAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
server := singleton.SortedServerList[workedServerIndex] server := singleton.SortedServerList[workedServerIndex]
singleton.UserLock.RLock() singleton.UserLock.RLock()
role, ok := singleton.UserRoleMap[server.UserID] var role uint8
if !ok { if u, ok := singleton.UserInfoMap[server.UserID]; !ok {
role = model.RoleMember role = model.RoleMember
} else {
role = u.Role
} }
singleton.UserLock.RUnlock() singleton.UserLock.RUnlock()
if task.UserID == server.UserID || role == model.RoleAdmin { if task.UserID == server.UserID || role == model.RoleAdmin {

View File

@ -18,6 +18,12 @@ type User struct {
AgentSecret string `json:"agent_secret,omitempty" gorm:"type:char(32)"` AgentSecret string `json:"agent_secret,omitempty" gorm:"type:char(32)"`
} }
type UserInfo struct {
Role uint8
_ [3]byte
AgentSecret string
}
func (u *User) BeforeSave(tx *gorm.DB) error { func (u *User) BeforeSave(tx *gorm.DB) error {
if u.AgentSecret != "" { if u.AgentSecret != "" {
return nil return nil

View File

@ -144,9 +144,11 @@ func checkStatus() {
for _, server := range ServerList { for _, server := range ServerList {
// 监测点 // 监测点
UserLock.RLock() UserLock.RLock()
role, ok := UserRoleMap[alert.UserID] var role uint8
if !ok { if u, ok := UserInfoMap[server.UserID]; !ok {
role = model.RoleMember role = model.RoleMember
} else {
role = u.Role
} }
UserLock.RUnlock() UserLock.RUnlock()
alertsStore[alert.ID][server.ID] = append(alertsStore[alert. alertsStore[alert.ID][server.ID] = append(alertsStore[alert.

View File

@ -8,26 +8,25 @@ import (
) )
var ( var (
UserIdToAgentSecret map[uint64]string UserInfoMap map[uint64]model.UserInfo
AgentSecretToUserId map[string]uint64 AgentSecretToUserId map[string]uint64
UserRoleMap map[uint64]uint8
UserLock sync.RWMutex UserLock sync.RWMutex
) )
func initUser() { func initUser() {
UserIdToAgentSecret = make(map[uint64]string) UserInfoMap = make(map[uint64]model.UserInfo)
AgentSecretToUserId = make(map[string]uint64) AgentSecretToUserId = make(map[string]uint64)
UserRoleMap = make(map[uint64]uint8)
var users []model.User var users []model.User
DB.Find(&users) DB.Find(&users)
for _, u := range users { for _, u := range users {
UserIdToAgentSecret[u.ID] = u.AgentSecret UserInfoMap[u.ID] = model.UserInfo{
Role: u.Role,
AgentSecret: u.AgentSecret,
}
AgentSecretToUserId[u.AgentSecret] = u.ID AgentSecretToUserId[u.AgentSecret] = u.ID
UserRoleMap[u.ID] = u.Role
} }
} }
@ -39,9 +38,11 @@ func OnUserUpdate(u *model.User) {
return return
} }
UserIdToAgentSecret[u.ID] = u.AgentSecret UserInfoMap[u.ID] = model.UserInfo{
Role: u.Role,
AgentSecret: u.AgentSecret,
}
AgentSecretToUserId[u.AgentSecret] = u.ID AgentSecretToUserId[u.AgentSecret] = u.ID
UserRoleMap[u.ID] = u.Role
} }
func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) error { func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) error {
@ -117,9 +118,9 @@ func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) err
OnServerDelete(servers) OnServerDelete(servers)
} }
secret := UserIdToAgentSecret[uid] secret := UserInfoMap[uid].AgentSecret
delete(AgentSecretToUserId, secret) delete(AgentSecretToUserId, secret)
delete(UserIdToAgentSecret, uid) delete(UserInfoMap, uid)
} }
if cron { if cron {