You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
global settings: add createUserDir option. new feature: auto create user home dir while adding user.
Former-commit-id: 331a76abdc611236ccc761d0fd9a814ed1ee0c35 [formerly 0c1024a5b8109c84d213e0cbdbe05e10eb5793d4] [formerly 467e1789f55c410ff2ca9e9ef125d9fe28410bc9 [formerly e8570e4dba]]
Former-commit-id: 1eed58870b6e009d84806db6b55efc5fc3983e2a [formerly 3e9083f7758e72bd307ed23c4b512a8ab5adc523]
Former-commit-id: 5023ef77eb92636e62fde511ea609114e667a7d7
This commit is contained in:
77
settings/dir.go
Normal file
77
settings/dir.go
Normal 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
|
||||
}
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user