all repos — flounder @ 06055f4269d3561e701595efa385e13bfbd3aa7b

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