feat: refactor kickout, replaced

This commit is contained in:
weloe
2023-10-22 05:28:00 +08:00
parent bb1845fd3c
commit 272189517b
2 changed files with 20 additions and 8 deletions

View File

@@ -48,8 +48,8 @@ type IEnforcer interface {
GetLoginCounts() (int, error) GetLoginCounts() (int, error)
GetLoginTokenCounts() (int, error) GetLoginTokenCounts() (int, error)
Kickout(id string, device string) error Kickout(id string, device ...string) error
Replaced(id string, device string) error Replaced(id string, device ...string) error
// Banned banned api // Banned banned api
Banned(id string, service string, level int, time int64) error Banned(id string, service string, level int, time int64) error

View File

@@ -1,6 +1,7 @@
package token_go package token_go
import ( import (
"container/list"
"fmt" "fmt"
"github.com/weloe/token-go/constant" "github.com/weloe/token-go/constant"
"github.com/weloe/token-go/model" "github.com/weloe/token-go/model"
@@ -8,11 +9,16 @@ import (
) )
// Replaced replace other user // Replaced replace other user
func (e *Enforcer) Replaced(id string, device string) error { func (e *Enforcer) Replaced(id string, device ...string) error {
var err error var err error
if session := e.GetSession(id); session != nil { if session := e.GetSession(id); session != nil {
// get by login device var tokenSignList *list.List
tokenSignList := session.GetFilterTokenSign(device) if len(device) == 0 {
tokenSignList = session.GetTokenSignListCopy()
} else {
// get by login device
tokenSignList = session.GetFilterTokenSign(device[0])
}
// sign account replaced // sign account replaced
for element := tokenSignList.Front(); element != nil; element = element.Next() { for element := tokenSignList.Front(); element != nil; element = element.Next() {
if tokenSign, ok := element.Value.(*model.TokenSign); ok { if tokenSign, ok := element.Value.(*model.TokenSign); ok {
@@ -42,11 +48,17 @@ func (e *Enforcer) Replaced(id string, device string) error {
} }
// Kickout kickout user // Kickout kickout user
func (e *Enforcer) Kickout(id string, device string) error { func (e *Enforcer) Kickout(id string, device ...string) error {
session := e.GetSession(id) session := e.GetSession(id)
if session != nil { if session != nil {
// get by login device var tokenSignList *list.List
tokenSignList := session.GetFilterTokenSign(device) if len(device) == 0 {
tokenSignList = session.GetTokenSignListCopy()
} else {
// get by login device
tokenSignList = session.GetFilterTokenSign(device[0])
}
// sign account kicked // sign account kicked
for element := tokenSignList.Front(); element != nil; element = element.Next() { for element := tokenSignList.Front(); element != nil; element = element.Next() {
if tokenSign, ok := element.Value.(*model.TokenSign); ok { if tokenSign, ok := element.Value.(*model.TokenSign); ok {