2017-07-08 16:51:47 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/asdine/storm"
|
|
|
|
)
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// usersHandler is the entry point of the users API. It's just a router
|
|
|
|
// to send the request to its
|
2017-07-11 15:58:18 +00:00
|
|
|
func usersHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-26 16:50:39 +00:00
|
|
|
if r.URL.Path == "/change-password" {
|
|
|
|
return usersUpdatePassword(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/change-css" {
|
|
|
|
return usersUpdateCSS(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is admin and the HTTP Method is not
|
|
|
|
// PUT, then we return forbidden.
|
|
|
|
if !c.User.Admin {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return usersGetHandler(c, w, r)
|
|
|
|
case http.MethodPost:
|
|
|
|
return usersPostHandler(c, w, r)
|
|
|
|
case http.MethodDelete:
|
|
|
|
return usersDeleteHandler(c, w, r)
|
|
|
|
case http.MethodPut:
|
|
|
|
return usersPutHandler(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// getUserID returns the id from the user which is present
|
|
|
|
// in the request url. If the url is invalid and doesn't
|
|
|
|
// contain a valid ID, it returns an error.
|
|
|
|
func getUserID(r *http.Request) (int, error) {
|
|
|
|
// Obtains the ID in string from the URL and converts
|
|
|
|
// it into an integer.
|
|
|
|
sid := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
sid = strings.TrimSuffix(sid, "/")
|
|
|
|
id, err := strconv.Atoi(sid)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getUser returns the user which is present in the request
|
|
|
|
// body. If the body is empty or the JSON is invalid, it
|
|
|
|
// returns an error.
|
|
|
|
func getUser(r *http.Request) (*User, error) {
|
|
|
|
if r.Body == nil {
|
|
|
|
return nil, errEmptyRequest
|
|
|
|
}
|
|
|
|
|
2017-07-26 17:08:49 +00:00
|
|
|
u := &User{}
|
2017-07-26 16:50:39 +00:00
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func usersGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-26 16:50:39 +00:00
|
|
|
// Request for the default user data.
|
|
|
|
if r.URL.Path == "/base" {
|
|
|
|
return renderJSON(w, c.FM.DefaultUser)
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// Request for the listing of users.
|
2017-07-08 16:51:47 +00:00
|
|
|
if r.URL.Path == "/" {
|
|
|
|
users := []User{}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
for _, user := range c.FM.Users {
|
2017-07-26 16:50:39 +00:00
|
|
|
// Copies the user info and removes its
|
|
|
|
// password so it won't be sent to the
|
|
|
|
// front-end.
|
2017-07-08 16:51:47 +00:00
|
|
|
u := *user
|
|
|
|
u.Password = ""
|
|
|
|
users = append(users, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(users, func(i, j int) bool {
|
|
|
|
return users[i].ID < users[j].ID
|
|
|
|
})
|
|
|
|
|
|
|
|
return renderJSON(w, users)
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
id, err := getUserID(r)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Searches for the user and prints the one who matches.
|
2017-07-11 15:58:18 +00:00
|
|
|
for _, user := range c.FM.Users {
|
2017-07-08 16:51:47 +00:00
|
|
|
if user.ID != id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u := *user
|
|
|
|
u.Password = ""
|
|
|
|
return renderJSON(w, u)
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// If there aren't any matches, return not found.
|
|
|
|
return http.StatusNotFound, errUserNotExist
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-08 16:51:47 +00:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
u, err := getUser(r)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusBadRequest, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// The username, password and scope cannot be empty.
|
2017-07-08 16:51:47 +00:00
|
|
|
if u.Username == "" || u.Password == "" || u.FileSystem == "" {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusBadRequest, errors.New("username, password or scope is empty")
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize rules if they're not initialized.
|
|
|
|
if u.Rules == nil {
|
|
|
|
u.Rules = []*Rule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize commands if not initialized.
|
|
|
|
if u.Commands == nil {
|
|
|
|
u.Commands = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's a new user so the ID will be auto created.
|
|
|
|
if u.ID != 0 {
|
|
|
|
u.ID = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashes the password.
|
|
|
|
pw, err := hashPassword(u.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Password = pw
|
|
|
|
|
|
|
|
// Saves the user to the database.
|
2017-07-26 17:08:49 +00:00
|
|
|
err = c.FM.db.Save(u)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err == storm.ErrAlreadyExists {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusConflict, errUserExist
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Saves the user to the memory.
|
2017-07-26 16:50:39 +00:00
|
|
|
c.FM.Users[u.Username] = u
|
2017-07-08 16:51:47 +00:00
|
|
|
|
|
|
|
// Set the Location header and return.
|
|
|
|
w.Header().Set("Location", "/users/"+strconv.Itoa(u.ID))
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func usersDeleteHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-08 16:51:47 +00:00
|
|
|
if r.URL.Path == "/" {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
id, err := getUserID(r)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// Deletes the user from the database.
|
2017-07-11 15:58:18 +00:00
|
|
|
err = c.FM.db.DeleteStruct(&User{ID: id})
|
2017-07-08 16:51:47 +00:00
|
|
|
if err == storm.ErrNotFound {
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusNotFound, errUserNotExist
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
// Delete the user from the in-memory users map.
|
2017-07-11 15:58:18 +00:00
|
|
|
for _, user := range c.FM.Users {
|
2017-07-08 16:51:47 +00:00
|
|
|
if user.ID == id {
|
2017-07-11 15:58:18 +00:00
|
|
|
delete(c.FM.Users, user.Username)
|
2017-07-26 16:50:39 +00:00
|
|
|
break
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
func usersUpdatePassword(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
if r.Method != http.MethodPut {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
u, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
if u.Password == "" {
|
|
|
|
return http.StatusBadRequest, errEmptyPassword
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
pw, err := hashPassword(u.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
c.User.Password = pw
|
|
|
|
err = c.FM.db.UpdateField(&User{ID: c.User.ID}, "Password", pw)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
func usersUpdateCSS(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
if r.Method != http.MethodPut {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
u, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
c.User.CSS = u.CSS
|
|
|
|
err = c.FM.db.UpdateField(&User{ID: c.User.ID}, "CSS", u.CSS)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
// New users should be created on /api/users.
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
id, err := getUserID(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
u, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The username and the filesystem cannot be empty.
|
|
|
|
if u.Username == "" || u.FileSystem == "" {
|
|
|
|
return http.StatusBadRequest, errors.New("Username, password or scope are empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize rules if they're not initialized.
|
|
|
|
if u.Rules == nil {
|
|
|
|
u.Rules = []*Rule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize commands if not initialized.
|
|
|
|
if u.Commands == nil {
|
|
|
|
u.Commands = []string{}
|
|
|
|
}
|
|
|
|
|
2017-07-19 06:55:58 +00:00
|
|
|
var ouser *User
|
|
|
|
for _, user := range c.FM.Users {
|
|
|
|
if user.ID == id {
|
|
|
|
ouser = user
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ouser == nil {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
u.ID = id
|
|
|
|
|
|
|
|
if u.Password == "" {
|
|
|
|
u.Password = ouser.Password
|
|
|
|
} else {
|
|
|
|
pw, err := hashPassword(u.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Password = pw
|
|
|
|
}
|
|
|
|
|
2017-07-14 07:25:37 +00:00
|
|
|
if u.Permissions == nil {
|
|
|
|
u.Permissions = c.FM.DefaultUser.Permissions
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
// Updates the whole User struct because we always are supposed
|
|
|
|
// to send a new entire object.
|
2017-07-26 17:08:49 +00:00
|
|
|
err = c.FM.db.Save(u)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-19 06:55:58 +00:00
|
|
|
// If the user changed the username, delete the old user
|
|
|
|
// from the in-memory user map.
|
|
|
|
if ouser.Username != u.Username {
|
|
|
|
delete(c.FM.Users, ouser.Username)
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:50:39 +00:00
|
|
|
c.FM.Users[u.Username] = u
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|