1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 03:40:54 +00:00

Add interface change in pkg folder

This commit is contained in:
Phillip Stagnet 2020-08-05 18:29:22 +02:00
parent 6f5c5bd3a6
commit 5f2f6e4f86
No known key found for this signature in database
GPG Key ID: 029E4A7879FBE381
14 changed files with 107 additions and 49 deletions

View File

@ -179,16 +179,16 @@ func (cp *Provider) SessionRead(sid string) (session.Store, error) {
// SessionExist Check couchbase session exist. // SessionExist Check couchbase session exist.
// it checkes sid exist or not. // it checkes sid exist or not.
func (cp *Provider) SessionExist(sid string) bool { func (cp *Provider) SessionExist(sid string) (bool, error) {
cp.b = cp.getBucket() cp.b = cp.getBucket()
defer cp.b.Close() defer cp.b.Close()
var doc []byte var doc []byte
if err := cp.b.Get(sid, &doc); err != nil || doc == nil { if err := cp.b.Get(sid, &doc); err != nil || doc == nil {
return false return false, err
} }
return true return true, nil
} }
// SessionRegenerate remove oldsid and use sid to generate new session // SessionRegenerate remove oldsid and use sid to generate new session

View File

@ -132,9 +132,9 @@ func (lp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check ledis session exist by sid // SessionExist check ledis session exist by sid
func (lp *Provider) SessionExist(sid string) bool { func (lp *Provider) SessionExist(sid string) (bool, error) {
count, _ := c.Exists([]byte(sid)) count, _ := c.Exists([]byte(sid))
return count != 0 return count != 0, nil
} }
// SessionRegenerate generate new sid for ledis session // SessionRegenerate generate new sid for ledis session

View File

@ -149,16 +149,16 @@ func (rp *MemProvider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check memcache session exist by sid // SessionExist check memcache session exist by sid
func (rp *MemProvider) SessionExist(sid string) bool { func (rp *MemProvider) SessionExist(sid string) (bool, error) {
if client == nil { if client == nil {
if err := rp.connectInit(); err != nil { if err := rp.connectInit(); err != nil {
return false return false, err
} }
} }
if item, err := client.Get(sid); err != nil || len(item.Value) == 0 { if item, err := client.Get(sid); err != nil || len(item.Value) == 0 {
return false return false, err
} }
return true return true, nil
} }
// SessionRegenerate generate new sid for memcache session // SessionRegenerate generate new sid for memcache session

View File

@ -164,13 +164,19 @@ func (mp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check mysql session exist // SessionExist check mysql session exist
func (mp *Provider) SessionExist(sid string) bool { func (mp *Provider) SessionExist(sid string) (bool, error) {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid) row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
return err != sql.ErrNoRows if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
} }
// SessionRegenerate generate new sid for mysql session // SessionRegenerate generate new sid for mysql session

View File

@ -178,13 +178,19 @@ func (mp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check postgresql session exist // SessionExist check postgresql session exist
func (mp *Provider) SessionExist(sid string) bool { func (mp *Provider) SessionExist(sid string) (bool, error) {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()
row := c.QueryRow("select session_data from session where session_key=$1", sid) row := c.QueryRow("select session_data from session where session_key=$1", sid)
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
return err != sql.ErrNoRows if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
} }
// SessionRegenerate generate new sid for postgresql session // SessionRegenerate generate new sid for postgresql session

View File

@ -211,14 +211,14 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check redis session exist by sid // SessionExist check redis session exist by sid
func (rp *Provider) SessionExist(sid string) bool { func (rp *Provider) SessionExist(sid string) (bool, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 { if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
return false return false, err
} }
return true return true, nil
} }
// SessionRegenerate generate new sid for redis session // SessionRegenerate generate new sid for redis session

View File

@ -176,12 +176,12 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check redis_cluster session exist by sid // SessionExist check redis_cluster session exist by sid
func (rp *Provider) SessionExist(sid string) bool { func (rp *Provider) SessionExist(sid string) (bool, error) {
c := rp.poollist c := rp.poollist
if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 { if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 {
return false return false, err
} }
return true return true, nil
} }
// SessionRegenerate generate new sid for redis_cluster session // SessionRegenerate generate new sid for redis_cluster session

View File

@ -189,12 +189,12 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist check redis_sentinel session exist by sid // SessionExist check redis_sentinel session exist by sid
func (rp *Provider) SessionExist(sid string) bool { func (rp *Provider) SessionExist(sid string) (bool, error) {
c := rp.poollist c := rp.poollist
if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 { if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 {
return false return false, err
} }
return true return true, nil
} }
// SessionRegenerate generate new sid for redis_sentinel session // SessionRegenerate generate new sid for redis_sentinel session

View File

@ -147,8 +147,8 @@ func (pder *CookieProvider) SessionRead(sid string) (Store, error) {
} }
// SessionExist Cookie session is always existed // SessionExist Cookie session is always existed
func (pder *CookieProvider) SessionExist(sid string) bool { func (pder *CookieProvider) SessionExist(sid string) (bool, error) {
return true return true, nil
} }
// SessionRegenerate Implement method, no used. // SessionRegenerate Implement method, no used.

View File

@ -176,17 +176,17 @@ func (fp *FileProvider) SessionRead(sid string) (Store, error) {
// SessionExist Check file session exist. // SessionExist Check file session exist.
// it checks the file named from sid exist or not. // it checks the file named from sid exist or not.
func (fp *FileProvider) SessionExist(sid string) bool { func (fp *FileProvider) SessionExist(sid string) (bool, error) {
filepder.lock.Lock() filepder.lock.Lock()
defer filepder.lock.Unlock() defer filepder.lock.Unlock()
if len(sid) < 2 { if len(sid) < 2 {
SLogger.Println("min length of session id is 2", sid) SLogger.Println("min length of session id is 2 but got length: ", sid)
return false return false, errors.New("min length of session id is 2")
} }
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid)) _, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
return err == nil return err == nil, nil
} }
// SessionDestroy Remove all files in this save path // SessionDestroy Remove all files in this save path

View File

@ -56,16 +56,24 @@ func TestFileProvider_SessionExist(t *testing.T) {
_ = fp.SessionInit(180, sessionPath) _ = fp.SessionInit(180, sessionPath)
if fp.SessionExist(sid) { exists, err := fp.SessionExist(sid)
if err != nil{
t.Error(err)
}
if exists {
t.Error() t.Error()
} }
_, err := fp.SessionRead(sid) _, err = fp.SessionRead(sid)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if !fp.SessionExist(sid) { exists, err = fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if !exists {
t.Error() t.Error()
} }
} }
@ -79,15 +87,27 @@ func TestFileProvider_SessionExist2(t *testing.T) {
_ = fp.SessionInit(180, sessionPath) _ = fp.SessionInit(180, sessionPath)
if fp.SessionExist(sid) { exists, err := fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if exists {
t.Error() t.Error()
} }
if fp.SessionExist("") { exists, err = fp.SessionExist("")
if err == nil {
t.Error()
}
if exists {
t.Error() t.Error()
} }
if fp.SessionExist("1") { exists, err = fp.SessionExist("1")
if err == nil {
t.Error()
}
if exists {
t.Error() t.Error()
} }
} }
@ -171,7 +191,11 @@ func TestFileProvider_SessionRegenerate(t *testing.T) {
t.Error(err) t.Error(err)
} }
if !fp.SessionExist(sid) { exists, err := fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if !exists {
t.Error() t.Error()
} }
@ -180,11 +204,19 @@ func TestFileProvider_SessionRegenerate(t *testing.T) {
t.Error(err) t.Error(err)
} }
if fp.SessionExist(sid) { exists, err = fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if exists {
t.Error() t.Error()
} }
if !fp.SessionExist(sidNew) { exists, err = fp.SessionExist(sidNew)
if err != nil {
t.Error(err)
}
if !exists {
t.Error() t.Error()
} }
} }
@ -203,7 +235,11 @@ func TestFileProvider_SessionDestroy(t *testing.T) {
t.Error(err) t.Error(err)
} }
if !fp.SessionExist(sid) { exists, err := fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if !exists {
t.Error() t.Error()
} }
@ -212,7 +248,11 @@ func TestFileProvider_SessionDestroy(t *testing.T) {
t.Error(err) t.Error(err)
} }
if fp.SessionExist(sid) { exists, err = fp.SessionExist(sid)
if err != nil {
t.Error(err)
}
if exists {
t.Error() t.Error()
} }
} }

