mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-10-30 11:46:16 +00:00
Refactor admin routes
They're not perfect, I still don't like them, but I can't think of a good solution right now. I'm going to thinking about the best way of doing web stuff for some time.
This commit is contained in:
parent
3ee21e312d
commit
49b0a35304
79
web/admin.go
79
web/admin.go
@ -2,12 +2,12 @@ package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
@ -19,41 +19,34 @@ import (
|
||||
|
||||
// initAdmin sets up /admin routes if auth is used. Call it after you have decided if you want to use auth.
|
||||
func initAdmin(r *mux.Router) {
|
||||
if cfg.UseAuth {
|
||||
r.HandleFunc("/admin/shutdown", handlerAdminShutdown)
|
||||
r.HandleFunc("/admin/reindex-users", handlerAdminReindexUsers)
|
||||
r.HandleFunc("/shutdown", handlerAdminShutdown).Methods(http.MethodPost)
|
||||
r.HandleFunc("/reindex-users", handlerAdminReindexUsers).Methods(http.MethodPost)
|
||||
|
||||
r.PathPrefix("/admin/users/").HandlerFunc(handlerAdminUsers)
|
||||
r.HandleFunc("/admin/user/new", handlerAdminUserNew)
|
||||
r.HandleFunc("/admin", handlerAdmin)
|
||||
}
|
||||
r.HandleFunc("/user/new", handlerAdminUserNew).Methods(http.MethodGet, http.MethodPost)
|
||||
r.HandleFunc("/users/{username}/edit", handlerAdminUserEdit).Methods(http.MethodGet, http.MethodPost)
|
||||
r.HandleFunc("/users/{username}/delete", handlerAdminUserDelete).Methods(http.MethodGet, http.MethodPost)
|
||||
r.HandleFunc("/users", handlerAdminUsers)
|
||||
|
||||
r.HandleFunc("/", handlerAdmin)
|
||||
}
|
||||
|
||||
// handlerAdmin provides the admin panel.
|
||||
func handlerAdmin(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
if user.CanProceed(rq, "admin") {
|
||||
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err := io.WriteString(w, views.BaseHTML("Admin panel", views.AdminPanelHTML(), user.FromRequest(rq)))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
io.WriteString(w, views.BaseHTML("Admin panel", views.AdminPanelHTML(), user.FromRequest(rq)))
|
||||
}
|
||||
|
||||
// handlerAdminShutdown kills the wiki.
|
||||
func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
if user.CanProceed(rq, "admin/shutdown") && rq.Method == "POST" {
|
||||
log.Fatal("An admin commanded the wiki to shutdown")
|
||||
if user.CanProceed(rq, "admin/shutdown") {
|
||||
log.Println("An admin commanded the wiki to shutdown")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
// handlerAdminReindexUsers reinitialises the user system.
|
||||
func handlerAdminReindexUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
if user.CanProceed(rq, "admin") && rq.Method == "POST" {
|
||||
user.ReadUsersFromFilesystem()
|
||||
redirectTo := rq.Referer()
|
||||
if redirectTo == "" {
|
||||
@ -61,16 +54,8 @@ func handlerAdminReindexUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
}
|
||||
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
|
||||
func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
if user.CanProceed(rq, "admin") {
|
||||
path := strings.TrimPrefix(rq.URL.Path, "/admin/users")
|
||||
parts := strings.Split(path, "/")[1:]
|
||||
|
||||
// Users dashboard
|
||||
if len(parts) == 0 {
|
||||
// Get a sorted list of users
|
||||
var userList []*user.User
|
||||
for u := range user.YieldUsers() {
|
||||
@ -86,25 +71,17 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
html = views.BaseHTML("Manage users", html, user.FromRequest(rq))
|
||||
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
if _, err := io.WriteString(w, html); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return
|
||||
io.WriteString(w, html)
|
||||
}
|
||||
|
||||
if len(parts) != 2 {
|
||||
util.HTTP404Page(w, "404 page not found")
|
||||
return
|
||||
}
|
||||
|
||||
u := user.UserByName(parts[0])
|
||||
func handlerAdminUserEdit(w http.ResponseWriter, rq *http.Request) {
|
||||
vars := mux.Vars(rq)
|
||||
u := user.UserByName(vars["username"])
|
||||
if u == nil {
|
||||
util.HTTP404Page(w, "404 page not found")
|
||||
return
|
||||
}
|
||||
|
||||
switch parts[1] {
|
||||
case "edit":
|
||||
f := util.FormDataFromRequest(rq, []string{"group"})
|
||||
|
||||
if rq.Method == http.MethodPost {
|
||||
@ -136,8 +113,16 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
}
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func handlerAdminUserDelete(w http.ResponseWriter, rq *http.Request) {
|
||||
vars := mux.Vars(rq)
|
||||
u := user.UserByName(vars["username"])
|
||||
if u == nil {
|
||||
util.HTTP404Page(w, "404 page not found")
|
||||
return
|
||||
case "delete":
|
||||
}
|
||||
|
||||
f := util.NewFormData()
|
||||
|
||||
if rq.Method == http.MethodPost {
|
||||
@ -157,16 +142,9 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
|
||||
}
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
io.WriteString(w, html)
|
||||
return
|
||||
}
|
||||
|
||||
util.HTTP404Page(w, "404 page not found")
|
||||
}
|
||||
}
|
||||
|
||||
func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
if user.CanProceed(rq, "admin") {
|
||||
if rq.Method == http.MethodGet {
|
||||
// New user form
|
||||
html := views.AdminUserNewHTML(util.NewFormData())
|
||||
@ -174,7 +152,6 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
io.WriteString(w, html)
|
||||
return
|
||||
} else if rq.Method == http.MethodPost {
|
||||
// Create a user
|
||||
f := util.FormDataFromRequest(rq, []string{"name", "password", "group"})
|
||||
@ -191,9 +168,5 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
|
||||
} else {
|
||||
http.Redirect(w, rq, "/admin/users/", http.StatusSeeOther)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
util.HTTP404Page(w, "404 page not found")
|
||||
}
|
||||
|
41
web/web.go
41
web/web.go
@ -65,8 +65,7 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
file, err := static.FS.Open("robots.txt")
|
||||
if err != nil {
|
||||
return
|
||||
if err != nil { return
|
||||
}
|
||||
io.Copy(w, file)
|
||||
file.Close()
|
||||
@ -75,22 +74,21 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
||||
func 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)
|
||||
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
||||
next.ServeHTTP(w, r)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
|
||||
util.PrepareRq(rq)
|
||||
next.ServeHTTP(w, rq)
|
||||
})
|
||||
})
|
||||
router.StrictSlash(true)
|
||||
|
||||
// Public routes
|
||||
// Public routes. They're always accessible regardless of the user status.
|
||||
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))))
|
||||
|
||||
// Wiki routes. They may be locked or restricted
|
||||
// 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) {
|
||||
@ -103,11 +101,17 @@ func Handler() http.Handler {
|
||||
|
||||
initReaders(wikiRouter)
|
||||
initMutators(wikiRouter)
|
||||
initAdmin(wikiRouter)
|
||||
initHistory(wikiRouter)
|
||||
initStuff(wikiRouter)
|
||||
initSearch(wikiRouter)
|
||||
|
||||
// Admin routes.
|
||||
if cfg.UseAuth {
|
||||
adminRouter := wikiRouter.PathPrefix("/admin").Subrouter()
|
||||
adminRouter.Use(groupMiddleware("admin"))
|
||||
initAdmin(adminRouter)
|
||||
}
|
||||
|
||||
// Miscellaneous
|
||||
wikiRouter.HandleFunc("/user-list", handlerUserList)
|
||||
|
||||
@ -121,3 +125,20 @@ func Handler() http.Handler {
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func groupMiddleware(group string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
|
||||
if cfg.UseAuth && user.CanProceed(rq, group) {
|
||||
next.ServeHTTP(w, rq)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: handle this better. Merge this code with all other
|
||||
// authorization code in this project.
|
||||
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
io.WriteString(w, "403 forbidden")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user