2022-09-28 09:01:17 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-09-30 19:15:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/customize"
|
2022-10-19 21:19:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetSettings returns the user app settings as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/settings
|
|
|
|
func GetSettings(router *gin.RouterGroup) {
|
|
|
|
router.GET("/settings", func(c *gin.Context) {
|
|
|
|
s := AuthAny(c, acl.ResourceSettings, acl.Permissions{acl.AccessAll, acl.AccessOwn})
|
|
|
|
|
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
settings := get.Config().SessionSettings(s)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
if settings == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, settings)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSettings saved the user app settings.
|
|
|
|
//
|
|
|
|
// POST /api/v1/settings
|
|
|
|
func SaveSettings(router *gin.RouterGroup) {
|
|
|
|
router.POST("/settings", func(c *gin.Context) {
|
2022-10-02 11:38:30 +02:00
|
|
|
s := AuthAny(c, acl.ResourceSettings, acl.Permissions{acl.ActionView, acl.ActionUpdate, acl.ActionManage})
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Settings disabled?
|
2022-09-28 09:01:17 +02:00
|
|
|
if conf.DisableSettings() {
|
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var settings *customize.Settings
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
// Only super admins can change global config defaults.
|
|
|
|
if s.User().IsSuperAdmin() {
|
2022-09-28 09:01:17 +02:00
|
|
|
settings = conf.Settings()
|
|
|
|
|
|
|
|
if err := c.BindJSON(settings); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := settings.Save(conf.SettingsYaml()); err != nil {
|
|
|
|
log.Debugf("config: %s (save app settings)", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Flush session cache and update client config.
|
|
|
|
entity.FlushSessionCache()
|
2022-09-28 09:01:17 +02:00
|
|
|
UpdateClientConfig()
|
|
|
|
} else {
|
2022-10-02 11:38:30 +02:00
|
|
|
// Apply to user preferences and keep current values if unspecified.
|
2022-09-28 09:01:17 +02:00
|
|
|
user := s.User()
|
|
|
|
|
|
|
|
if user == nil {
|
2024-01-16 14:36:08 +01:00
|
|
|
AbortUnexpectedError(c)
|
2022-09-28 09:01:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
settings = &customize.Settings{}
|
|
|
|
|
|
|
|
if err := c.BindJSON(settings); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if acl.Resources.DenyAll(acl.ResourceSettings, s.User().AclRole(), acl.Permissions{acl.ActionUpdate, acl.ActionManage}) {
|
|
|
|
c.JSON(http.StatusOK, user.Settings().Apply(settings).ApplyTo(conf.Settings().ApplyACL(acl.Resources, user.AclRole())))
|
|
|
|
return
|
|
|
|
} else if err := user.Settings().Apply(settings).Save(); err != nil {
|
2022-09-28 09:01:17 +02:00
|
|
|
log.Debugf("config: %s (save user settings)", err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Return updated user settings.
|
2022-10-15 21:54:11 +02:00
|
|
|
c.JSON(http.StatusOK, get.Config().SessionSettings(s))
|
2022-09-28 09:01:17 +02:00
|
|
|
})
|
|
|
|
}
|