Add --create-admin option
This enables automated user creation by running: writefreely --create-admin username:password It will fail if an admin (first user) already exists, which makes this suitable for use on both for single- and multi-user instances. Closes T544
This commit is contained in:
parent
8ceb165020
commit
df4cd9ed00
1 changed files with 50 additions and 0 deletions
50
app.go
50
app.go
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/gorilla/sessions"
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/writeas/go-strip-markdown"
|
||||
"github.com/writeas/web-core/auth"
|
||||
"github.com/writeas/web-core/converter"
|
||||
"github.com/writeas/web-core/log"
|
||||
"github.com/writeas/writefreely/config"
|
||||
|
@ -178,6 +179,7 @@ func Serve() {
|
|||
doConfig := flag.Bool("config", false, "Run the configuration process")
|
||||
genKeys := flag.Bool("gen-keys", false, "Generate encryption and authentication keys")
|
||||
createSchema := flag.Bool("init-db", false, "Initialize app database")
|
||||
createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password")
|
||||
resetPassUser := flag.String("reset-pass", "", "Reset the given user's password")
|
||||
outputVersion := flag.Bool("v", false, "Output the current version")
|
||||
flag.Parse()
|
||||
|
@ -281,6 +283,54 @@ func Serve() {
|
|||
}
|
||||
}
|
||||
os.Exit(0)
|
||||
} else if *createAdmin != "" {
|
||||
// Create an admin user with --create-admin
|
||||
creds := strings.Split(*createAdmin, ":")
|
||||
if len(creds) != 2 {
|
||||
log.Error("usage: writefreely --create-admin username:password")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Info("Loading configuration...")
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Error("Unable to load configuration: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
app.cfg = cfg
|
||||
connectToDatabase(app)
|
||||
defer shutdown(app)
|
||||
|
||||
// Ensure an admin / first user doesn't already exist
|
||||
if u, _ := app.db.GetUserByID(1); u != nil {
|
||||
log.Error("Admin user already exists (%s). Aborting.", u.Username)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Create the user
|
||||
username := creds[0]
|
||||
password := creds[1]
|
||||
|
||||
hashedPass, err := auth.HashPass([]byte(password))
|
||||
if err != nil {
|
||||
log.Error("Unable to hash password: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
u := &User{
|
||||
Username: username,
|
||||
HashedPass: hashedPass,
|
||||
Created: time.Now().Truncate(time.Second).UTC(),
|
||||
}
|
||||
|
||||
log.Info("Creating user %s...\n", u.Username)
|
||||
err = app.db.CreateUser(u, "")
|
||||
if err != nil {
|
||||
log.Error("Unable to create user: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("Done!")
|
||||
os.Exit(0)
|
||||
} else if *resetPassUser != "" {
|
||||
// Connect to the database
|
||||
log.Info("Loading configuration...")
|
||||
|
|
Loading…
Add table
Reference in a new issue