2019-01-05 22:44:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2024-12-17 00:01:55 +00:00
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/settings"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/users"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type settingsData struct {
|
2022-06-03 13:59:36 +00:00
|
|
|
Signup bool `json:"signup"`
|
|
|
|
CreateUserDir bool `json:"createUserDir"`
|
|
|
|
UserHomeBasePath string `json:"userHomeBasePath"`
|
|
|
|
Defaults settings.UserDefaults `json:"defaults"`
|
2024-11-21 00:15:30 +00:00
|
|
|
Rules []users.Rule `json:"rules"`
|
2023-09-01 14:00:02 +00:00
|
|
|
Frontend settings.Frontend `json:"frontend"`
|
2022-06-03 13:59:36 +00:00
|
|
|
Commands map[string][]string `json:"commands"`
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
// settingsGetHandler retrieves the current system settings.
|
|
|
|
// @Summary Get system settings
|
|
|
|
// @Description Returns the current configuration settings for signup, user directories, rules, frontend, and commands.
|
|
|
|
// @Tags Settings
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} settingsData "System settings data"
|
|
|
|
// @Router /api/settings [get]
|
|
|
|
func settingsGetHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
|
2019-01-05 22:44:33 +00:00
|
|
|
data := &settingsData{
|
2024-11-21 00:15:30 +00:00
|
|
|
Signup: config.Auth.Signup,
|
|
|
|
CreateUserDir: config.Server.CreateUserDir,
|
|
|
|
UserHomeBasePath: config.Server.UserHomeBasePath,
|
|
|
|
Defaults: config.UserDefaults,
|
|
|
|
Rules: config.Rules,
|
|
|
|
Frontend: config.Frontend,
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderJSON(w, r, data)
|
2024-11-21 00:15:30 +00:00
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
// settingsPutHandler updates the system settings.
|
|
|
|
// @Summary Update system settings
|
|
|
|
// @Description Updates the system configuration settings for signup, user directories, rules, frontend, and commands.
|
|
|
|
// @Tags Settings
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param body body settingsData true "Settings data to update"
|
|
|
|
// @Success 200 "Settings updated successfully"
|
|
|
|
// @Failure 400 {object} map[string]string "Bad request - failed to decode body"
|
|
|
|
// @Failure 500 {object} map[string]string "Internal server error"
|
|
|
|
// @Router /api/settings [put]
|
|
|
|
func settingsPutHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
|
2019-01-05 22:44:33 +00:00
|
|
|
req := &settingsData{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(req)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
config.Server.CreateUserDir = req.CreateUserDir
|
|
|
|
config.Server.UserHomeBasePath = req.UserHomeBasePath
|
|
|
|
config.UserDefaults = req.Defaults
|
|
|
|
config.Rules = req.Rules
|
|
|
|
config.Frontend = req.Frontend
|
|
|
|
config.Auth.Signup = req.Signup
|
|
|
|
err = store.Settings.Save(config)
|
2019-01-05 22:44:33 +00:00
|
|
|
return errToStatus(err), err
|
2024-11-21 00:15:30 +00:00
|
|
|
}
|