From 65a53514d552c2867d40d2a8605200e0601d334d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92=E9=87=8E=E7=84=A1=E7=87=88?= Date: Fri, 15 Feb 2019 23:12:02 +0800 Subject: [PATCH] global settings: add createUserDir option. new feature: auto create user home dir while adding user. Former-commit-id: 331a76abdc611236ccc761d0fd9a814ed1ee0c35 [formerly 0c1024a5b8109c84d213e0cbdbe05e10eb5793d4] [formerly 467e1789f55c410ff2ca9e9ef125d9fe28410bc9 [formerly e8570e4dba58d15fae0c5bbd33a531573582fc59]] Former-commit-id: 1eed58870b6e009d84806db6b55efc5fc3983e2a [formerly 3e9083f7758e72bd307ed23c4b512a8ab5adc523] Former-commit-id: 5023ef77eb92636e62fde511ea609114e667a7d7 --- cmd/config.go | 1 + cmd/root.go | 1 + cmd/users_add.go | 14 ++++++++ http/settings.go | 3 ++ http/users.go | 10 ++++++ settings/dir.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ settings/settings.go | 1 + 7 files changed, 107 insertions(+) create mode 100644 settings/dir.go diff --git a/cmd/config.go b/cmd/config.go index db97cf10..25903137 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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:") diff --git a/cmd/root.go b/cmd/root.go index 37aad636..0e8ddaca 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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", diff --git a/cmd/users_add.go b/cmd/users_add.go index f0ce0776..73c71091 100644 --- a/cmd/users_add.go +++ b/cmd/users_add.go @@ -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}) diff --git a/http/settings.go b/http/settings.go index 6a75b930..58036f71 100644 --- a/http/settings.go +++ b/http/settings.go @@ -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 diff --git a/http/users.go b/http/users.go index 67a9d03e..75557235 100644 --- a/http/users.go +++ b/http/users.go @@ -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 diff --git a/settings/dir.go b/settings/dir.go new file mode 100644 index 00000000..f66bbd81 --- /dev/null +++ b/settings/dir.go @@ -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 +} \ No newline at end of file diff --git a/settings/settings.go b/settings/settings.go index 0d485b77..92713cb9 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -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"`