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 log.Println("Made admin user", username)
51 return nil
52}
53
54func activateUser(username string) error {
55 _, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username)
56 if err != nil {
57 return err
58 }
59 log.Println("Activated user", username)
60 baseIndex := `# Welcome to Flounder!
61## About
62Welcome 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:
63=> //admin.flounder.online
64
65And 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.
66=> //admin.flounder.online/gemini_text_guide.gmi
67
68Have fun!`
69 // Redundant filepath.Clean call just in case.
70 username = filepath.Clean(username)
71 os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
72 ioutil.WriteFile(path.Join(c.FilesDirectory, username, "index.gmi"), []byte(baseIndex), 0644)
73 os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
74 return nil
75}
76
77func deleteUser(username string) error {
78 _, err := DB.Exec("DELETE FROM user WHERE username = $1", username)
79 if err != nil {
80 return err
81 }
82 username = filepath.Clean(username)
83 os.RemoveAll(path.Join(c.FilesDirectory, username))
84 return nil
85}