flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit ea2c1241eaa89aa42c07a47059955eed7a9bcbbe
parent d8efa1bc645cd8051a8ae6ee2744017ca11611d4
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  5 Dec 2020 20:43:13 -0800

Cleanup admin commands

Diffstat:
Madmin.go | 20+++++++++++++++-----
Mhttp.go | 16++++++++++++++--
Mtemplates/my_site.html | 1-
3 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/admin.go b/admin.go @@ -20,7 +20,7 @@ import ( func runAdminCommand() { args := flag.Args() // again? if len(args) < 3 { - fmt.Println("Expected subcommand with parameter activate-user|delete-user|make-admin") + fmt.Println("Expected subcommand with parameter activate-user|delete-user|make-admin|rename-user") os.Exit(1) } var err error @@ -39,6 +39,7 @@ func runAdminCommand() { username := args[2] newUsername := args[3] err = renameUser(username, newUsername) + // case "set-password": } if err != nil { log.Fatal(err) @@ -56,9 +57,14 @@ func makeAdmin(username string) error { return nil } +func setPassword(username string, newPass string) error { + return nil +} + func activateUser(username string) error { - _, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username) + _, err := DB.Exec("UPDATE user SET active = true WHERE username = ?", username) if err != nil { + // TODO verify 1 row updated return err } log.Println("Activated user", username) @@ -84,8 +90,6 @@ func renameUser(oldUsername string, newUsername string) error { if err != nil { return err } - fmt.Println("Old user", oldUsername) - fmt.Println("new user", newUsername) res, err := DB.Exec("UPDATE user set username = ? WHERE username = ?", newUsername, oldUsername) if err != nil { return err @@ -104,6 +108,7 @@ func renameUser(oldUsername string, newUsername string) error { // TODO some sort of better handling? return err } + log.Printf("Changed username from %s to %s", oldUsername, newUsername) return nil } @@ -113,6 +118,11 @@ func deleteUser(username string) error { return err } username = filepath.Clean(username) - os.RemoveAll(path.Join(c.FilesDirectory, username)) + err = os.RemoveAll(path.Join(c.FilesDirectory, username)) + if err != nil { + // bad state + return err + } + log.Println("Deleted user", username) return nil } diff --git a/http.go b/http.go @@ -287,7 +287,6 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { } else if r.Method == "POST" { r.ParseForm() newUsername := r.Form.Get("username") - fmt.Println(newUsername) errors := []string{} newEmail := r.Form.Get("email") newUsername = strings.ToLower(newUsername) @@ -297,12 +296,13 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { if err != nil { // TODO better error not sql errors = append(errors, err.Error()) + } else { + log.Printf("Changed email for %s from %s to %s", authUser, me.Email, newEmail) } } if newUsername != authUser { // Rename User err = renameUser(authUser, newUsername) - fmt.Println(newEmail, me.Email, newUsername, authUser) if err != nil { errors = append(errors, err.Error()) } else { @@ -556,6 +556,17 @@ func userFile(w http.ResponseWriter, r *http.Request) { } } +func deleteAccountHandler(w http.ResponseWriter, r *http.Request) { + _, authUser, _ := getAuthUser(r) + err := deleteUser(authUser) + if err != nil { + log.Println(err) + renderDefaultError(w, http.StatusInternalServerError) + return + } + logoutHandler(w, r) +} + func adminUserHandler(w http.ResponseWriter, r *http.Request) { _, _, isAdmin := getAuthUser(r) if r.Method == "POST" { @@ -609,6 +620,7 @@ func runHTTPServer() { serveMux.HandleFunc(hostname+"/logout", logoutHandler) serveMux.HandleFunc(hostname+"/register", registerHandler) serveMux.HandleFunc(hostname+"/delete/", deleteFileHandler) + serveMux.HandleFunc(hostname+"/delete-account", deleteAccountHandler) // admin commands serveMux.HandleFunc(hostname+"/admin/user/", adminUserHandler) diff --git a/templates/my_site.html b/templates/my_site.html @@ -70,5 +70,4 @@ <input type="submit" value="Upload file" class="button" /> </form> <br> -<p><a href="/my_site/flounder-archive.zip">🗄️ Download site archive (.zip)</a></p> {{template "footer" .}}