forked from upstream/filebrowser
+10








3c02eedca6
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>
92 lines
2.5 KiB
Go
92 lines
2.5 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
|
|
}
|
|
|
|
settings.Config.Auth.Key = utils.GenerateKey()
|
|
|
|
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.Config.Auth.AdminUsername
|
|
if user.Username == "" {
|
|
user.Username = "admin"
|
|
}
|
|
|
|
if settings.Config.Auth.AdminPassword == "" {
|
|
settings.Config.Auth.AdminPassword = "admin"
|
|
}
|
|
|
|
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)
|
|
|
|
hashedPassword, hashErr := utils.HashPwd(settings.Config.Auth.AdminPassword)
|
|
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
|
|
}
|