View File

@ -109,13 +109,13 @@ func (pder *MemProvider) SessionRead(sid string) (Store, error) {
} }
// SessionExist check session store exist in memory session by sid // SessionExist check session store exist in memory session by sid
func (pder *MemProvider) SessionExist(sid string) bool { func (pder *MemProvider) SessionExist(sid string) (bool, error) {
pder.lock.RLock() pder.lock.RLock()
defer pder.lock.RUnlock() defer pder.lock.RUnlock()
if _, ok := pder.sessions[sid]; ok { if _, ok := pder.sessions[sid]; ok {
return true return true, nil
} }
return false return false, nil
} }
// SessionRegenerate generate new sid for session store in memory session // SessionRegenerate generate new sid for session store in memory session

View File

@ -56,7 +56,7 @@ type Store interface {
type Provider interface { type Provider interface {
SessionInit(gclifetime int64, config string) error SessionInit(gclifetime int64, config string) error
SessionRead(sid string) (Store, error) SessionRead(sid string) (Store, error)
SessionExist(sid string) bool SessionExist(sid string) (bool, error)
SessionRegenerate(oldsid, sid string) (Store, error) SessionRegenerate(oldsid, sid string) (Store, error)
SessionDestroy(sid string) error SessionDestroy(sid string) error
SessionAll() int //get all active session SessionAll() int //get all active session
@ -211,9 +211,15 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
return nil, errs return nil, errs
} }
if sid != "" && manager.provider.SessionExist(sid) { if sid != "" {
exists, err := manager.provider.SessionExist(sid)
if err != nil {
return nil, err
}
if exists {
return manager.provider.SessionRead(sid) return manager.provider.SessionRead(sid)
} }
}
// Generate a new session // Generate a new session
sid, errs = manager.sessionID() sid, errs = manager.sessionID()

View File

@ -68,10 +68,10 @@ func (p *Provider) SessionRead(sid string) (session.Store, error) {
} }
// SessionExist judged whether sid is exist in session // SessionExist judged whether sid is exist in session
func (p *Provider) SessionExist(sid string) bool { func (p *Provider) SessionExist(sid string) (bool, error) {
if p.client == nil { if p.client == nil {
if err := p.connectInit(); err != nil { if err := p.connectInit(); err != nil {
panic(err) return false, err
} }
} }
value, err := p.client.Get(sid) value, err := p.client.Get(sid)
@ -79,9 +79,9 @@ func (p *Provider) SessionExist(sid string) bool {
panic(err) panic(err)
} }
if value == nil || len(value.(string)) == 0 { if value == nil || len(value.(string)) == 0 {
return false return false, nil
} }
return true return true, nil
} }
// SessionRegenerate regenerate session with new sid and delete oldsid // SessionRegenerate regenerate session with new sid and delete oldsid