Files
filebrowser/backend/internal/state/quick_setup.go
T
+5 8f2e8add9a Beta/v2.1.1 (#3109)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: spatiumstas <79056064+spatiumstas@users.noreply.github.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Andreas Fehn <fehnomenal@fehn.systems>
Co-authored-by: Matt Van Horn <mvanhorn@users.noreply.github.com>
Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: Takahiro Shimizu <anatofuz@gmail.com>
Co-authored-by: Graham Steffaniak <gtsteffaniak@users.noreply.github.com>
Co-authored-by: Joel Town Road <13539685+hippi345@users.noreply.github.com>
Co-authored-by: beemines <3083452174@qq.com>
Co-authored-by: Bee mines <Beemines82@gmail.com>
Co-authored-by: dweebzxx <134675545+dweebzxx@users.noreply.github.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
2026-10-09 19:18:51 -05:00

91 lines
2.4 KiB
Go

package state
import (
"fmt"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/internal/utils"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
"github.com/gtsteffaniak/go-logger/logger"
)
// QuickSetup creates the initial admin user on first run.
func QuickSetup() error {
if settings.Env.IsPlaywright || settings.Env.IsDevMode {
settings.Env.IsFirstLoad = false
} else {
settings.Env.IsFirstLoad = true
}
passwordAuth := settings.Config.Auth.Methods.PasswordAuth.Enabled
noAuth := settings.Config.Auth.Methods.NoAuth
if !passwordAuth && !noAuth {
return nil
}
user := &users.User{}
ApplyUserDefaults(user)
user.Username = settings.PasswordAdminUsername()
user.Permissions.Admin = true
user.LoginMethod = users.LoginMethodPassword
user.BackendScopes = []users.BackendScope{}
for _, val := range settings.Config.Server.Sources {
user.BackendScopes = append(user.BackendScopes, users.BackendScope{
Path: val.Path,
Scope: "/",
})
}
user.LockPassword = false
user.Permissions = settings.AdminPerms()
adminPerms := settings.AdminSourceFilePermissions()
for i := range user.BackendScopes {
user.BackendScopes[i].Permissions = adminPerms
}
users.SyncBackendSourcePermissionsMap(user)
user.Version = users.CurrentUserMigrationVersion
user.ShowFirstLogin = settings.Env.IsFirstLoad && user.Permissions.Admin
logger.Debugf("Creating user as admin: %v", user.Username)
plainPassword, generated, err := bootstrapDefaultAdminPassword(user.Username)
if err != nil {
return err
}
if generated {
user.RequirePasswordChange = true
}
hashedPassword, hashErr := utils.HashPwd(plainPassword)
if hashErr != nil {
return fmt.Errorf("failed to hash admin password: %w", hashErr)
}
user.Password = hashedPassword
nid, err := utils.RandomUint64ID()
if err != nil {
return fmt.Errorf("failed to allocate admin user id: %w", err)
}
user.ID = nid
if err := sqlDb.CreateUser(user); err != nil {
return fmt.Errorf("failed to create admin user: %w", err)
}
logger.Debug("quickSetup created admin user",
"username", user.Username,
"userID", user.ID,
"permAdmin", user.Permissions.Admin,
"permShare", user.Permissions.Share,
"permModify", user.Permissions.Modify,
"permCreate", user.Permissions.Create,
"permDelete", user.Permissions.Delete,
"permDownload", user.Permissions.Download,
"permApi", user.Permissions.Api,
)
return nil
}