mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
Refactor session.go
This commit is contained in:
@ -15,6 +15,8 @@
|
|||||||
package object
|
package object
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/beego/beego"
|
"github.com/beego/beego"
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
"github.com/xorm-io/core"
|
"github.com/xorm-io/core"
|
||||||
@ -72,20 +74,19 @@ func GetSessionCount(owner, field, value string) int {
|
|||||||
|
|
||||||
func GetSingleSession(id string) *Session {
|
func GetSingleSession(id string) *Session {
|
||||||
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
||||||
session := &Session{Owner: owner, Name: name, Application: application}
|
session := Session{Owner: owner, Name: name, Application: application}
|
||||||
_, err := adapter.Engine.ID(core.PK{owner, name, application}).Get(session)
|
_, err := adapter.Engine.Get(session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return session
|
return &session
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateSession(id string, session *Session) bool {
|
func UpdateSession(id string, session *Session) bool {
|
||||||
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
||||||
|
|
||||||
_, err := adapter.Engine.ID(core.PK{owner, name, application}).Get(session)
|
if GetSingleSession(id) == nil {
|
||||||
if err != nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,19 +99,16 @@ func UpdateSession(id string, session *Session) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddSession(session *Session) bool {
|
func AddSession(session *Session) bool {
|
||||||
owner, name, application := session.Owner, session.Name, session.Application
|
dbSession := GetSingleSession(session.GetId())
|
||||||
|
if dbSession == nil {
|
||||||
|
session.CreatedTime = util.GetCurrentTime()
|
||||||
|
|
||||||
dbSession := &Session{Owner: owner, Name: name, Application: application}
|
affected, err := adapter.Engine.Insert(session)
|
||||||
get, err := adapter.Engine.ID(core.PK{owner, name, application}).Get(dbSession)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var affected int64
|
return affected != 0
|
||||||
var dbErr error
|
|
||||||
if !get {
|
|
||||||
session.CreatedTime = util.GetCurrentTime()
|
|
||||||
affected, dbErr = adapter.Engine.Insert(session)
|
|
||||||
} else {
|
} else {
|
||||||
m := make(map[string]struct{})
|
m := make(map[string]struct{})
|
||||||
for _, v := range dbSession.SessionId {
|
for _, v := range dbSession.SessionId {
|
||||||
@ -121,51 +119,44 @@ func AddSession(session *Session) bool {
|
|||||||
dbSession.SessionId = append(dbSession.SessionId, v)
|
dbSession.SessionId = append(dbSession.SessionId, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
affected, dbErr = adapter.Engine.ID(core.PK{owner, name, application}).Update(dbSession)
|
|
||||||
|
return UpdateSession(dbSession.GetId(), dbSession)
|
||||||
}
|
}
|
||||||
if dbErr != nil {
|
|
||||||
panic(dbErr)
|
|
||||||
}
|
|
||||||
return affected != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSession(id string) bool {
|
func DeleteSession(id string) bool {
|
||||||
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
||||||
|
|
||||||
session := &Session{Owner: owner, Name: name, Application: application}
|
|
||||||
_, err := adapter.Engine.ID(core.PK{owner, name, application}).Get(session)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if owner == CasdoorOrganization && application == CasdoorApplication {
|
if owner == CasdoorOrganization && application == CasdoorApplication {
|
||||||
|
session := GetSingleSession(id)
|
||||||
|
if session != nil {
|
||||||
DeleteBeegoSession(session.SessionId)
|
DeleteBeegoSession(session.SessionId)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
affected, err := adapter.Engine.ID(core.PK{owner, name, application}).Delete(&Session{})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
affected, err := adapter.Engine.ID(core.PK{owner, name, application}).Delete(session)
|
|
||||||
return affected != 0
|
return affected != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSessionId(id string, sessionId string) bool {
|
func DeleteSessionId(id string, sessionId string) bool {
|
||||||
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
session := GetSingleSession(id)
|
||||||
|
if session == nil {
|
||||||
session := &Session{Owner: owner, Name: name, Application: application}
|
|
||||||
_, err := adapter.Engine.ID(core.PK{owner, name, application}).Get(session)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
owner, _, application := util.GetOwnerAndNameAndOtherFromId(id)
|
||||||
if owner == CasdoorOrganization && application == CasdoorApplication {
|
if owner == CasdoorOrganization && application == CasdoorApplication {
|
||||||
DeleteBeegoSession([]string{sessionId})
|
DeleteBeegoSession([]string{sessionId})
|
||||||
}
|
}
|
||||||
session.SessionId = util.DeleteVal(session.SessionId, sessionId)
|
|
||||||
|
|
||||||
if len(session.SessionId) < 1 {
|
session.SessionId = util.DeleteVal(session.SessionId, sessionId)
|
||||||
affected, _ := adapter.Engine.ID(core.PK{owner, name, application}).Delete(session)
|
if len(session.SessionId) == 0 {
|
||||||
return affected != 0
|
return DeleteSession(id)
|
||||||
} else {
|
} else {
|
||||||
affected, _ := adapter.Engine.ID(core.PK{owner, name, application}).Update(session)
|
return UpdateSession(id, session)
|
||||||
return affected != 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,11 +169,13 @@ func DeleteBeegoSession(sessionIds []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *Session) GetId() string {
|
||||||
|
return fmt.Sprintf("%s/%s/%s", session.Owner, session.Name, session.Application)
|
||||||
|
}
|
||||||
|
|
||||||
func IsSessionDuplicated(id string, sessionId string) bool {
|
func IsSessionDuplicated(id string, sessionId string) bool {
|
||||||
owner, name, application := util.GetOwnerAndNameAndOtherFromId(id)
|
session := GetSingleSession(id)
|
||||||
session := &Session{Owner: owner, Name: name, Application: application}
|
if session == nil {
|
||||||
get, _ := adapter.Engine.ID(core.PK{owner, name, application}).Get(session)
|
|
||||||
if !get {
|
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
if len(session.SessionId) > 1 {
|
if len(session.SessionId) > 1 {
|
||||||
|
Reference in New Issue
Block a user