MCPcopy
hub / github.com/writefreely/writefreely / Redirect

Method Redirect

handle.go:752–787  ·  view source on GitHub ↗
(url string, ul UserLevelFunc)

Source from the content-addressed store, hash-verified

750}
751
752func (h *Handler) Redirect(url string, ul UserLevelFunc) http.HandlerFunc {
753 return func(w http.ResponseWriter, r *http.Request) {
754 h.handleHTTPError(w, r, func() error {
755 start := time.Now()
756
757 var status int
758 if ul(h.app.App().cfg) != UserLevelNoneType {
759 session, err := h.sessionStore.Get(r, cookieName)
760 if err != nil && (ul(h.app.App().cfg) == UserLevelNoneRequiredType || ul(h.app.App().cfg) == UserLevelUserType) {
761 // Cookie is required, but we can ignore this error
762 log.Error("Handler: Unable to get session (for user permission %d); ignoring: %v", ul(h.app.App().cfg), err)
763 }
764
765 _, gotUser := session.Values[cookieUserVal].(*User)
766 if ul(h.app.App().cfg) == UserLevelNoneRequiredType && gotUser {
767 to := correctPageFromLoginAttempt(r)
768 log.Info("Handler: Required NO user, but got one. Redirecting to %s", to)
769 err := impart.HTTPError{http.StatusFound, to}
770 status = err.Status
771 return err
772 } else if ul(h.app.App().cfg) == UserLevelUserType && !gotUser {
773 log.Info("Handler: Required a user, but DIDN'T get one. Sending not logged in.")
774 err := ErrNotLoggedIn
775 status = err.Status
776 return err
777 }
778 }
779
780 status = sendRedirect(w, http.StatusFound, url)
781
782 log.Info(h.app.ReqLog(r, status, time.Since(start)))
783
784 return nil
785 }())
786 }
787}
788
789func (h *Handler) handleHTTPError(w http.ResponseWriter, r *http.Request, err error) {
790 if err == nil {

Callers 3

loginOrFailFunction · 0.80
InitRoutesFunction · 0.80
ServeFunction · 0.80

Calls 5

handleHTTPErrorMethod · 0.95
sendRedirectFunction · 0.85
AppMethod · 0.65
ReqLogMethod · 0.65

Tested by

no test coverage detected