Files
filebrowser/backend/pkg/settings/settings.go
T
+5 751cc56809 Merge main v2.2.0 v2.1.3 (#3248)
Co-authored-by: Paolo Valletta <11146190+paoValle@users.noreply.github.com>
Co-authored-by: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com>
Co-authored-by: vulragrag-star <320132805+vulragrag-star@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <5327697+anpryl@users.noreply.github.com>
Co-authored-by: Kevin B. <178341547+Kurami32@users.noreply.github.com>
Co-authored-by: Som Samantray <92726151+SomSamantray@users.noreply.github.com>
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: HawkForge <75696486+HawkForge@users.noreply.github.com>
Co-authored-by: JUSHUANGHUI LI <71743926+oodadoudou@users.noreply.github.com>
Co-authored-by: anonymous-writer <35840715+anonymous-writer@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Graham Steffaniak <gtsteffaniak@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 12:55:25 -05:00

218 lines
6.5 KiB
Go

//go:generate go run ./tools/yaml.go -input=pkg/settings/settings.go -output=config.generated.yaml
package settings
import (
"crypto/rand"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
// boolPtr returns a pointer to a bool value
func boolPtr(b bool) *bool {
return &b
}
// boolValueOrDefault returns the value of a bool pointer, or the default if nil
func boolValueOrDefault(ptr *bool, defaultValue bool) bool {
if ptr != nil {
return *ptr
}
return defaultValue
}
// ConvertPermissionsToUsers converts account permission defaults to users.Permissions (global only).
func ConvertPermissionsToUsers(p UserDefaultsAccountPermissions) users.Permissions {
return users.Permissions{
Api: p.Api,
Admin: p.Admin,
Share: p.Share,
Realtime: p.Realtime,
}
}
const DefaultUsersHomeBasePath = "/users"
// AuthMethod describes an authentication method.
type AuthMethod string
// GenerateKey generates a key of 512 bits.
func GenerateKey() ([]byte, error) {
b := make([]byte, 64)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
func GetSettingsConfig(nameType string, Value string) string {
return nameType + Value
}
func AdminPerms() users.Permissions {
return users.Permissions{
Share: true,
Admin: true,
Api: true,
Realtime: false,
}
}
// AdminSourceFilePermissions returns full per-source file permissions for admin users.
func AdminSourceFilePermissions() users.SourceFilePermissions {
return users.SourceFilePermissions{
View: true,
Download: true,
Modify: true,
Delete: true,
Create: true,
}
}
// MergeDefaultEnabledBackendScopes ensures every defaultEnabled source is present in
// BackendScopes. Existing scopes for configured sources are preserved (empty Scope is
// filled from DefaultUserScope). Unknown paths not in Config.Server.Sources are kept
// at the end. defaultEnabled: false sources are never auto-added.
// declined lists defaultEnabled source paths the user explicitly removed via scope edits.
func MergeDefaultEnabledBackendScopes(existing []users.BackendScope, declined []string) []users.BackendScope {
declinedSet := make(map[string]struct{}, len(declined))
for _, path := range declined {
declinedSet[path] = struct{}{}
}
byPath := make(map[string]users.BackendScope, len(existing))
for _, s := range existing {
byPath[s.Path] = s
}
newScopes := make([]users.BackendScope, 0, len(existing)+len(Config.Server.Sources))
seen := make(map[string]struct{}, len(Config.Server.Sources))
for _, src := range Config.Server.Sources {
if src == nil {
continue
}
existingScope, ok := byPath[src.Path]
if ok {
if existingScope.Scope == "" {
existingScope.Scope = src.Config.DefaultUserScope
}
} else if src.Config.DefaultEnabled {
if _, omitted := declinedSet[src.Path]; omitted {
continue
}
existingScope = users.BackendScope{
Scope: src.Config.DefaultUserScope,
}
} else {
continue
}
newScopes = append(newScopes, users.BackendScope{
Path: src.Path,
Scope: existingScope.Scope,
Permissions: existingScope.Permissions,
})
seen[src.Path] = struct{}{}
}
for _, s := range existing {
if _, ok := seen[s.Path]; !ok {
newScopes = append(newScopes, s)
}
}
return newScopes
}
// ComputeDeclinedDefaultSources records defaultEnabled sources intentionally omitted
// from a user's persisted BackendScopes after an explicit scope edit.
func ComputeDeclinedDefaultSources(scopes []users.BackendScope) []string {
present := make(map[string]struct{}, len(scopes))
for _, scope := range scopes {
present[scope.Path] = struct{}{}
}
var declined []string
for _, src := range Config.Server.Sources {
if src == nil || !src.Config.DefaultEnabled {
continue
}
if _, ok := present[src.Path]; !ok {
declined = append(declined, src.Path)
}
}
return declined
}
// ApplyUserDefaults applies Config.UserDefaults to a user (tests and legacy callers).
func ApplyUserDefaults(u *users.User) {
ApplyUserDefaultsFrom(u, Config.UserDefaults)
}
// ApplyUserDefaultsFrom applies the given defaults template to a user.
// Keep this in sync with [UserDefaults]: every user-facing field there should be copied
// here (except DefaultScopes, which is wired through source config rather than this helper).
func ApplyUserDefaultsFrom(u *users.User, d UserDefaults) {
applyUserDefaultsFrom(u, d, false)
}
// ApplyUserDefaultsFromKeepingAccountPermissions applies profile and scope defaults but
// leaves u.Permissions unchanged so default-enabled scope permissions use the caller's
// final admin flag (see CreateUser).
func ApplyUserDefaultsFromKeepingAccountPermissions(u *users.User, d UserDefaults) {
applyUserDefaultsFrom(u, d, true)
}
func applyUserDefaultsFrom(u *users.User, d UserDefaults, keepAccountPermissions bool) {
savedPermissions := u.Permissions
ApplyFullProfileFromDefaults(u, d)
if u.Username == "anonymous" {
return
}
// Global permissions (admin, api, share, realtime). Profile merge overwrites these;
// restore the caller's values when CreateUser already applied requested permissions
// so default-enabled scope permissions use the final admin flag.
if keepAccountPermissions {
u.Permissions = savedPermissions
} else {
u.Permissions.Api = d.Account.Permissions.Api
u.Permissions.Admin = d.Account.Permissions.Admin
u.Permissions.Share = d.Account.Permissions.Share
u.Permissions.Realtime = d.Account.Permissions.Realtime
}
sourceDefaults := DefaultSourceFilePermissions()
u.BackendScopes = MergeDefaultEnabledBackendScopes(u.BackendScopes, nil)
if len(u.SidebarLinks) == 0 && len(u.BackendScopes) > 0 {
scope := u.BackendScopes[0]
u.SidebarLinks = append(u.SidebarLinks, users.SidebarLink{
Category: "source",
Target: "/",
Icon: "",
SourceName: scope.Path,
})
}
if u.BackendSourcePermissions == nil {
u.BackendSourcePermissions = make(map[string]users.SourceFilePermissions)
}
for i, scope := range u.BackendScopes {
if _, ok := u.BackendSourcePermissions[scope.Path]; !ok {
perms := sourceDefaults
if u.Permissions.Admin {
perms = AdminSourceFilePermissions()
}
u.BackendScopes[i].Permissions = perms
u.BackendSourcePermissions[scope.Path] = perms
} else if u.BackendScopes[i].Permissions.IsUnset() {
u.BackendScopes[i].Permissions = u.BackendSourcePermissions[scope.Path]
}
}
if u.LoginMethod == "" && d.Account.LoginMethod != "" {
u.LoginMethod = users.LoginMethod(d.Account.LoginMethod)
}
u.Version = users.CurrentUserMigrationVersion
}