2021-02-18 15:16:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
2021-03-14 13:29:57 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2021-02-22 18:19:28 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
2021-02-18 15:16:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is not init(), because user.AuthUsed is not set at init-stage.
|
|
|
|
func initAdmin() {
|
|
|
|
if user.AuthUsed {
|
|
|
|
http.HandleFunc("/admin", handlerAdmin)
|
|
|
|
http.HandleFunc("/admin/shutdown", handlerAdminShutdown)
|
2021-03-14 13:29:57 +00:00
|
|
|
http.HandleFunc("/admin/reindex-users", handlerAdminReindexUsers)
|
2021-02-18 15:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAdmin(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
if user.CanProceed(rq, "admin") {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-02-22 18:19:28 +00:00
|
|
|
w.Write([]byte(base("Admin panel", views.AdminPanelHTML(), user.FromRequest(rq))))
|
2021-02-18 15:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
if user.CanProceed(rq, "admin/shutdown") && rq.Method == "POST" {
|
|
|
|
log.Fatal("An admin commanded the wiki to shutdown")
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 13:29:57 +00:00
|
|
|
|
|
|
|
func handlerAdminReindexUsers(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
log.Println(rq.URL)
|
|
|
|
if user.CanProceed(rq, "admin") && rq.Method == "POST" {
|
|
|
|
user.ReadUsersFromFilesystem()
|
|
|
|
http.Redirect(w, rq, "/hypha/"+util.UserHypha, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
}
|