all repos — flounder @ 6b3c7281a94fa0e8efbe12901b526fcb17dd29a1

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	"fmt"
11	"io/ioutil"
12	"log"
13	"os"
14	"path"
15	"path/filepath"
16)
17
18// TODO improve cli
19func runAdminCommand() {
20	if len(os.Args) < 4 {
21		fmt.Println("expected subcommand with parameter")
22		os.Exit(1)
23	}
24	switch os.Args[2] {
25	case "activate-user":
26		username := os.Args[3]
27		err := activateUser(username)
28		log.Fatal(err)
29	case "delete-user":
30		username := os.Args[3]
31		// TODO add confirmation
32		err := deleteUser(username)
33		log.Fatal(err)
34	case "make-admin":
35		username := os.Args[3]
36		err := makeAdmin(username)
37		log.Fatal(err)
38	}
39	// reset password
40
41}
42
43func makeAdmin(username string) error {
44	_, err := DB.Exec("UPDATE user SET admin = true WHERE username = $1", username)
45	if err != nil {
46		return err
47	}
48	return nil
49}
50
51func activateUser(username string) error {
52	_, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username)
53	if err != nil {
54		return err
55	}
56	log.Println("Activated user", username)
57	baseIndex := `# Welcome to Flounder!
58## About
59Welcome 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:
60=> //admin.flounder.online
61
62And 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.
63=> //admin.flounder.online/gemini_text_guide.gmi
64
65Have fun!`
66	// Redundant filepath.Clean call just in case.
67	username = filepath.Clean(username)
68	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
69	ioutil.WriteFile(path.Join(c.FilesDirectory, username, "index.gmi"), []byte(baseIndex), 0644)
70	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
71	return nil
72}
73
74func deleteUser(username string) error {
75	_, err := DB.Exec("DELETE FROM user WHERE username = $1", username)
76	if err != nil {
77		return err
78	}
79	username = filepath.Clean(username)
80	os.RemoveAll(path.Join(c.FilesDirectory, username))
81	return nil
82}