Translate exceptions message for signup and create user
This commit is contained in:
@@ -7,7 +7,8 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -15,53 +16,53 @@ import (
|
||||
)
|
||||
|
||||
// this method will hash the password
|
||||
func CreateUser(db *gorm.DB, username string, password string) error {
|
||||
func CreateUser(ac appcontext.AppContext, username string, password string) error {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return CreateUserWithHashedPassword(db, username, string(hashedPassword))
|
||||
return CreateUserWithHashedPassword(ac, username, string(hashedPassword))
|
||||
}
|
||||
|
||||
// only call this method with hashed password
|
||||
func CreateUserWithHashedPassword(db *gorm.DB, username string, hashedPassword string) error {
|
||||
func CreateUserWithHashedPassword(ac appcontext.AppContext, username string, hashedPassword string) error {
|
||||
var existingUser model.User
|
||||
err := db.Where("name = ?", username).First(&existingUser).Error
|
||||
err := ac.Db.Where("name = ?", username).First(&existingUser).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
if err == nil {
|
||||
return myvalidator.HttpError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Err: errors.New("An user with this name already exists."),
|
||||
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "UserAlreadyExists")),
|
||||
}
|
||||
}
|
||||
user := model.User{
|
||||
Name: username,
|
||||
Password: hashedPassword,
|
||||
}
|
||||
return db.Model(&model.User{}).Save(&user).Error
|
||||
return ac.Db.Model(&model.User{}).Save(&user).Error
|
||||
}
|
||||
|
||||
func CreateDefaultUsers(db *gorm.DB, config *config.Config) error {
|
||||
func CreateDefaultUsers(ac appcontext.AppContext) error {
|
||||
usersPasswordMap := make(map[string]string)
|
||||
var usernames []string
|
||||
for _, s := range config.AddUser {
|
||||
for _, s := range ac.Config.AddUser {
|
||||
splittedString := strings.Split(s, ":")
|
||||
if len(splittedString) < 2 {
|
||||
return fmt.Errorf("Error when creating user from string %s", s)
|
||||
return fmt.Errorf(i18nresource.GetTranslatedMessage(&ac, "ErrorWhenCreatingUserFromStr"), s)
|
||||
}
|
||||
usernames = append(usernames, splittedString[0])
|
||||
usersPasswordMap[splittedString[0]] = splittedString[1]
|
||||
}
|
||||
if !slices.Contains(usernames, config.DemoUsername) {
|
||||
usernames = append(usernames, config.DemoUsername)
|
||||
usersPasswordMap[config.DemoUsername] = ""
|
||||
if !slices.Contains(usernames, ac.Config.DemoUsername) {
|
||||
usernames = append(usernames, ac.Config.DemoUsername)
|
||||
usersPasswordMap[ac.Config.DemoUsername] = ""
|
||||
|
||||
}
|
||||
|
||||
var existingUsers []model.User
|
||||
err := db.Where("name IN ?", usernames).Find(&existingUsers).Error
|
||||
err := ac.Db.Where("name IN ?", usernames).Find(&existingUsers).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -70,7 +71,7 @@ func CreateDefaultUsers(db *gorm.DB, config *config.Config) error {
|
||||
if isInExistingUsers(username, existingUsers) {
|
||||
continue
|
||||
}
|
||||
err = CreateUserWithHashedPassword(db, username, usersPasswordMap[username])
|
||||
err = CreateUserWithHashedPassword(ac, username, usersPasswordMap[username])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user