123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package xweb
- import (
- "net/http"
- "net/url"
- "regexp"
- )
- type Filter interface {
- Do(http.ResponseWriter, *http.Request) bool
- }
- type LoginFilter struct {
- App *App
- SessionName string
- AnonymousUrls []*regexp.Regexp
- AskLoginUrls []*regexp.Regexp
- Redirect string
- OriUrlName string
- }
- func NewLoginFilter(app *App, name string, redirect string) *LoginFilter {
- filter := &LoginFilter{App: app, SessionName: name,
- AnonymousUrls: make([]*regexp.Regexp, 0),
- AskLoginUrls: make([]*regexp.Regexp, 0),
- Redirect: redirect,
- }
- filter.AddAnonymousUrls("/favicon.ico", redirect)
- return filter
- }
- func (s *LoginFilter) AddAnonymousUrls(urls ...string) {
- for _, r := range urls {
- cr, err := regexp.Compile(r)
- if err == nil {
- s.AnonymousUrls = append(s.AnonymousUrls, cr)
- }
- }
- }
- func (s *LoginFilter) AddAskLoginUrls(urls ...string) {
- for _, r := range urls {
- cr, err := regexp.Compile(r)
- if err == nil {
- s.AskLoginUrls = append(s.AskLoginUrls, cr)
- }
- }
- }
- func (s *LoginFilter) Do(w http.ResponseWriter, req *http.Request) bool {
- requestPath := removeStick(req.URL.Path)
- session := s.App.SessionManager.Session(req, w)
- id := session.Get(s.SessionName)
- has := (id != nil && id != "")
- var redirect = s.Redirect
- if s.OriUrlName != "" {
- redirect = redirect + "?" + s.OriUrlName + "=" + url.QueryEscape(req.URL.String())
- }
- for _, cr := range s.AskLoginUrls {
- if !cr.MatchString(requestPath) {
- continue
- }
- match := cr.FindStringSubmatch(requestPath)
- if len(match[0]) != len(requestPath) {
- continue
- }
- if !has {
- s.App.Redirect(w, requestPath, redirect)
- }
- return has
- }
- if len(s.AnonymousUrls) == 0 {
- return true
- }
- for _, cr := range s.AnonymousUrls {
- if !cr.MatchString(requestPath) {
- continue
- }
- match := cr.FindStringSubmatch(requestPath)
- if len(match[0]) != len(requestPath) {
- continue
- }
- return true
- }
- if !has {
- s.App.Redirect(w, requestPath, redirect)
- }
- return has
- }
|