2017-05-23 18:56:10 +00:00
|
|
|
package handler
|
2016-12-18 05:21:29 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-01 08:14:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
2016-12-18 05:21:29 +00:00
|
|
|
"github.com/portainer/portainer"
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror "github.com/portainer/portainer/http/error"
|
|
|
|
"github.com/portainer/portainer/http/security"
|
2016-12-18 05:21:29 +00:00
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
// SettingsHandler represents an HTTP API handler for managing Settings.
|
2016-12-18 05:21:29 +00:00
|
|
|
type SettingsHandler struct {
|
|
|
|
*mux.Router
|
2017-06-01 08:14:55 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
SettingsService portainer.SettingsService
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
// NewSettingsHandler returns a new instance of OldSettingsHandler.
|
|
|
|
func NewSettingsHandler(bouncer *security.RequestBouncer) *SettingsHandler {
|
2016-12-18 05:21:29 +00:00
|
|
|
h := &SettingsHandler{
|
2017-06-01 08:14:55 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
h.Handle("/settings",
|
2017-06-01 08:14:55 +00:00
|
|
|
bouncer.PublicAccess(http.HandlerFunc(h.handleGetSettings))).Methods(http.MethodGet)
|
|
|
|
h.Handle("/settings",
|
|
|
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handlePutSettings))).Methods(http.MethodPut)
|
2017-03-12 16:24:15 +00:00
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetSettings handles GET requests on /settings
|
|
|
|
func (handler *SettingsHandler) handleGetSettings(w http.ResponseWriter, r *http.Request) {
|
2017-06-01 08:14:55 +00:00
|
|
|
settings, err := handler.SettingsService.Settings()
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, settings, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePutSettings handles PUT requests on /settings
|
|
|
|
func (handler *SettingsHandler) handlePutSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req putSettingsRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
httperror.WriteErrorResponse(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
_, err := govalidator.ValidateStruct(req)
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteErrorResponse(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := &portainer.Settings{
|
|
|
|
TemplatesURL: req.TemplatesURL,
|
|
|
|
LogoURL: req.LogoURL,
|
|
|
|
BlackListedLabels: req.BlackListedLabels,
|
|
|
|
DisplayExternalContributors: req.DisplayExternalContributors,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.SettingsService.StoreSettings(settings)
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type putSettingsRequest struct {
|
|
|
|
TemplatesURL string `valid:"required"`
|
|
|
|
LogoURL string `valid:""`
|
|
|
|
BlackListedLabels []portainer.Pair `valid:""`
|
|
|
|
DisplayExternalContributors bool `valid:""`
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|