Files
filebrowser/backend/pkg/settings/user_enforcement_fields.go
T
+10 3c02eedca6 Beta/v2.0.0 (#2746)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Gabriel Górski <me@glaeqen.eu>
Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com>
Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com>
Co-authored-by: carloda <35763669+carloda@users.noreply.github.com>
Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com>
Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com>
Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com>
Co-authored-by: Juan Soler <juansoler@gmail.com>
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net>
Co-authored-by: Neil <kylelzzz02@icloud.com>
Co-authored-by: Graham <info@quantumx-apps.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:24:38 -05:00

149 lines
5.2 KiB
Go

package settings
import (
"strings"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
// userJSONFieldEnforcementPaths maps flat user JSON field names (PATCH which / data keys)
// to user-defaults enforcement dot-paths (see UserDefaultsEnforcement).
var userJSONFieldEnforcementPaths = map[string]string{
"disableQuickToggles": "sidebar.disableQuickToggles",
"hideSidebarFileActions": "sidebar.hideFileActions",
"stickySidebar": "sidebar.sticky",
"hideFilesInTree": "sidebar.hideFiles",
"showToolsInSidebar": "sidebar.showTools",
"deleteWithoutConfirming": "listing.deleteWithoutConfirming",
"dateFormat": "listing.dateFormat",
"showHidden": "listing.showHidden",
"quickDownload": "listing.quickDownload",
"showSelectMultiple": "listing.showSelectMultiple",
"singleClick": "listing.singleClick",
"hideFileExt": "listing.hideFileExt",
"showCopyPath": "listing.showCopyPath",
"deleteAfterArchive": "listing.deleteAfterArchive",
"viewMode": "listing.viewMode",
"gallerySize": "listing.gallerySize",
"disablePreviewExt": "preview.disablePreviewExt",
"disableSearchOptions": "search.disableOptions",
"editorQuickSave": "fileViewer.editorQuickSave",
"preferEditorForMarkdown": "fileViewer.preferEditorForMarkdown",
"debugOffice": "fileViewer.debugOffice",
"disableViewingExt": "fileViewer.disableViewingExt",
"disableOnlyOfficeExt": "fileViewer.disableOnlyOfficeExt",
"disableOfficePreviewExt": "fileViewer.disableOnlyOfficeExt",
"darkMode": "ui.darkMode",
"themeColor": "ui.themeColor",
"customTheme": "ui.customTheme",
"locale": "ui.locale",
"lockPassword": "account.lockPassword",
"disableSettings": "account.disableSettings",
"disableUpdateNotifications": "account.disableUpdateNotifications",
}
var previewJSONSubfields = map[string]string{
"image": "preview.image",
"video": "preview.video",
"audio": "preview.audio",
"motionVideoPreview": "preview.motionVideoPreview",
"office": "preview.office",
"popup": "preview.popup",
"folder": "preview.folder",
"models": "preview.models",
"autoplayMedia": "fileViewer.autoplayMedia",
"disableHideSidebar": "sidebar.disableHideOnPreview",
}
var fileLoadingJSONSubfields = map[string]string{
"maxConcurrentUpload": "fileLoading.maxConcurrentUpload",
"uploadChunkSizeMb": "fileLoading.uploadChunkSizeMb",
"downloadChunkSizeMb": "fileLoading.downloadChunkSizeMb",
}
// EnforcedPathsTouchedByUserJSONField returns enforcement paths a user JSON field name may affect.
func EnforcedPathsTouchedByUserJSONField(jsonField string) []string {
jsonField = strings.TrimSpace(jsonField)
if jsonField == "" {
return nil
}
if path, ok := userJSONFieldEnforcementPaths[jsonField]; ok {
return []string{path}
}
switch jsonField {
case "preview":
out := make([]string, 0, len(previewJSONSubfields))
for _, p := range previewJSONSubfields {
out = append(out, p)
}
return out
case "fileLoading":
out := make([]string, 0, len(fileLoadingJSONSubfields))
for _, p := range fileLoadingJSONSubfields {
out = append(out, p)
}
return out
case "permissions", "Permissions":
return []string{
"account.permissions.admin",
"account.permissions.api",
"account.permissions.share",
"account.permissions.realtime",
}
default:
if path, ok := previewJSONSubfields[jsonField]; ok {
return []string{path}
}
if path, ok := fileLoadingJSONSubfields[jsonField]; ok {
return []string{path}
}
}
return nil
}
// ErrEnforcedUserField is returned when a self-service user update touches an enforced default.
type ErrEnforcedUserField struct {
Field string
Path string
}
func (e ErrEnforcedUserField) Error() string {
if e.Field != "" {
return "setting is enforced by administrator: " + e.Field
}
return "setting is enforced by administrator: " + e.Path
}
// ValidateSelfUserUpdateNotEnforced rejects PATCH fields that are locked by user-defaults enforcement.
func ValidateSelfUserUpdateNotEnforced(which []string, enforced UserDefaultsEnforcement, u *users.User) error {
if !EnforcementAppliesToUser(u) {
return nil
}
paths := EnforcedPathSet(enforced)
if len(paths) == 0 {
return nil
}
for _, jsonField := range which {
jsonField = strings.TrimSpace(jsonField)
if jsonField == "" || strings.EqualFold(jsonField, "password") {
continue
}
for _, path := range EnforcedPathsTouchedByUserJSONField(jsonField) {
if _, ok := paths[path]; ok {
return ErrEnforcedUserField{Field: jsonField, Path: path}
}
}
}
return nil
}
// UserJSONFieldsForEnforcedSync lists top-level user JSON keys that enforced-default sync may modify.
func UserJSONFieldsForEnforcedSync() []string {
out := make([]string, 0, len(userJSONFieldEnforcementPaths)+3)
for k := range userJSONFieldEnforcementPaths {
out = append(out, k)
}
out = append(out, "preview", "fileLoading", "permissions")
return out
}