github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/user_manager.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package nodes
     4  
     5  import (
     6  	"sync"
     7  )
     8  
     9  var SharedUserManager = NewUserManager()
    10  
    11  type User struct {
    12  	ServersEnabled bool
    13  }
    14  
    15  type UserManager struct {
    16  	userMap map[int64]*User // id => *User
    17  
    18  	locker sync.RWMutex
    19  }
    20  
    21  func NewUserManager() *UserManager {
    22  	return &UserManager{
    23  		userMap: map[int64]*User{},
    24  	}
    25  }
    26  
    27  func (this *UserManager) UpdateUserServersIsEnabled(userId int64, isEnabled bool) {
    28  	this.locker.Lock()
    29  	u, ok := this.userMap[userId]
    30  	if ok {
    31  		u.ServersEnabled = isEnabled
    32  	} else {
    33  		u = &User{ServersEnabled: isEnabled}
    34  		this.userMap[userId] = u
    35  	}
    36  	this.locker.Unlock()
    37  }
    38  
    39  func (this *UserManager) CheckUserServersIsEnabled(userId int64) (isEnabled bool) {
    40  	if userId <= 0 {
    41  		return true
    42  	}
    43  	
    44  	this.locker.RLock()
    45  	u, ok := this.userMap[userId]
    46  	if ok {
    47  		isEnabled = u.ServersEnabled
    48  	} else {
    49  		isEnabled = true
    50  	}
    51  	this.locker.RUnlock()
    52  	return
    53  }