2019-01-05 22:44:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-02-15 15:12:02 +00:00
|
|
|
"log"
|
2019-01-05 22:44:33 +00:00
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
2020-12-24 17:22:48 +00:00
|
|
|
var (
|
|
|
|
NonModifiableFieldsForNonAdmin = []string{"Username", "Scope", "LockPassword", "Perm", "Commands", "Rules"}
|
|
|
|
)
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
type modifyUserRequest struct {
|
|
|
|
modifyRequest
|
|
|
|
Data *users.User `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserID(r *http.Request) (uint, error) {
|
|
|
|
vars := mux.Vars(r)
|
2021-07-26 10:00:05 +00:00
|
|
|
i, err := strconv.ParseUint(vars["id"], 10, 0) //nolint:gomnd
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint(i), err
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func getUser(_ http.ResponseWriter, r *http.Request) (*modifyUserRequest, error) {
|
2019-01-05 22:44:33 +00:00
|
|
|
if r.Body == nil {
|
|
|
|
return nil, errors.ErrEmptyRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &modifyUserRequest{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.What != "user" {
|
|
|
|
return nil, errors.ErrInvalidDataType
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func withSelfOrAdmin(fn handleFunc) handleFunc {
|
|
|
|
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
id, err := getUserID(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.user.ID != id && !d.user.Perm.Admin {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.raw = id
|
|
|
|
return fn(w, r, d)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersGetHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
2019-01-08 10:29:09 +00:00
|
|
|
users, err := d.store.Users.Gets(d.server.Root)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range users {
|
|
|
|
u.Password = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(users, func(i, j int) bool {
|
|
|
|
return users[i].ID < users[j].ID
|
|
|
|
})
|
|
|
|
|
|
|
|
return renderJSON(w, r, users)
|
|
|
|
})
|
|
|
|
|
|
|
|
var userGetHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
2019-01-08 10:29:09 +00:00
|
|
|
u, err := d.store.Users.Get(d.server.Root, d.raw.(uint))
|
2019-01-05 22:44:33 +00:00
|
|
|
if err == errors.ErrNotExist {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Password = ""
|
|
|
|
return renderJSON(w, r, u)
|
|
|
|
})
|
|
|
|
|
|
|
|
var userDeleteHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
err := d.store.Users.Delete(d.raw.(uint))
|
2021-01-11 21:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return errToStatus(err), err
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var userPostHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
req, err := getUser(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Which) != 0 {
|
|
|
|
return http.StatusBadRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Data.Password == "" {
|
|
|
|
return http.StatusBadRequest, errors.ErrEmptyPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Data.Password, err = users.HashPwd(req.Data.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2019-04-20 13:15:28 +00:00
|
|
|
userHome, err := d.settings.MakeUserDir(req.Data.Username, req.Data.Scope, d.server.Root)
|
2019-02-15 15:12:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
req.Data.Scope = userHome
|
|
|
|
log.Printf("user: %s, home dir: [%s].", req.Data.Username, userHome)
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
err = d.store.Users.Save(req.Data)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2021-07-26 10:00:05 +00:00
|
|
|
w.Header().Set("Location", "/settings/users/"+strconv.FormatUint(uint64(req.Data.ID), 10)) //nolint:gomnd
|
2019-01-05 22:44:33 +00:00
|
|
|
return http.StatusCreated, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var userPutHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
req, err := getUser(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Data.ID != d.raw.(uint) {
|
|
|
|
return http.StatusBadRequest, nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:22:48 +00:00
|
|
|
if len(req.Which) == 0 || (len(req.Which) == 1 && req.Which[0] == "all") {
|
2019-01-05 22:44:33 +00:00
|
|
|
if !d.user.Perm.Admin {
|
2020-12-24 17:22:48 +00:00
|
|
|
return http.StatusForbidden, nil
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Data.Password != "" {
|
|
|
|
req.Data.Password, err = users.HashPwd(req.Data.Password)
|
|
|
|
} else {
|
|
|
|
var suser *users.User
|
2019-01-08 10:29:09 +00:00
|
|
|
suser, err = d.store.Users.Get(d.server.Root, d.raw.(uint))
|
2019-01-05 22:44:33 +00:00
|
|
|
req.Data.Password = suser.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Which = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range req.Which {
|
2020-12-24 17:22:48 +00:00
|
|
|
v = strings.Title(v)
|
|
|
|
req.Which[k] = v
|
|
|
|
|
|
|
|
if v == "Password" {
|
2019-01-05 22:44:33 +00:00
|
|
|
if !d.user.Perm.Admin && d.user.LockPassword {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Data.Password, err = users.HashPwd(req.Data.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:22:48 +00:00
|
|
|
for _, f := range NonModifiableFieldsForNonAdmin {
|
|
|
|
if !d.user.Perm.Admin && v == f {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.store.Users.Update(req.Data, req.Which...)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
})
|