fix bug
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package initdb
|
package initdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"senlinai-agent/backend/internal/models"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"senlinai-agent/backend/internal/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -15,31 +16,27 @@ const (
|
|||||||
// InitUser returns the existing root user or creates and returns it when the user table is empty.
|
// InitUser returns the existing root user or creates and returns it when the user table is empty.
|
||||||
func InitUser(database *gorm.DB) (*models.SaUser, error) {
|
func InitUser(database *gorm.DB) (*models.SaUser, error) {
|
||||||
var count int64
|
var count int64
|
||||||
if err := database.Model(&models.SaUser{}).Count(&count).Error; err != nil {
|
var user models.SaUser
|
||||||
return nil, err
|
database.Model(&models.SaUser{}).Where("email=?", rootUsername).First(&user).Count(&count)
|
||||||
}
|
|
||||||
if count > 0 {
|
if count == 0 {
|
||||||
var user models.SaUser
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(rootPassword), bcrypt.DefaultCost)
|
||||||
if err := database.Where("email = ? AND role = ?", rootUsername, rootRole).
|
if err != nil {
|
||||||
First(&user).Error; err != nil {
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
user = models.SaUser{
|
||||||
|
Email: rootUsername,
|
||||||
|
DisplayName: rootUsername,
|
||||||
|
PasswordHash: string(passwordHash),
|
||||||
|
Role: rootRole,
|
||||||
|
}
|
||||||
|
if err := database.Create(&user).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(rootPassword), bcrypt.DefaultCost)
|
return &user, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user := &models.SaUser{
|
|
||||||
Email: rootUsername,
|
|
||||||
DisplayName: rootUsername,
|
|
||||||
PasswordHash: string(passwordHash),
|
|
||||||
Role: rootRole,
|
|
||||||
}
|
|
||||||
if err := database.Create(user).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user