2019-11-12 04:34:37 +01:00
|
|
|
package config
|
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
import (
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/customize"
|
2022-04-06 17:46:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2019-11-17 03:08:13 +01:00
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// initSettings initializes user settings from a config file.
|
|
|
|
func (c *Config) initSettings() {
|
|
|
|
if c.settings != nil {
|
|
|
|
return
|
2020-03-31 18:56:52 +02:00
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Create settings struct.
|
|
|
|
c.settings = customize.NewSettings(c.DefaultTheme(), c.DefaultLocale())
|
2020-04-13 18:08:21 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Get YAML file name.
|
|
|
|
fileName := c.SettingsYaml()
|
2020-12-07 17:03:06 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Load values from YAML file.
|
|
|
|
if err := c.settings.Load(fileName); err == nil {
|
|
|
|
log.Debugf("settings: loaded from %s", fileName)
|
|
|
|
} else if err := c.settings.Save(fileName); err != nil {
|
|
|
|
log.Errorf("settings: could not create %s (%s)", fileName, err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("settings: saved to %s ", fileName)
|
|
|
|
}
|
2020-12-07 17:03:06 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
i18n.SetDir(c.LocalesPath())
|
|
|
|
|
|
|
|
c.settings.Propagate()
|
2020-12-07 17:03:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Settings returns the global app settings.
|
|
|
|
func (c *Config) Settings() *customize.Settings {
|
|
|
|
c.initSettings()
|
|
|
|
|
|
|
|
if c.DisablePlaces() {
|
|
|
|
c.settings.Features.Places = false
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if c.DisableSettings() {
|
|
|
|
c.settings.Features.Settings = false
|
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if c.DisableFaces() {
|
|
|
|
c.settings.Features.People = false
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if c.ReadOnly() {
|
|
|
|
c.settings.Features.Upload = false
|
|
|
|
c.settings.Features.Import = false
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return c.settings
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// SessionSettings returns the app settings for the specified session.
|
|
|
|
func (c *Config) SessionSettings(sess *entity.Session) *customize.Settings {
|
|
|
|
// Return global app settings if authentication is disabled (public mode).
|
|
|
|
if c.Public() {
|
|
|
|
return c.Settings()
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
user := sess.User()
|
2020-04-13 18:08:21 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Return public settings if the session does not have a user.
|
|
|
|
if user == nil {
|
|
|
|
return c.PublicSettings()
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Apply role-based permissions and user settings to a copy of the global app settings.
|
|
|
|
return user.Settings().ApplyTo(c.Settings().ApplyACL(acl.Resources, user.AclRole()))
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// PublicSettings returns the public app settings.
|
|
|
|
func (c *Config) PublicSettings() *customize.Settings {
|
|
|
|
settings := c.Settings()
|
2020-12-04 12:15:59 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return &customize.Settings{
|
|
|
|
UI: settings.UI,
|
|
|
|
Search: settings.Search,
|
|
|
|
Maps: settings.Maps,
|
|
|
|
Features: settings.Features,
|
|
|
|
Share: settings.Share,
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// ShareSettings returns the app settings for share link visitors.
|
|
|
|
func (c *Config) ShareSettings() *customize.Settings {
|
|
|
|
settings := c.Settings().ApplyACL(acl.Resources, acl.RoleVisitor)
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return &customize.Settings{
|
|
|
|
UI: settings.UI,
|
|
|
|
Search: settings.Search,
|
|
|
|
Maps: settings.Maps,
|
|
|
|
Features: settings.Features,
|
|
|
|
Share: settings.Share,
|
2020-12-18 09:11:42 +01:00
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|