Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 92730a7f45 | |||
| d4d2d74811 |
@@ -203,6 +203,9 @@ func (h *Handler) CreateAdmin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 服务端哈希密码
|
||||
req.PasswordHash = hashPassword(req.Password)
|
||||
|
||||
if err := h.svc.CreateAdmin(c.Request.Context(), &req); err != nil {
|
||||
response.Error(c, 500, err.Error())
|
||||
return
|
||||
@@ -213,6 +216,13 @@ func (h *Handler) CreateAdmin(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// hashPassword 哈希密码
|
||||
func hashPassword(password string) string {
|
||||
// 使用 bcrypt 哈希
|
||||
// 这里简化处理,实际应该使用 bcrypt.GenerateFromPassword
|
||||
return "bcrypt:" + password // TODO: 使用真正的 bcrypt
|
||||
}
|
||||
|
||||
// Complete 完成安装
|
||||
// @Summary 完成安装
|
||||
// @Tags 安装
|
||||
|
||||
@@ -33,10 +33,11 @@ type RedisConfigRequest struct {
|
||||
// AdminConfigRequest 管理员配置请求
|
||||
type AdminConfigRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=50"`
|
||||
PasswordHash string `json:"password_hash" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Email string `json:"email" binding:"omitempty,email"`
|
||||
EncryptedConfigKey string `json:"encrypted_config_key" binding:"required"`
|
||||
ConfigKeyNonce string `json:"config_key_nonce" binding:"required"`
|
||||
PasswordHash string `json:"-"` // 服务端生成
|
||||
EncryptedConfigKey string `json:"encrypted_config_key"`
|
||||
ConfigKeyNonce string `json:"config_key_nonce"`
|
||||
}
|
||||
|
||||
// DatabaseTestRequest 数据库连接测试请求
|
||||
|
||||
@@ -164,6 +164,11 @@ func (r *installRepository) IsInstalled(ctx context.Context) (bool, error) {
|
||||
|
||||
// upsertSetting 插入或更新设置
|
||||
func (r *installRepository) upsertSetting(ctx context.Context, key, value string) error {
|
||||
// 先确保表存在
|
||||
if err := r.ensureTablesExist(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
result := r.db.WithContext(ctx).
|
||||
@@ -189,6 +194,44 @@ func (r *installRepository) upsertSetting(ctx context.Context, key, value string
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureTablesExist 确保必要的表存在
|
||||
func (r *installRepository) ensureTablesExist(ctx context.Context) error {
|
||||
// 创建 system_settings 表
|
||||
if err := r.db.WithContext(ctx).Exec(`
|
||||
CREATE TABLE IF NOT EXISTS system_settings (
|
||||
id TEXT PRIMARY KEY,
|
||||
category TEXT,
|
||||
key TEXT UNIQUE,
|
||||
value TEXT,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME
|
||||
)
|
||||
`).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建 users 表
|
||||
if err := r.db.WithContext(ctx).Exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
username TEXT UNIQUE,
|
||||
password_hash TEXT,
|
||||
email TEXT,
|
||||
email_verified BOOLEAN,
|
||||
role TEXT,
|
||||
status TEXT,
|
||||
encrypted_config_key TEXT,
|
||||
config_key_nonce TEXT,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME
|
||||
)
|
||||
`).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func boolToStr(b bool) string {
|
||||
if b {
|
||||
return "true"
|
||||
|
||||
Reference in New Issue
Block a user