all repos — flounder @ d8efa1bc645cd8051a8ae6ee2744017ca11611d4

A small site builder for the Gemini protocol

admin.go (view raw)

  1package main
  2
  3// Commands for administering your instance
  4// reset user password -> generate link
  5// delete user
  6
  7// Run some scripts to setup your instance
  8
  9import (
 10	"flag"
 11	"fmt"
 12	"io/ioutil"
 13	"log"
 14	"os"
 15	"path"
 16	"path/filepath"
 17)
 18
 19// TODO improve cli
 20func runAdminCommand() {
 21	args := flag.Args() // again?
 22	if len(args) < 3 {
 23		fmt.Println("Expected subcommand with parameter activate-user|delete-user|make-admin")
 24		os.Exit(1)
 25	}
 26	var err error
 27	switch args[1] {
 28	case "activate-user":
 29		username := args[2]
 30		err = activateUser(username)
 31	case "delete-user":
 32		username := args[2]
 33		// TODO add confirmation
 34		err = deleteUser(username)
 35	case "make-admin":
 36		username := args[2]
 37		err = makeAdmin(username)
 38	case "rename-user":
 39		username := args[2]
 40		newUsername := args[3]
 41		err = renameUser(username, newUsername)
 42	}
 43	if err != nil {
 44		log.Fatal(err)
 45	}
 46	// reset password
 47
 48}
 49
 50func makeAdmin(username string) error {
 51	_, err := DB.Exec("UPDATE user SET admin = true WHERE username = $1", username)
 52	if err != nil {
 53		return err
 54	}
 55	log.Println("Made admin user", username)
 56	return nil
 57}
 58
 59func activateUser(username string) error {
 60	_, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username)
 61	if err != nil {
 62		return err
 63	}
 64	log.Println("Activated user", username)
 65	baseIndex := `# Welcome to Flounder!
 66## About
 67Welcome to an ultra-lightweight platform for making and sharing small websites. You can get started by editing this page -- remove this content and replace it with whatever you like! It will be live at <your-name>.flounder.online. You can go there right now to see what this page currently looks like. Here is a link to a page which will give you more information about using flounder:
 68=> //admin.flounder.online
 69
 70And here's a guide to the text format that Flounder uses to create pages, Gemini. These pages are converted into HTML so they can be displayed in a web browser.
 71=> //admin.flounder.online/gemini_text_guide.gmi
 72
 73Have fun!`
 74	// Redundant filepath.Clean call just in case.
 75	username = filepath.Clean(username)
 76	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
 77	ioutil.WriteFile(path.Join(c.FilesDirectory, username, "index.gmi"), []byte(baseIndex), 0644)
 78	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
 79	return nil
 80}
 81
 82func renameUser(oldUsername string, newUsername string) error {
 83	err := isOkUsername(newUsername)
 84	if err != nil {
 85		return err
 86	}
 87	fmt.Println("Old user", oldUsername)
 88	fmt.Println("new user", newUsername)
 89	res, err := DB.Exec("UPDATE user set username = ? WHERE username = ?", newUsername, oldUsername)
 90	if err != nil {
 91		return err
 92	}
 93	rowsAffected, err := res.RowsAffected()
 94	if rowsAffected != 1 {
 95		return fmt.Errorf("No User updated %s %s", oldUsername, newUsername)
 96	} else if err != nil {
 97		return err
 98	}
 99	userFolder := path.Join(c.FilesDirectory, oldUsername)
100	newUserFolder := path.Join(c.FilesDirectory, newUsername)
101	err = os.Rename(userFolder, newUserFolder)
102	if err != nil {
103		// This would be bad. User in broken, insecure state.
104		// TODO some sort of better handling?
105		return err
106	}
107	return nil
108}
109
110func deleteUser(username string) error {
111	_, err := DB.Exec("DELETE FROM user WHERE username = $1", username)
112	if err != nil {
113		return err
114	}
115	username = filepath.Clean(username)
116	os.RemoveAll(path.Join(c.FilesDirectory, username))
117	return nil
118}