mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:43:03 +08:00 
			
		
		
		
	用户状态发生变化时,同步服务状态
This commit is contained in:
		@@ -918,6 +918,7 @@ func (this *ServerDAO) FindAllEnabledServersWithNode(tx *dbs.Tx, nodeId int64) (
 | 
			
		||||
	for _, clusterId := range clusterIds {
 | 
			
		||||
		ones, err := this.Query(tx).
 | 
			
		||||
			Attr("clusterId", clusterId).
 | 
			
		||||
			Where("(userId=0 OR userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE isOn AND state=1))").
 | 
			
		||||
			State(ServerStateEnabled).
 | 
			
		||||
			AscPk().
 | 
			
		||||
			FindAll()
 | 
			
		||||
@@ -2526,6 +2527,28 @@ func (this *ServerDAO) FindServerUAM(tx *dbs.Tx, serverId int64) ([]byte, error)
 | 
			
		||||
		FindJSONCol()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserServerClusterIds 获取用户相关服务的集群ID组合
 | 
			
		||||
func (this *ServerDAO) FindUserServerClusterIds(tx *dbs.Tx, userId int64) ([]int64, error) {
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
	ones, err := this.Query(tx).
 | 
			
		||||
		State(ServerStateEnabled).
 | 
			
		||||
		Attr("userId", userId).
 | 
			
		||||
		Gt("clusterId", 0).
 | 
			
		||||
		Result("DISTINCT(clusterId) AS clusterId").
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var clusterIds = []int64{}
 | 
			
		||||
	for _, one := range ones {
 | 
			
		||||
		clusterIds = append(clusterIds, int64(one.(*Server).ClusterId))
 | 
			
		||||
	}
 | 
			
		||||
	return clusterIds, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUpdate 同步服务所在的集群
 | 
			
		||||
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
 | 
			
		||||
	// 创建任务
 | 
			
		||||
@@ -2608,3 +2631,23 @@ func (this *ServerDAO) NotifyDisable(tx *dbs.Tx, serverId int64) error {
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUserClustersChange 通知用户相关集群更新
 | 
			
		||||
func (this *ServerDAO) NotifyUserClustersChange(tx *dbs.Tx, userId int64) error {
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clusterIds, err := this.FindUserServerClusterIds(tx, userId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	for _, clusterId := range clusterIds {
 | 
			
		||||
		err = SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -42,19 +42,29 @@ func init() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EnableUser 启用条目
 | 
			
		||||
func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
func (this *UserDAO) EnableUser(tx *dbs.Tx, userId int64) error {
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return errors.New("invalid 'userId'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
		Set("state", UserStateEnabled).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DisableUser 禁用条目
 | 
			
		||||
func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
func (this *UserDAO) DisableUser(tx *dbs.Tx, userId int64) error {
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return errors.New("invalid 'userId'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
		Set("state", UserStateDisabled).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledUser 查找启用的用户
 | 
			
		||||
@@ -178,6 +188,16 @@ func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, passw
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return errors.New("invalid userId")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 是否启用变化
 | 
			
		||||
	oldIsOn, err := this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
		Result("isOn").
 | 
			
		||||
		FindBoolCol()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var op = NewUserOperator()
 | 
			
		||||
	op.Id = userId
 | 
			
		||||
	op.Username = username
 | 
			
		||||
@@ -191,8 +211,16 @@ func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, passw
 | 
			
		||||
	op.Remark = remark
 | 
			
		||||
	op.ClusterId = nodeClusterId
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
	err := this.Save(tx, op)
 | 
			
		||||
	return err
 | 
			
		||||
	err = this.Save(tx, op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if oldIsOn != isOn {
 | 
			
		||||
		return SharedServerDAO.NotifyUserClustersChange(tx, userId)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateUserInfo 修改用户基本信息
 | 
			
		||||
 
 | 
			
		||||
@@ -159,7 +159,7 @@ func (this *UserService) DeleteUser(ctx context.Context, req *pb.DeleteUserReque
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = models.SharedUserDAO.DisableUser(tx, req.UserId)
 | 
			
		||||
	err = models.SharedUserDAO.DisableUser(tx, req.UserId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user