diff --git a/users.go b/users.go index 8b23e838..60a12e5d 100644 --- a/users.go +++ b/users.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "net/http" + "os" "sort" "strconv" "strings" @@ -175,6 +176,11 @@ func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) u.ID = 0 } + // Checks if the scope exists. + if code, err := checkFS(string(u.FileSystem)); err != nil { + return code, err + } + // Hashes the password. pw, err := hashPassword(u.Password) if err != nil { @@ -202,6 +208,28 @@ func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) return 0, nil } +func checkFS(path string) (int, error) { + info, err := os.Stat(path) + + if err != nil { + if !os.IsNotExist(err) { + return http.StatusInternalServerError, err + } + + err = os.MkdirAll(path, 0666) + if err != nil { + return http.StatusInternalServerError, err + } + + } + + if !info.IsDir() { + return http.StatusBadRequest, errors.New("Scope is not a dir") + } + + return 0, nil +} + func usersDeleteHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { if r.URL.Path == "/" { return http.StatusMethodNotAllowed, nil @@ -308,6 +336,11 @@ func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) return http.StatusBadRequest, errEmptyScope } + // Checks if the scope exists. + if code, err := checkFS(string(u.FileSystem)); err != nil { + return code, err + } + // Initialize rules if they're not initialized. if u.Rules == nil { u.Rules = []*Rule{} @@ -344,8 +377,6 @@ func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) u.Password = suser.Password } - - // Updates the whole User struct because we always are supposed // to send a new entire object. err = c.db.Save(u)