2022-07-06 23:19:05 +02:00
|
|
|
package apis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-21 13:30:36 +02:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2022-07-06 23:19:05 +02:00
|
|
|
"github.com/labstack/echo/v5"
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
|
|
|
"github.com/pocketbase/pocketbase/forms"
|
2023-01-15 17:00:28 +02:00
|
|
|
"github.com/pocketbase/pocketbase/models/settings"
|
2022-07-06 23:19:05 +02:00
|
|
|
)
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
// bindSettingsApi registers the settings api endpoints.
|
|
|
|
func bindSettingsApi(app core.App, rg *echo.Group) {
|
2022-07-06 23:19:05 +02:00
|
|
|
api := settingsApi{app: app}
|
|
|
|
|
|
|
|
subGroup := rg.Group("/settings", ActivityLogger(app), RequireAdminAuth())
|
|
|
|
subGroup.GET("", api.list)
|
|
|
|
subGroup.PATCH("", api.set)
|
2022-08-21 13:30:36 +02:00
|
|
|
subGroup.POST("/test/s3", api.testS3)
|
|
|
|
subGroup.POST("/test/email", api.testEmail)
|
2023-03-01 23:29:45 +02:00
|
|
|
subGroup.POST("/apple/generate-client-secret", api.generateAppleClientSecret)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type settingsApi struct {
|
|
|
|
app core.App
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *settingsApi) list(c echo.Context) error {
|
|
|
|
settings, err := api.app.Settings().RedactClone()
|
|
|
|
if err != nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 22:19:08 +02:00
|
|
|
event := new(core.SettingsListEvent)
|
|
|
|
event.HttpContext = c
|
|
|
|
event.RedactedSettings = settings
|
2022-07-06 23:19:05 +02:00
|
|
|
|
|
|
|
return api.app.OnSettingsListRequest().Trigger(event, func(e *core.SettingsListEvent) error {
|
2023-07-20 09:40:03 +02:00
|
|
|
if e.HttpContext.Response().Committed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
return e.HttpContext.JSON(http.StatusOK, e.RedactedSettings)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *settingsApi) set(c echo.Context) error {
|
|
|
|
form := forms.NewSettingsUpsert(api.app)
|
2022-07-12 12:42:06 +02:00
|
|
|
|
|
|
|
// load request
|
2022-07-06 23:19:05 +02:00
|
|
|
if err := c.Bind(form); err != nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("An error occurred while loading the submitted data.", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 22:19:08 +02:00
|
|
|
event := new(core.SettingsUpdateEvent)
|
|
|
|
event.HttpContext = c
|
|
|
|
event.OldSettings = api.app.Settings()
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
// update the settings
|
2023-05-29 20:50:07 +02:00
|
|
|
return form.Submit(func(next forms.InterceptorNextFunc[*settings.Settings]) forms.InterceptorNextFunc[*settings.Settings] {
|
2023-01-15 17:00:28 +02:00
|
|
|
return func(s *settings.Settings) error {
|
|
|
|
event.NewSettings = s
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
return api.app.OnSettingsBeforeUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
|
2023-01-15 17:00:28 +02:00
|
|
|
if err := next(e.NewSettings); err != nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("An error occurred while submitting the form.", err)
|
2022-07-12 12:42:06 +02:00
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2023-07-18 14:31:36 +02:00
|
|
|
return api.app.OnSettingsAfterUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
|
2023-07-20 09:40:03 +02:00
|
|
|
if e.HttpContext.Response().Committed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:31:36 +02:00
|
|
|
redactedSettings, err := api.app.Settings().RedactClone()
|
|
|
|
if err != nil {
|
|
|
|
return NewBadRequestError("", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.HttpContext.JSON(http.StatusOK, redactedSettings)
|
|
|
|
})
|
2022-07-12 12:42:06 +02:00
|
|
|
})
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
}
|
2022-08-21 13:30:36 +02:00
|
|
|
|
|
|
|
func (api *settingsApi) testS3(c echo.Context) error {
|
2023-05-13 21:10:14 +02:00
|
|
|
form := forms.NewTestS3Filesystem(api.app)
|
2022-08-21 13:30:36 +02:00
|
|
|
|
2023-05-13 21:10:14 +02:00
|
|
|
// load request
|
|
|
|
if err := c.Bind(form); err != nil {
|
|
|
|
return NewBadRequestError("An error occurred while loading the submitted data.", err)
|
2022-08-21 13:30:36 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 21:10:14 +02:00
|
|
|
// send
|
|
|
|
if err := form.Submit(); err != nil {
|
|
|
|
// form error
|
|
|
|
if fErr, ok := err.(validation.Errors); ok {
|
|
|
|
return NewBadRequestError("Failed to test the S3 filesystem.", fErr)
|
|
|
|
}
|
2022-08-21 13:30:36 +02:00
|
|
|
|
2023-05-13 21:10:14 +02:00
|
|
|
// mailer error
|
|
|
|
return NewBadRequestError("Failed to test the S3 filesystem. Raw error: \n"+err.Error(), nil)
|
2022-08-21 13:30:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *settingsApi) testEmail(c echo.Context) error {
|
|
|
|
form := forms.NewTestEmailSend(api.app)
|
|
|
|
|
|
|
|
// load request
|
|
|
|
if err := c.Bind(form); err != nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("An error occurred while loading the submitted data.", err)
|
2022-08-21 13:30:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send
|
|
|
|
if err := form.Submit(); err != nil {
|
2023-03-01 23:29:45 +02:00
|
|
|
// form error
|
2022-08-21 13:30:36 +02:00
|
|
|
if fErr, ok := err.(validation.Errors); ok {
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("Failed to send the test email.", fErr)
|
2022-08-21 13:30:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// mailer error
|
2022-10-30 10:28:14 +02:00
|
|
|
return NewBadRequestError("Failed to send the test email. Raw error: \n"+err.Error(), nil)
|
2022-08-21 13:30:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
|
|
}
|
2023-03-01 23:29:45 +02:00
|
|
|
|
|
|
|
func (api *settingsApi) generateAppleClientSecret(c echo.Context) error {
|
|
|
|
form := forms.NewAppleClientSecretCreate(api.app)
|
|
|
|
|
|
|
|
// load request
|
|
|
|
if err := c.Bind(form); err != nil {
|
|
|
|
return NewBadRequestError("An error occurred while loading the submitted data.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate
|
|
|
|
secret, err := form.Submit()
|
|
|
|
if err != nil {
|
|
|
|
// form error
|
|
|
|
if fErr, ok := err.(validation.Errors); ok {
|
|
|
|
return NewBadRequestError("Invalid client secret data.", fErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// secret generation error
|
|
|
|
return NewBadRequestError("Failed to generate client secret. Raw error: \n"+err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, map[string]any{
|
|
|
|
"secret": secret,
|
|
|
|
})
|
|
|
|
}
|