You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.4 KiB
71 lines
1.4 KiB
package rest
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/eyebluecn/tank/code/core"
|
|
"github.com/eyebluecn/tank/code/tool/result"
|
|
"github.com/eyebluecn/tank/code/tool/util"
|
|
)
|
|
|
|
type BaseBean struct {
|
|
logger core.Logger
|
|
}
|
|
|
|
func (this *BaseBean) Init() {
|
|
this.logger = core.LOGGER
|
|
}
|
|
|
|
func (this *BaseBean) Bootstrap() {
|
|
|
|
}
|
|
|
|
// clean up the application.
|
|
func (this *BaseBean) Cleanup() {
|
|
|
|
}
|
|
|
|
// shortcut for panic check.
|
|
func (this *BaseBean) PanicError(err error) {
|
|
core.PanicError(err)
|
|
}
|
|
|
|
// find the current user from request.
|
|
func (this *BaseBean) findUser(request *http.Request) *User {
|
|
|
|
//try to find from SessionCache.
|
|
sessionId := util.GetSessionUuidFromRequest(request, core.COOKIE_AUTH_KEY)
|
|
if sessionId == "" {
|
|
return nil
|
|
}
|
|
|
|
cacheItem, err := core.CONTEXT.GetSessionCache().Value(sessionId)
|
|
if err != nil {
|
|
this.logger.Warn("error while get from session cache. sessionId = %s, error = %v", sessionId, err)
|
|
return nil
|
|
}
|
|
|
|
if cacheItem == nil || cacheItem.Data() == nil {
|
|
|
|
this.logger.Warn("cache item doesn't exist with sessionId = %s", sessionId)
|
|
return nil
|
|
}
|
|
|
|
if value, ok := cacheItem.Data().(*User); ok {
|
|
return value
|
|
} else {
|
|
this.logger.Error("cache item not store the *User")
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// find current error. If not found, panic the LOGIN error.
|
|
func (this *BaseBean) checkUser(request *http.Request) *User {
|
|
_user := this.findUser(request)
|
|
if _user == nil {
|
|
panic(result.LOGIN)
|
|
}
|
|
return _user
|
|
}
|
|
|