2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
limit := 100
|
|
|
|
offset := 0
|
2020-06-07 21:30:18 +00:00
|
|
|
order := dataprovider.OrderASC
|
2019-07-20 10:26:52 +00:00
|
|
|
username := ""
|
|
|
|
var err error
|
|
|
|
if _, ok := r.URL.Query()["limit"]; ok {
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("limit"))
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid limit")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > 500 {
|
|
|
|
limit = 500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["offset"]; ok {
|
|
|
|
offset, err = strconv.Atoi(r.URL.Query().Get("offset"))
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid offset")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["order"]; ok {
|
|
|
|
order = r.URL.Query().Get("order")
|
2020-06-07 21:30:18 +00:00
|
|
|
if order != dataprovider.OrderASC && order != dataprovider.OrderDESC {
|
2019-07-20 10:26:52 +00:00
|
|
|
err = errors.New("Invalid order")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["username"]; ok {
|
|
|
|
username = r.URL.Query().Get("username")
|
|
|
|
}
|
|
|
|
users, err := dataprovider.GetUsers(dataProvider, limit, offset, order, username)
|
|
|
|
if err == nil {
|
|
|
|
render.JSON(w, r, users)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserByID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.GetUserByID(dataProvider, userID)
|
|
|
|
if err == nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
render.JSON(w, r, dataprovider.HideUserSensitiveData(&user))
|
2019-08-12 16:31:31 +00:00
|
|
|
} else if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2019-07-20 10:26:52 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUser(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
var user dataprovider.User
|
|
|
|
err := render.DecodeJSON(r.Body, &user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = dataprovider.AddUser(dataProvider, user)
|
|
|
|
if err == nil {
|
|
|
|
user, err = dataprovider.UserExists(dataProvider, user.Username)
|
|
|
|
if err == nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
render.JSON(w, r, dataprovider.HideUserSensitiveData(&user))
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUser(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.GetUserByID(dataProvider, userID)
|
2020-01-31 18:04:00 +00:00
|
|
|
currentPermissions := user.Permissions
|
2020-03-01 21:10:29 +00:00
|
|
|
currentFileExtensions := user.Filters.FileExtensions
|
2020-01-31 18:04:00 +00:00
|
|
|
currentS3AccessSecret := ""
|
2020-01-19 06:41:05 +00:00
|
|
|
if user.FsConfig.Provider == 1 {
|
2020-01-31 18:04:00 +00:00
|
|
|
currentS3AccessSecret = user.FsConfig.S3Config.AccessSecret
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = make(map[string][]string)
|
2020-03-01 21:10:29 +00:00
|
|
|
user.Filters.FileExtensions = []dataprovider.ExtensionsFilter{}
|
2019-08-12 16:31:31 +00:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2019-07-20 10:26:52 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = render.DecodeJSON(r.Body, &user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
// we use new Permissions if passed otherwise the old ones
|
|
|
|
if len(user.Permissions) == 0 {
|
2020-01-31 18:04:00 +00:00
|
|
|
user.Permissions = currentPermissions
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-03-01 21:10:29 +00:00
|
|
|
// we use new file extensions if passed otherwise the old ones
|
|
|
|
if len(user.Filters.FileExtensions) == 0 {
|
|
|
|
user.Filters.FileExtensions = currentFileExtensions
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
// we use the new access secret if different from the old one and not empty
|
|
|
|
if user.FsConfig.Provider == 1 {
|
2020-01-31 18:04:00 +00:00
|
|
|
if utils.RemoveDecryptionKey(currentS3AccessSecret) == user.FsConfig.S3Config.AccessSecret ||
|
2020-02-16 09:14:44 +00:00
|
|
|
(len(user.FsConfig.S3Config.AccessSecret) == 0 && len(user.FsConfig.S3Config.AccessKey) > 0) {
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = currentS3AccessSecret
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
if user.ID != userID {
|
|
|
|
sendAPIResponse(w, r, err, "user ID in request body does not match user ID in path parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = dataprovider.UpdateUser(dataProvider, user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "User updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.GetUserByID(dataProvider, userID)
|
2019-08-12 16:31:31 +00:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
2019-07-20 10:26:52 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = dataprovider.DeleteUser(dataProvider, user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "User deleted", http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|