@ -9,6 +9,8 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder"
)
)
// Session represents a session compatible for go-chi session
// Session represents a session compatible for go-chi session
@ -33,34 +35,28 @@ func UpdateSession(ctx context.Context, key string, data []byte) error {
// ReadSession reads the data for the provided session
// ReadSession reads the data for the provided session
func ReadSession ( ctx context . Context , key string ) ( * Session , error ) {
func ReadSession ( ctx context . Context , key string ) ( * Session , error ) {
session := Session {
Key : key ,
}
ctx , committer , err := db . TxContext ( ctx )
ctx , committer , err := db . TxContext ( ctx )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
defer committer . Close ( )
defer committer . Close ( )
if has , err := db . GetByBean ( ctx , & session ) ; err != nil {
session , exist , err := db . Get [ Session ] ( ctx , builder . Eq { "key" : key } )
if err != nil {
return nil , err
return nil , err
} else if ! has {
} else if ! exist {
session . Expiry = timeutil . TimeStampNow ( )
session . Expiry = timeutil . TimeStampNow ( )
if err := db . Insert ( ctx , & session ) ; err != nil {
if err := db . Insert ( ctx , & session ) ; err != nil {
return nil , err
return nil , err
}
}
}
}
return & session , committer . Commit ( )
return session , committer . Commit ( )
}
}
// ExistSession checks if a session exists
// ExistSession checks if a session exists
func ExistSession ( ctx context . Context , key string ) ( bool , error ) {
func ExistSession ( ctx context . Context , key string ) ( bool , error ) {
session := Session {
return db . Exist [ Session ] ( ctx , builder . Eq { "key" : key } )
Key : key ,
}
return db . GetEngine ( ctx ) . Get ( & session )
}
}
// DestroySession destroys a session
// DestroySession destroys a session
@ -79,17 +75,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
}
}
defer committer . Close ( )
defer committer . Close ( )
if has , err := db . GetByBean ( ctx , & Session {
if has , err := db . Exist [ Session ] ( ctx , builder . Eq { "key" : newKey } ) ; err != nil {
Key : newKey ,
} ) ; err != nil {
return nil , err
return nil , err
} else if has {
} else if has {
return nil , fmt . Errorf ( "session Key: %s already exists" , newKey )
return nil , fmt . Errorf ( "session Key: %s already exists" , newKey )
}
}
if has , err := db . GetByBean ( ctx , & Session {
if has , err := db . Exist [ Session ] ( ctx , builder . Eq { "key" : oldKey } ) ; err != nil {
Key : oldKey ,
} ) ; err != nil {
return nil , err
return nil , err
} else if ! has {
} else if ! has {
if err := db . Insert ( ctx , & Session {
if err := db . Insert ( ctx , & Session {
@ -104,14 +96,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
return nil , err
return nil , err
}
}
s := Session {
s , _ , err := db . Get [ Session ] ( ctx , builder . Eq { "key" : newKey } )
Key : newKey ,
if err != nil {
}
// is not exist, it should be impossible
if _ , err := db . GetByBean ( ctx , & s ) ; err != nil {
return nil , err
return nil , err
}
}
return & s , committer . Commit ( )
return s , committer . Commit ( )
}
}
// CountSessions returns the number of sessions
// CountSessions returns the number of sessions