diff --git a/web/admin.go b/web/admin.go index 18dc00d..da16524 100644 --- a/web/admin.go +++ b/web/admin.go @@ -32,9 +32,6 @@ func initAdmin(r *mux.Router) { // handlerAdmin provides the admin panel. func handlerAdmin(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if user.CanProceed(rq, "admin") { w.Header().Set("Content-Type", "text/html;charset=utf-8") w.WriteHeader(http.StatusOK) @@ -48,9 +45,6 @@ func handlerAdmin(w http.ResponseWriter, rq *http.Request) { // handlerAdminShutdown kills the wiki. func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if user.CanProceed(rq, "admin/shutdown") && rq.Method == "POST" { log.Fatal("An admin commanded the wiki to shutdown") } @@ -59,9 +53,6 @@ func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) { // handlerAdminReindexUsers reinitialises the user system. func handlerAdminReindexUsers(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if user.CanProceed(rq, "admin") && rq.Method == "POST" { user.ReadUsersFromFilesystem() redirectTo := rq.Referer() @@ -74,9 +65,6 @@ func handlerAdminReindexUsers(w http.ResponseWriter, rq *http.Request) { func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if user.CanProceed(rq, "admin") { path := strings.TrimPrefix(rq.URL.Path, "/admin/users") parts := strings.Split(path, "/")[1:] @@ -178,9 +166,6 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) { func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if user.CanProceed(rq, "admin") { if rq.Method == http.MethodGet { // New user form diff --git a/web/auth.go b/web/auth.go index 5a7ea16..8419167 100644 --- a/web/auth.go +++ b/web/auth.go @@ -40,9 +40,6 @@ func handlerLock(w http.ResponseWriter, rq *http.Request) { // handlerRegister both displays the register form (GET) and registers users (POST). func handlerRegister(w http.ResponseWriter, rq *http.Request) { - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } util.PrepareRq(rq) if !cfg.AllowRegistration { w.WriteHeader(http.StatusForbidden) @@ -112,9 +109,6 @@ func handlerLogoutConfirm(w http.ResponseWriter, rq *http.Request) { // handlerLogin shows the login form. func handlerLogin(w http.ResponseWriter, rq *http.Request) { - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } util.PrepareRq(rq) w.Header().Set("Content-Type", "text/html;charset=utf-8") if cfg.UseAuth { diff --git a/web/history.go b/web/history.go index 5bb77dc..0be4c1c 100644 --- a/web/history.go +++ b/web/history.go @@ -27,9 +27,6 @@ func initHistory(r *mux.Router) { // handlerHistory lists all revisions of a hypha. func handlerHistory(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } hyphaName := util.HyphaNameFromRq(rq, "history") var list string @@ -47,9 +44,6 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) { // handlerRecentChanges displays the /recent-changes/ page. func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } var ( noPrefix = strings.TrimPrefix(rq.URL.String(), "/recent-changes/") n, err = strconv.Atoi(noPrefix) @@ -64,9 +58,6 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) { // genericHandlerOfFeeds is a helper function for the web feed handlers. func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func() (string, error), name string) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if content, err := f(); err != nil { w.Header().Set("Content-Type", "text/plain;charset=utf-8") w.WriteHeader(http.StatusInternalServerError) diff --git a/web/mutators.go b/web/mutators.go index 3f507ed..0d64596 100644 --- a/web/mutators.go +++ b/web/mutators.go @@ -44,9 +44,6 @@ func factoryHandlerAsker( h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if err, errtitle := asker(u, h); err != nil { httpErr( w, @@ -97,9 +94,6 @@ func factoryHandlerConfirmer( h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if hop, errtitle := confirmer(h, u, rq); hop.HasErrors() { httpErr(w, http.StatusInternalServerError, hyphaName, errtitle, @@ -147,9 +141,6 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) { err error u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if err, errtitle := shroom.CanEdit(u, h); err != nil { httpErr(w, http.StatusInternalServerError, hyphaName, errtitle, @@ -189,9 +180,6 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) { hop *history.HistoryOp errtitle string ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if action != "Preview" { hop, errtitle = shroom.UploadText(h, []byte(textData), message, u) @@ -233,9 +221,6 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) { u = user.FromRequest(rq) file, handler, err = rq.FormFile("binary") ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if err != nil { httpErr(w, http.StatusInternalServerError, hyphaName, "Error", diff --git a/web/readers.go b/web/readers.go index 0e2bf4a..aa5cfd8 100644 --- a/web/readers.go +++ b/web/readers.go @@ -39,9 +39,6 @@ func handlerAttachment(w http.ResponseWriter, rq *http.Request) { h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } util.HTTP200Page(w, views.BaseHTML( fmt.Sprintf("Attachment of %s", util.BeautifulName(hyphaName)), @@ -59,9 +56,6 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } util.HTTP200Page(w, views.BaseHTML( fmt.Sprintf("Diff of %s at %s", hyphaName, revHash), @@ -82,9 +76,6 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) { textContents, err = history.FileAtRevision(h.TextPath, revHash) u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if err == nil { ctx, _ := mycocontext.ContextFromStringInput(hyphaName, textContents) contents = mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx)) @@ -103,9 +94,6 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) { // handlerText serves raw source text of the hypha. func handlerText(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } hyphaName := util.HyphaNameFromRq(rq, "text") if h := hyphae.ByName(hyphaName); h.Exists { log.Println("Serving", h.TextPath) @@ -117,9 +105,6 @@ func handlerText(w http.ResponseWriter, rq *http.Request) { // handlerBinary serves binary part of the hypha. func handlerBinary(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } hyphaName := util.HyphaNameFromRq(rq, "binary") if h := hyphae.ByName(hyphaName); h.Exists { log.Println("Serving", h.BinaryPath) @@ -138,9 +123,6 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) { openGraph string u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } if h.Exists { fileContentsT, errT := os.ReadFile(h.TextPath) _, errB := os.Stat(h.BinaryPath) diff --git a/web/search.go b/web/search.go index 81df445..5274f0c 100644 --- a/web/search.go +++ b/web/search.go @@ -24,9 +24,6 @@ func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) { query = rq.FormValue("q") u = user.FromRequest(rq) ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } w.WriteHeader(http.StatusOK) _, _ = io.WriteString( w, @@ -41,13 +38,7 @@ func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) { func handlerTitleSearchJSON(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) _ = rq.ParseForm() - var ( - query = rq.FormValue("q") - u = user.FromRequest(rq) - ) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } + query := rq.FormValue("q") w.WriteHeader(http.StatusOK) _, _ = io.WriteString( w, diff --git a/web/stuff.go b/web/stuff.go index b393861..63239a9 100644 --- a/web/stuff.go +++ b/web/stuff.go @@ -37,10 +37,6 @@ func initStuff(r *mux.Router) { // handlerHelp gets the appropriate documentation or tells you where you (personally) have failed. func handlerHelp(w http.ResponseWriter, rq *http.Request) { - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } - content, err := help.Get(rq.URL.Path[6:]) // Drop /help/ if err != nil && strings.HasPrefix(err.Error(), "open") { w.WriteHeader(http.StatusNotFound) @@ -80,9 +76,6 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) { // handlerList shows a list of all hyphae in the wiki in random order. func handlerList(w http.ResponseWriter, rq *http.Request) { u := user.FromRequest(rq) - if shown := u.ShowLockMaybe(w, rq); shown { - return - } util.PrepareRq(rq) util.HTTP200Page(w, views.BaseHTML("List of pages", views.HyphaListHTML(), u)) } @@ -90,9 +83,6 @@ func handlerList(w http.ResponseWriter, rq *http.Request) { // handlerReindex reindexes all hyphae by checking the wiki storage directory anew. func handlerReindex(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if ok := user.CanProceed(rq, "reindex"); !ok { httpErr(w, http.StatusForbidden, cfg.HomeHypha, "Not enough rights", "You must be an admin to reindex hyphae.") log.Println("Rejected", rq.URL) @@ -110,9 +100,6 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) { // See https://mycorrhiza.wiki/hypha/configuration/header func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } if ok := user.CanProceed(rq, "update-header-links"); !ok { httpErr(w, http.StatusForbidden, cfg.HomeHypha, "Not enough rights", "You must be a moderator to update header links.") log.Println("Rejected", rq.URL) @@ -125,9 +112,6 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) { // handlerRandom redirects to a random hypha. func handlerRandom(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } var ( randomHyphaName string amountOfHyphae = hyphae.Count() @@ -149,9 +133,6 @@ func handlerRandom(w http.ResponseWriter, rq *http.Request) { // handlerAbout shows a summary of wiki's software. func handlerAbout(w http.ResponseWriter, rq *http.Request) { - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } w.Header().Set("Content-Type", "text/html;charset=utf-8") w.WriteHeader(http.StatusOK) _, err := io.WriteString(w, views.BaseHTML("About "+cfg.WikiName, views.AboutHTML(), user.FromRequest(rq))) diff --git a/web/web.go b/web/web.go index 4ab91f3..5e93eb0 100644 --- a/web/web.go +++ b/web/web.go @@ -56,9 +56,6 @@ func handlerStyle(w http.ResponseWriter, rq *http.Request) { } func handlerUserList(w http.ResponseWriter, rq *http.Request) { - if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { - return - } w.Header().Set("Content-Type", mime.TypeByExtension(".html")) w.WriteHeader(http.StatusOK) w.Write([]byte(views.BaseHTML("User list", views.UserListHTML(), user.FromRequest(rq)))) @@ -76,8 +73,8 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) { } func Handler() http.Handler { - r := mux.NewRouter() - r.Use(func(next http.Handler) http.Handler { + router := mux.NewRouter() + router.Use(func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Do stuff here log.Println(r.RequestURI) @@ -86,32 +83,41 @@ func Handler() http.Handler { }) }) - // Available all the time - initAuth(r) + // Public routes + initAuth(router) + router.HandleFunc("/robots.txt", handlerRobotsTxt) + router.HandleFunc("/static/style.css", handlerStyle) + router.PathPrefix("/static/"). + Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.FS)))) - initReaders(r) - initMutators(r) - - initAdmin(r) - initHistory(r) - initStuff(r) - initSearch(r) - - // Miscellaneous - r.HandleFunc("/user-list", handlerUserList) - r.HandleFunc("/robots.txt", handlerRobotsTxt) - - // Static assets - r.HandleFunc("/static/style.css", handlerStyle) - r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.FS)))) - - // Index page - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // Let's pray it never fails - addr, _ := url.Parse("/hypha/" + cfg.HomeHypha) - r.URL = addr - handlerHypha(w, r) + // Wiki routes. They may be locked or restricted + wikiRouter := router.PathPrefix("").Subrouter() + wikiRouter.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) { + user := user.FromRequest(rq) + if !user.ShowLockMaybe(w, rq) { + next.ServeHTTP(w, rq) + } + }) }) - return r + initReaders(wikiRouter) + initMutators(wikiRouter) + initAdmin(wikiRouter) + initHistory(wikiRouter) + initStuff(wikiRouter) + initSearch(wikiRouter) + + // Miscellaneous + wikiRouter.HandleFunc("/user-list", handlerUserList) + + // Index page + wikiRouter.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) { + // Let's pray it never fails + addr, _ := url.Parse("/hypha/" + cfg.HomeHypha) + rq.URL = addr + handlerHypha(w, rq) + }) + + return router }