mindoc/routers/filter.go
roberChen 90c1329de2 chore!(all): attempt to update beego to v2
BREAKING CHANGE: beego has update to v2, and this version of mindoc IS
NOT TESTED AND STABLE!!!
2021-03-23 21:55:50 +08:00

61 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package routers
import (
"encoding/json"
"net/url"
"regexp"
"github.com/beego/beego/v2"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/server/web/context"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
)
func init() {
var FilterUser = func(ctx *context.Context) {
_, ok := ctx.Input.Session(conf.LoginSessionName).(models.Member)
if !ok {
if ctx.Input.IsAjax() {
jsonData := make(map[string]interface{}, 3)
jsonData["errcode"] = 403
jsonData["message"] = "请登录后再操作"
returnJSON, _ := json.Marshal(jsonData)
ctx.ResponseWriter.Write(returnJSON)
} else {
ctx.Redirect(302, conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+ctx.Request.URL.RequestURI()))
}
}
}
beego.InsertFilter("/manager", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/manager/*", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/setting", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/setting/*", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/book", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/book/*", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/api/*", beego.BeforeRouter, FilterUser)
beego.InsertFilter("/manage/*", beego.BeforeRouter, FilterUser)
var FinishRouter = func(ctx *context.Context) {
ctx.ResponseWriter.Header().Add("MinDoc-Version", conf.VERSION)
ctx.ResponseWriter.Header().Add("MinDoc-Site", "https://www.iminho.me")
ctx.ResponseWriter.Header().Add("X-XSS-Protection", "1; mode=block")
}
var StartRouter = func(ctx *context.Context) {
sessionId := ctx.Input.Cookie(adapter.AppConfig.String("sessionname"))
if sessionId != "" {
//sessionId必须是数字字母组成且最小32个字符最大1024字符
if ok, err := regexp.MatchString(`^[a-zA-Z0-9]{32,512}$`, sessionId); !ok || err != nil {
panic("401")
}
}
}
beego.InsertFilter("/*", beego.BeforeStatic, StartRouter, false)
beego.InsertFilter("/*", beego.BeforeRouter, FinishRouter, false)
}