global settings: add createUserDir option. new feature: auto create user home dir while adding user.

This commit is contained in:
荒野無燈 2019-02-15 23:12:02 +08:00
parent cf985676b9
commit e8570e4dba
7 changed files with 107 additions and 0 deletions

View File

@ -89,6 +89,7 @@ func printSettings(ser *settings.Server, set *settings.Settings, auther auth.Aut
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "Sign up:\t%t\n", set.Signup)
fmt.Fprintf(w, "Create User Dir:\t%t\n", set.CreateUserDir)
fmt.Fprintf(w, "Auth method:\t%s\n", set.AuthMethod)
fmt.Fprintf(w, "Shell:\t%s\t\n", strings.Join(set.Shell, " "))
fmt.Fprintln(w, "\nBranding:")

View File

@ -215,6 +215,7 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {
set := &settings.Settings{
Key: generateKey(),
Signup: false,
CreateUserDir: false,
Defaults: settings.UserDefaults{
Scope: ".",
Locale: "en",

View File

@ -1,6 +1,7 @@
package cmd
import (
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra"
)
@ -30,6 +31,19 @@ var usersAddCmd = &cobra.Command{
}
s.Defaults.Apply(user)
servSettings, err := d.store.Settings.GetServer()
checkErr(err)
//since getUserDefaults() polluted s.Defaults.Scope
//which makes the Scope not the one saved in the db
//we need the right s.Defaults.Scope here
s2, err := d.store.Settings.Get()
checkErr(err)
userHome, err := settings.CreateUserDir(user.Username, user.Scope, servSettings.Root, s2)
checkErr(err)
user.Scope = userHome
err = d.store.Users.Save(user)
checkErr(err)
printUsers([]*users.User{user})

View File

@ -10,6 +10,7 @@ import (
type settingsData struct {
Signup bool `json:"signup"`
CreateUserDir bool `json:"createUserDir"`
Defaults settings.UserDefaults `json:"defaults"`
Rules []rules.Rule `json:"rules"`
Branding settings.Branding `json:"branding"`
@ -20,6 +21,7 @@ type settingsData struct {
var settingsGetHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
data := &settingsData{
Signup: d.settings.Signup,
CreateUserDir: d.settings.CreateUserDir,
Defaults: d.settings.Defaults,
Rules: d.settings.Rules,
Branding: d.settings.Branding,
@ -38,6 +40,7 @@ var settingsPutHandler = withAdmin(func(w http.ResponseWriter, r *http.Request,
}
d.settings.Signup = req.Signup
d.settings.CreateUserDir = req.CreateUserDir
d.settings.Defaults = req.Defaults
d.settings.Rules = req.Rules
d.settings.Branding = req.Branding

View File

@ -2,6 +2,8 @@ package http
import (
"encoding/json"
"github.com/filebrowser/filebrowser/v2/settings"
"log"
"net/http"
"sort"
"strconv"
@ -119,6 +121,14 @@ var userPostHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *
return http.StatusInternalServerError, err
}
userHome,err := settings.CreateUserDir(req.Data.Username, req.Data.Scope, d.server.Root, d.settings)
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
return http.StatusInternalServerError, err
}
req.Data.Scope = userHome
log.Printf("user: %s, home dir: [%s].", req.Data.Username, userHome)
err = d.store.Users.Save(req.Data)
if err != nil {
return http.StatusInternalServerError, err

77
settings/dir.go Normal file
View File

@ -0,0 +1,77 @@
package settings
import (
"errors"
"github.com/spf13/afero"
"log"
"os"
"regexp"
"strings"
)
var (
invalidFilenameChars = regexp.MustCompile(`[^0-9A-Za-z@_\-.]`)
dashes = regexp.MustCompile(`[\-]+`)
)
func CreateUserDir(username, userScope, serverRoot string, settings *Settings) (string, error) {
var err error
userScope = strings.TrimSpace(userScope)
if userScope == "" || userScope == "./" {
userScope = "."
}
if !settings.CreateUserDir {
return userScope, nil
}
fs := afero.NewBasePathFs(afero.NewOsFs(), serverRoot)
//use the default auto create logic only if specific scope is not the default scope
if userScope != settings.Defaults.Scope {
//try create the dir, for example: settings.Defaults.Scope == "." and userScope == "./foo"
if userScope != "." {
err = fs.MkdirAll(userScope, os.ModePerm)
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userScope)
}
}
return userScope, err
}
//clean username first
username = cleanUsername(username)
if username == "" || username == "-" || username == "." {
log.Printf("create user: invalid user for home dir creation: [%s]", username)
return "", errors.New("invalid user for home dir creation")
}
//create default user dir
userHomeBase := settings.Defaults.Scope + string(os.PathSeparator) + "users"
userHome := userHomeBase + string(os.PathSeparator) + username
err = fs.MkdirAll(userHome, os.ModePerm)
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
} else {
log.Printf("create user: mkdir user home dir: [%s] successfully.", userHome)
}
return userHome,err
}
func cleanUsername(s string) string {
// Remove any trailing space to avoid ending on -
s = strings.Trim(s, " ")
s = strings.Replace(s, "..", "", -1)
// Replace all characters which not in the list `0-9A-Za-z@_\-.` with a dash
s = invalidFilenameChars.ReplaceAllString(s, "-")
// Remove any multiple dashes caused by replacements above
s = dashes.ReplaceAllString(s, "-")
return s
}

View File

@ -14,6 +14,7 @@ type AuthMethod string
type Settings struct {
Key []byte `json:"key"`
Signup bool `json:"signup"`
CreateUserDir bool `json:"createUserDir"`
Defaults UserDefaults `json:"defaults"`
AuthMethod AuthMethod `json:"authMethod"`
Branding Branding `json:"branding"`