photoprism/internal/config/settings.go
Michael Mayer f5a8c5a45d Auth: Session and ACL enhancements #98 #1746
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-28 09:01:17 +02:00

107 lines
2.6 KiB
Go

package config
import (
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/customize"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/i18n"
)
// initSettings initializes user settings from a config file.
func (c *Config) initSettings() {
if c.settings != nil {
return
}
// Create settings struct.
c.settings = customize.NewSettings(c.DefaultTheme(), c.DefaultLocale())
// Get YAML file name.
fileName := c.SettingsYaml()
// 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)
}
i18n.SetDir(c.LocalesPath())
c.settings.Propagate()
}
// Settings returns the global app settings.
func (c *Config) Settings() *customize.Settings {
c.initSettings()
if c.DisablePlaces() {
c.settings.Features.Places = false
}
if c.DisableSettings() {
c.settings.Features.Settings = false
}
if c.DisableFaces() {
c.settings.Features.People = false
}
if c.ReadOnly() {
c.settings.Features.Upload = false
c.settings.Features.Import = false
}
if c.Public() && !c.Demo() {
c.settings.Features.Account = false
c.settings.Features.Advanced = false
}
return c.settings
}
// 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()
}
user := sess.User()
// Return public settings if the session does not have a user.
if user == nil {
return c.PublicSettings()
}
// 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()))
}
// PublicSettings returns the public app settings.
func (c *Config) PublicSettings() *customize.Settings {
settings := c.Settings()
return &customize.Settings{
UI: settings.UI,
Search: settings.Search,
Maps: settings.Maps,
Features: settings.Features,
Share: settings.Share,
}
}
// ShareSettings returns the app settings for share link visitors.
func (c *Config) ShareSettings() *customize.Settings {
settings := c.Settings().ApplyACL(acl.Resources, acl.RoleVisitor)
return &customize.Settings{
UI: settings.UI,
Search: settings.Search,
Maps: settings.Maps,
Features: settings.Features,
Share: settings.Share,
}
}