wego/middlewares/auth.go
2015-04-19 16:43:09 +08:00

86 lines
1.3 KiB
Go

package middlewares
import (
"github.com/go-tango/wego/models"
"github.com/lunny/tango"
"github.com/tango-contrib/session"
)
var (
LoginIdKey = "auth_user_id"
)
type auther interface {
AskAuth() bool
SetUserId(int64)
SetSession(*session.Session)
}
type Auther struct {
id int64
s *session.Session
}
func (Auther) AskAuth() bool {
return true
}
func (a *Auther) SetSession(s *session.Session) {
a.s = s
}
func (a *Auther) SetUserId(id int64) {
a.id = id
}
func (a *Auther) SetLogin(id int64) {
a.SetUserId(id)
a.s.Set(LoginIdKey, id)
}
func (a *Auther) Logout() {
a.s.Del(LoginIdKey)
}
func (a *Auther) LoginUserId() int64 {
return a.id
}
func (a *Auther) IsLogin() bool {
return a.id > 0
}
func (a *Auther) LoginUser() *models.User {
user, err := models.GetUserById(a.id)
if err != nil {
return nil
}
return user
}
type AuthUser struct {
Auther
}
func (AuthUser) AskAuth() bool {
return false
}
func Auth(redirct string, sessions *session.Sessions) tango.HandlerFunc {
return func(ctx *tango.Context) {
if auther, ok := ctx.Action().(auther); ok {
s := sessions.Session(ctx.Req(), ctx.ResponseWriter)
auther.SetSession(s)
if userId := s.Get(LoginIdKey); userId == nil {
if auther.AskAuth() {
ctx.Redirect(redirct)
return
}
} else {
auther.SetUserId(userId.(int64))
}
}
ctx.Next()
}
}