Skip to content

Instantly share code, notes, and snippets.

@KuanYuChang
Last active October 30, 2019 17:44
Show Gist options
  • Save KuanYuChang/a802e92c84483a68dba764e7ab413d0d to your computer and use it in GitHub Desktop.
Save KuanYuChang/a802e92c84483a68dba764e7ab413d0d to your computer and use it in GitHub Desktop.
add a new user to filebrowser v1.10.0. Link: https://github.com/filebrowser/filebrowser
package main
import "os"
import "fmt"
import "strings"
import "github.com/hacdias/fileutils"
import "github.com/asdine/storm"
import fb "github.com/filebrowser/filebrowser/lib"
import "github.com/filebrowser/filebrowser/lib/bolt"
var DefaultUser = fb.User{
AllowCommands: false,
AllowEdit: true,
AllowNew: true,
AllowPublish: false,
LockPassword: true,
Commands: []string{},
Rules: []*fb.Rule{},
CSS: "",
Admin: false,
Locale: "",
Scope: ".",
FileSystem: fileutils.Dir("."),
ViewMode: "mosaic",
}
func main () {
if len(os.Args) != 4 {
fmt.Println ("[Error] Missing group, username or password")
fmt.Println ("[Info] Usage:", os.Args [0], "<group> <username> <passwd>")
os.Exit (1)
}
db, err := storm.Open("filebrowser.db")
if err != nil {
fmt.Println ("[Error] Something goes wrong with the \"filebrowser.db\"")
os.Exit (1)
}
Group := strings.ToLower (os.Args [1])
Username := os.Args [2]
Password := os.Args [3]
Scope := "./" + Group + "/" + Username
u := DefaultUser
u.Username = Username
u.Password, _ = fb.HashPassword (Password)
if strings.Compare (Group, "admin") == 0 {
u.Admin = true
} else {
u.Scope = Scope
u.FileSystem = fileutils.Dir (u.Scope)
}
userStore := bolt.UsersStore{DB: db}
err = userStore.Save (&u)
if err != nil {
fmt.Println ("[Error] Something goes wrong while creating", Username)
os.Exit (1)
}
if strings.Compare (Group, "admin") != 0 {
err = os.MkdirAll(Scope, os.ModePerm)
if err != nil {
fmt.Println ("[Error] Something goes wrong while creating scope of", Username)
os.Exit (1)
}
}
defer db.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment