Files
Pikachu Ren 2d51c9ab4b feat!(init): add initialization wizard (#3041)
feat: add system initialization (setup wizard) support

Co-authored-by: PIKACHUIM <PIKACHUIM@users.noreply.github.com>
2026-09-07 14:14:22 +08:00

85 lines
2.3 KiB
Go

package data
import (
"fmt"
"os"
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
"github.com/OpenListTeam/OpenList/v4/internal/db"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
"github.com/OpenListTeam/OpenList/v4/pkg/utils/random"
"github.com/pkg/errors"
"gorm.io/gorm"
)
func initUser() {
initAdmin()
initGuest()
}
func initAdmin() {
_, err := op.GetAdmin()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 系统尚未初始化:仅在开发模式或显式配置 OPENLIST_ADMIN_PASSWORD 时
// 自动创建管理员;否则交由 Web 安装向导(POST /api/public/init/setup)完成。
adminPassword := "admin"
envpass := os.Getenv("OPENLIST_ADMIN_PASSWORD")
if flags.Dev {
adminPassword = "admin"
} else if len(envpass) > 0 {
adminPassword = envpass
} else {
// 未初始化:不自动创建管理员,等待 Web 安装向导。
return
}
salt := random.String(16)
admin := &model.User{
Username: "admin",
Salt: salt,
PwdHash: model.TwoHashPwd(adminPassword, salt),
Role: model.ADMIN,
BasePath: "/",
Authn: "[]",
// 0(can see hidden) - 8(webdav read) & 12(can read archives) - 14(can share)
Permission: 0x71FF,
}
if err := op.CreateUser(admin); err != nil {
panic(err)
} else {
// DO NOT output the password to log file. Only output to console.
// utils.Log.Infof("Successfully created the admin user and the initial password is: %s", adminPassword)
fmt.Printf("Successfully created the admin user and the initial password is: %s\n", adminPassword)
}
} else {
utils.Log.Fatalf("[init user] Failed to get admin user: %v", err)
}
}
}
func initGuest() {
_, err := op.GetGuest()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
salt := random.String(16)
guest := &model.User{
Username: "guest",
PwdHash: model.TwoHashPwd("guest", salt),
Salt: salt,
Role: model.GUEST,
BasePath: "/",
Permission: 0,
Disabled: true,
Authn: "[]",
}
if err := db.CreateUser(guest); err != nil {
utils.Log.Fatalf("[init user] Failed to create guest user: %v", err)
}
} else {
utils.Log.Fatalf("[init user] Failed to get guest user: %v", err)
}
}
}