152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
fberrors "github.com/filebrowser/filebrowser/v2/errors"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
)
|
|
|
|
var (
|
|
NonModifiableFieldsForNonAdmin = []string{"Username", "Scope", "LockPassword", "Perm", "Commands", "Rules"}
|
|
)
|
|
|
|
type modifyUserRequest struct {
|
|
modifyRequest
|
|
Data *users.User `json:"data"`
|
|
}
|
|
|
|
func getUserID(r *http.Request) (uint, error) {
|
|
vars := mux.Vars(r)
|
|
i, err := strconv.ParseUint(vars["id"], 10, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return uint(i), err
|
|
}
|
|
|
|
func getUser(w http.ResponseWriter, r *http.Request) (*modifyUserRequest, error) {
|
|
if r.Body == nil {
|
|
return nil, fberrors.ErrEmptyRequest
|
|
}
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
|
req := &modifyUserRequest{}
|
|
err := json.NewDecoder(r.Body).Decode(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if req.What != "user" {
|
|
return nil, fberrors.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) {
|
|
users, err := d.store.Users.Gets(d.server.Root, d.server.FollowExternalSymlinks)
|
|
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) {
|
|
u, err := d.store.Users.Get(d.server.Root, d.server.FollowExternalSymlinks, d.raw.(uint))
|
|
if errors.Is(err, fberrors.ErrNotExist) {
|
|
return http.StatusNotFound, err
|
|
}
|
|
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
u.Password = ""
|
|
if !d.user.Perm.Admin {
|
|
u.Scope = ""
|
|
}
|
|
return renderJSON(w, r, u)
|
|
})
|
|
|
|
var userDeleteHandler = withSelfOrAdmin(func(_ http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
if r.Body == nil {
|
|
return http.StatusBadRequest, fberrors.ErrEmptyRequest
|
|
}
|
|
|
|
var body struct {
|
|
CurrentPassword string `json:"current_password"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
return http.StatusBadRequest, err
|
|
}
|
|
|
|
err := d.store.Users.Delete(d.raw.(uint))
|
|
if err != nil {
|
|
return errToStatus(err), err
|
|
}
|
|
|
|
return http.StatusOK, 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 == nil || req.Data.ID != d.raw.(uint) || len(req.Which) == 0 || req.Data.Password != "" {
|
|
return http.StatusBadRequest, nil
|
|
}
|
|
allowed := map[string]string{"scope": "Scope", "perm": "Perm", "rules": "Rules", "locale": "Locale", "viewmode": "ViewMode", "singleclick": "SingleClick", "redirectaftercopymove": "RedirectAfterCopyMove", "hidedotfiles": "HideDotfiles", "hidepreviews": "HidePreviews", "dicom3d": "Dicom3D", "dateformat": "DateFormat", "aceeditortheme": "AceEditorTheme", "sorting": "Sorting"}
|
|
for i, field := range req.Which {
|
|
canonical, ok := allowed[strings.ToLower(field)]
|
|
if !ok {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
if !d.user.Perm.Admin && (canonical == "Scope" || canonical == "Perm" || canonical == "Rules") {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
req.Which[i] = canonical
|
|
}
|
|
if req.Data.Perm.Share && !req.Data.Perm.Download {
|
|
return http.StatusBadRequest, fberrors.ErrShareRequiresDownload
|
|
}
|
|
req.Data.Perm.Admin = false
|
|
req.Data.Perm.Execute = false
|
|
err = d.store.Users.Update(req.Data, req.Which...)
|
|
return errToStatus(err), err
|
|
})
|