2014-10-22 16:20:45 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/leanote/leanote/app/db"
|
2015-11-13 17:58:41 +08:00
|
|
|
"github.com/leanote/leanote/app/info"
|
2017-11-30 18:46:30 +08:00
|
|
|
// . "github.com/leanote/leanote/app/lea"
|
2014-10-22 16:20:45 +08:00
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
"time"
|
2015-11-13 17:58:41 +08:00
|
|
|
// "strings"
|
2014-10-22 16:20:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Session存储到mongodb中
|
|
|
|
type SessionService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *SessionService) Update(sessionId, key string, value interface{}) bool {
|
2015-11-13 17:58:41 +08:00
|
|
|
return db.UpdateByQMap(db.Sessions, bson.M{"SessionId": sessionId},
|
2014-10-22 16:20:45 +08:00
|
|
|
bson.M{key: value, "UpdatedTime": time.Now()})
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
// 注销时清空session
|
|
|
|
func (this *SessionService) Clear(sessionId string) bool {
|
|
|
|
return db.Delete(db.Sessions, bson.M{"SessionId": sessionId})
|
|
|
|
}
|
|
|
|
func (this *SessionService) Get(sessionId string) info.Session {
|
|
|
|
session := info.Session{}
|
|
|
|
db.GetByQ(db.Sessions, bson.M{"SessionId": sessionId}, &session)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
// 如果没有session, 那么插入一条之
|
|
|
|
if session.Id == "" {
|
|
|
|
session.Id = bson.NewObjectId()
|
|
|
|
session.SessionId = sessionId
|
|
|
|
session.CreatedTime = time.Now()
|
|
|
|
session.UpdatedTime = session.CreatedTime
|
|
|
|
db.Insert(db.Sessions, session)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------
|
|
|
|
// 错误次数处理
|
|
|
|
|
|
|
|
// 登录错误时间是否已超过了
|
|
|
|
func (this *SessionService) LoginTimesIsOver(sessionId string) bool {
|
|
|
|
session := this.Get(sessionId)
|
|
|
|
return session.LoginTimes > 5
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
// 登录成功后清空错误次数
|
|
|
|
func (this *SessionService) ClearLoginTimes(sessionId string) bool {
|
|
|
|
return this.Update(sessionId, "LoginTimes", 0)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
// 增加错误次数
|
|
|
|
func (this *SessionService) IncrLoginTimes(sessionId string) bool {
|
|
|
|
session := this.Get(sessionId)
|
2015-11-13 17:58:41 +08:00
|
|
|
return this.Update(sessionId, "LoginTimes", session.LoginTimes+1)
|
2014-10-22 16:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------
|
|
|
|
// 验证码
|
|
|
|
func (this *SessionService) GetCaptcha(sessionId string) string {
|
|
|
|
session := this.Get(sessionId)
|
|
|
|
return session.Captcha
|
|
|
|
}
|
|
|
|
func (this *SessionService) SetCaptcha(sessionId, captcha string) bool {
|
|
|
|
this.Get(sessionId)
|
2017-11-30 18:46:30 +08:00
|
|
|
// Log(sessionId)
|
|
|
|
// Log(captcha)
|
2014-10-22 16:20:45 +08:00
|
|
|
ok := this.Update(sessionId, "Captcha", captcha)
|
2017-11-30 18:46:30 +08:00
|
|
|
// Log(ok)
|
2014-10-22 16:20:45 +08:00
|
|
|
return ok
|
|
|
|
}
|
2015-03-31 14:27:26 +08:00
|
|
|
|
|
|
|
//-----------
|
|
|
|
// API
|
|
|
|
func (this *SessionService) GetUserId(sessionId string) string {
|
|
|
|
session := this.Get(sessionId)
|
|
|
|
// 更新updateTime, 避免过期
|
2015-11-13 17:58:41 +08:00
|
|
|
db.UpdateByQMap(db.Sessions, bson.M{"SessionId": sessionId},
|
2015-03-31 14:27:26 +08:00
|
|
|
bson.M{"UpdatedTime": time.Now()})
|
|
|
|
return session.UserId
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录成功后设置userId
|
|
|
|
func (this *SessionService) SetUserId(sessionId, userId string) bool {
|
|
|
|
this.Get(sessionId)
|
|
|
|
ok := this.Update(sessionId, "UserId", userId)
|
|
|
|
return ok
|
|
|
|
}
|