MakeUserDir makes the user directory according to settings.
(username, userScope, serverRoot string)
| 20 | |
| 21 | // MakeUserDir makes the user directory according to settings. |
| 22 | func (s *Settings) MakeUserDir(username, userScope, serverRoot string) (string, error) { |
| 23 | userScope = strings.TrimSpace(userScope) |
| 24 | if userScope == "" && s.CreateUserDir { |
| 25 | username = cleanUsername(username) |
| 26 | if username == "" || username == "-" || username == "." { |
| 27 | log.Printf("create user: invalid user for home dir creation: [%s]", username) |
| 28 | return "", errors.New("invalid user for home dir creation") |
| 29 | } |
| 30 | userScope = path.Join(s.UserHomeBasePath, username) |
| 31 | } |
| 32 | |
| 33 | userScope = path.Join("/", userScope) |
| 34 | |
| 35 | fs := afero.NewBasePathFs(afero.NewOsFs(), serverRoot) |
| 36 | if err := fs.MkdirAll(userScope, os.ModePerm); err != nil { |
| 37 | return "", fmt.Errorf("failed to create user home dir: [%s]: %w", userScope, err) |
| 38 | } |
| 39 | return userScope, nil |
| 40 | } |
| 41 | |
| 42 | func cleanUsername(s string) string { |
| 43 | // Remove any trailing space to avoid ending on - |
no test coverage detected