pull/196/head
Henrique Dias 2017-08-10 11:21:06 +01:00
parent 567e24231f
commit e533a3aa6e
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
1 changed files with 33 additions and 2 deletions

View File

@ -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)