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" "encoding/json"
"errors" "errors"
"net/http" "net/http"
"os"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -175,6 +176,11 @@ func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request)
u.ID = 0 u.ID = 0
} }
// Checks if the scope exists.
if code, err := checkFS(string(u.FileSystem)); err != nil {
return code, err
}
// Hashes the password. // Hashes the password.
pw, err := hashPassword(u.Password) pw, err := hashPassword(u.Password)
if err != nil { if err != nil {
@ -202,6 +208,28 @@ func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request)
return 0, nil 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) { func usersDeleteHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path == "/" { if r.URL.Path == "/" {
return http.StatusMethodNotAllowed, nil return http.StatusMethodNotAllowed, nil
@ -308,6 +336,11 @@ func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request)
return http.StatusBadRequest, errEmptyScope 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. // Initialize rules if they're not initialized.
if u.Rules == nil { if u.Rules == nil {
u.Rules = []*Rule{} u.Rules = []*Rule{}
@ -344,8 +377,6 @@ func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request)
u.Password = suser.Password u.Password = suser.Password
} }
// Updates the whole User struct because we always are supposed // Updates the whole User struct because we always are supposed
// to send a new entire object. // to send a new entire object.
err = c.db.Save(u) err = c.db.Save(u)