Translate exceptions message for signup and create user
This commit is contained in:
@@ -7,7 +7,8 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"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/model"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -15,53 +16,53 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// this method will hash the password
|
// 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)
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return CreateUserWithHashedPassword(db, username, string(hashedPassword))
|
return CreateUserWithHashedPassword(ac, username, string(hashedPassword))
|
||||||
}
|
}
|
||||||
|
|
||||||
// only call this method with hashed password
|
// 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
|
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) {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return myvalidator.HttpError{
|
return myvalidator.HttpError{
|
||||||
StatusCode: http.StatusInternalServerError,
|
StatusCode: http.StatusInternalServerError,
|
||||||
Err: errors.New("An user with this name already exists."),
|
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "UserAlreadyExists")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
user := model.User{
|
user := model.User{
|
||||||
Name: username,
|
Name: username,
|
||||||
Password: hashedPassword,
|
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)
|
usersPasswordMap := make(map[string]string)
|
||||||
var usernames []string
|
var usernames []string
|
||||||
for _, s := range config.AddUser {
|
for _, s := range ac.Config.AddUser {
|
||||||
splittedString := strings.Split(s, ":")
|
splittedString := strings.Split(s, ":")
|
||||||
if len(splittedString) < 2 {
|
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])
|
usernames = append(usernames, splittedString[0])
|
||||||
usersPasswordMap[splittedString[0]] = splittedString[1]
|
usersPasswordMap[splittedString[0]] = splittedString[1]
|
||||||
}
|
}
|
||||||
if !slices.Contains(usernames, config.DemoUsername) {
|
if !slices.Contains(usernames, ac.Config.DemoUsername) {
|
||||||
usernames = append(usernames, config.DemoUsername)
|
usernames = append(usernames, ac.Config.DemoUsername)
|
||||||
usersPasswordMap[config.DemoUsername] = ""
|
usersPasswordMap[ac.Config.DemoUsername] = ""
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var existingUsers []model.User
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -70,7 +71,7 @@ func CreateDefaultUsers(db *gorm.DB, config *config.Config) error {
|
|||||||
if isInExistingUsers(username, existingUsers) {
|
if isInExistingUsers(username, existingUsers) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err = CreateUserWithHashedPassword(db, username, usersPasswordMap[username])
|
err = CreateUserWithHashedPassword(ac, username, usersPasswordMap[username])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ ValidationRequired = "This field is required."
|
|||||||
ValidationTooShort = "This field is too short. It should be at least %s characters."
|
ValidationTooShort = "This field is too short. It should be at least %s characters."
|
||||||
ValidationTooLong = "This field is too long. It should be under %s characters."
|
ValidationTooLong = "This field is too long. It should be under %s characters."
|
||||||
ValidationPropertyFail = "Validation failed for '%s' property."
|
ValidationPropertyFail = "Validation failed for '%s' property."
|
||||||
|
RegistrationDisabled = "Registration has been disabled on this instance."
|
||||||
|
UserAlreadyExists = "An user with this name already exists."
|
||||||
|
ErrorWhenCreatingUserFromStr = "Error when creating user from string %s"
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ ValidationRequired = "Ce champ est requis."
|
|||||||
ValidationTooShort = "Ce champ est trop court. Il devrait contenir au moins %s caractères."
|
ValidationTooShort = "Ce champ est trop court. Il devrait contenir au moins %s caractères."
|
||||||
ValidationTooLong = "Ce champ est trop long. Il ne devrait pas dépasser %s caractères."
|
ValidationTooLong = "Ce champ est trop long. Il ne devrait pas dépasser %s caractères."
|
||||||
ValidationPropertyFail = "La validation a échoué pour la propriété '%s'."
|
ValidationPropertyFail = "La validation a échoué pour la propriété '%s'."
|
||||||
|
RegistrationDisabled = "La création de nouveaux comptes a été désactivée sur cette instance."
|
||||||
|
UserAlreadyExists = "Un utilisateur avec le même nom existe déjà."
|
||||||
|
ErrorWhenCreatingUserFromStr = "Erreur lors de la création de l'utilisateur %s"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/createuser"
|
"git.artlef.fr/PersonalLibraryManager/internal/createuser"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ func PostSignupHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac,
|
myvalidator.ReturnErrorsAsJsonResponse(&ac,
|
||||||
myvalidator.HttpError{
|
myvalidator.HttpError{
|
||||||
StatusCode: http.StatusForbidden,
|
StatusCode: http.StatusForbidden,
|
||||||
Err: errors.New("Registration has been disabled on this instance."),
|
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "RegistrationDisabled")),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ func PostSignupHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = createuser.CreateUser(ac.Db, user.Username, user.Password)
|
err = createuser.CreateUser(ac, user.Username, user.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -25,13 +25,13 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
err = createuser.CreateDefaultUsers(db, config)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
bundle := i18nresource.InitializeI18n()
|
bundle := i18nresource.InitializeI18n()
|
||||||
|
err = createuser.CreateDefaultUsers(appcontext.AppContext{C: nil, Db: db, I18n: bundle, Config: config})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
ws := r.Group("/ws")
|
ws := r.Group("/ws")
|
||||||
ws.Use(middleware.Auth())
|
ws.Use(middleware.Auth())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user