2019-01-06 06:44:33 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-02 00:24:06 +08:00
|
|
|
"errors"
|
2019-02-15 23:12:02 +08:00
|
|
|
"log"
|
2019-01-06 06:44:33 +08:00
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
"github.com/gorilla/mux"
|
2022-06-13 22:13:10 +08:00
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2020-06-01 07:12:36 +08:00
|
|
|
|
2024-04-02 00:24:06 +08:00
|
|
|
fbErrors "github.com/filebrowser/filebrowser/v2/errors"
|
2019-01-06 06:44:33 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
2020-12-25 01:22:48 +08:00
|
|
|
var (
|
|
|
|
NonModifiableFieldsForNonAdmin = []string{"Username", "Scope", "LockPassword", "Perm", "Commands", "Rules"}
|
|
|
|
)
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
type modifyUserRequest struct {
|
|
|
|
modifyRequest
|
|
|
|
Data *users.User `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserID(r *http.Request) (uint, error) {
|
|
|
|
vars := mux.Vars(r)
|
2023-02-16 16:11:12 +08:00
|
|
|
i, err := strconv.ParseUint(vars["id"], 10, 0)
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint(i), err
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
func getUser(_ http.ResponseWriter, r *http.Request) (*modifyUserRequest, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
if r.Body == nil {
|
2024-04-02 00:24:06 +08:00
|
|
|
return nil, fbErrors.ErrEmptyRequest
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &modifyUserRequest{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.What != "user" {
|
2024-04-02 00:24:06 +08:00
|
|
|
return nil, fbErrors.ErrInvalidDataType
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:29:09 +08:00
|
|
|
users, err := d.store.Users.Gets(d.server.Root)
|
2019-01-06 06:44:33 +08: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 18:29:09 +08:00
|
|
|
u, err := d.store.Users.Get(d.server.Root, d.raw.(uint))
|
2024-04-02 00:24:06 +08:00
|
|
|
if errors.Is(err, fbErrors.ErrNotExist) {
|
2019-01-06 06:44:33 +08:00
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Password = ""
|
2022-02-22 17:58:22 +08:00
|
|
|
if !d.user.Perm.Admin {
|
2022-02-22 03:17:42 +08:00
|
|
|
u.Scope = ""
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
return renderJSON(w, r, u)
|
|
|
|
})
|
|
|
|
|
2024-04-02 00:24:06 +08:00
|
|
|
var userDeleteHandler = withSelfOrAdmin(func(_ http.ResponseWriter, _ *http.Request, d *data) (int, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
err := d.store.Users.Delete(d.raw.(uint))
|
2021-01-12 05:33:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return errToStatus(err), err
|
2019-01-06 06:44:33 +08: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 == "" {
|
2024-04-02 00:24:06 +08:00
|
|
|
return http.StatusBadRequest, fbErrors.ErrEmptyPassword
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Data.Password, err = users.HashPwd(req.Data.Password)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2019-04-20 21:15:28 +08:00
|
|
|
userHome, err := d.settings.MakeUserDir(req.Data.Username, req.Data.Scope, d.server.Root)
|
2019-02-15 23:12:02 +08: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-06 06:44:33 +08:00
|
|
|
err = d.store.Users.Save(req.Data)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2023-02-16 16:11:12 +08:00
|
|
|
w.Header().Set("Location", "/settings/users/"+strconv.FormatUint(uint64(req.Data.ID), 10))
|
2019-01-06 06:44:33 +08: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-25 01:22:48 +08:00
|
|
|
if len(req.Which) == 0 || (len(req.Which) == 1 && req.Which[0] == "all") {
|
2019-01-06 06:44:33 +08:00
|
|
|
if !d.user.Perm.Admin {
|
2020-12-25 01:22:48 +08:00
|
|
|
return http.StatusForbidden, nil
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Data.Password != "" {
|
|
|
|
req.Data.Password, err = users.HashPwd(req.Data.Password)
|
|
|
|
} else {
|
|
|
|
var suser *users.User
|
2019-01-08 18:29:09 +08:00
|
|
|
suser, err = d.store.Users.Get(d.server.Root, d.raw.(uint))
|
2019-01-06 06:44:33 +08:00
|
|
|
req.Data.Password = suser.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Which = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range req.Which {
|
2022-07-05 22:55:31 +08:00
|
|
|
v = cases.Title(language.English, cases.NoLower).String(v)
|
2020-12-25 01:22:48 +08:00
|
|
|
req.Which[k] = v
|
|
|
|
|
|
|
|
if v == "Password" {
|
2019-01-06 06:44:33 +08: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-25 01:22:48 +08:00
|
|
|
for _, f := range NonModifiableFieldsForNonAdmin {
|
|
|
|
if !d.user.Perm.Admin && v == f {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.store.Users.Update(req.Data, req.Which...)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
})
|