add new db handler with table change to reference many2many
This commit is contained in:
@@ -16,31 +16,36 @@ func (aH *AccessHandler) AddUserTable() error {
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) AddDefaultUser() (err error) {
|
||||
name := "admin"
|
||||
role := "admin"
|
||||
email := "zuercher@tecamino.ch"
|
||||
|
||||
// Check if a user with this email already exists
|
||||
if aH.dbHandler.Exists(&models.User{}, "email", email, false) {
|
||||
aH.logger.Debug("AddDefaultUser", "user email "+email+" exists already")
|
||||
// Found a user → skip create
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create default settings for the new user
|
||||
settings := models.Settings{}
|
||||
aH.logger.Debug("AddDefaultUser", "set default quasar settings")
|
||||
settings.DefaultQuasarSettings()
|
||||
|
||||
// Insert default admin user into the database
|
||||
aH.dbHandler.AddNewColum(&models.User{
|
||||
Name: name,
|
||||
Role: role,
|
||||
Email: email,
|
||||
user := &models.User{
|
||||
Name: "admin",
|
||||
Email: "zuercher@tecamino.ch",
|
||||
Password: "$2a$10$sZZOWBP8DSFLrLFQNoXw8OsEEr0tez1B8lPzKCHofaHg6PMNxx1pG",
|
||||
Settings: settings,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if a user with this email already exists
|
||||
if aH.dbHandler.Exists(&models.User{}, "email", user.Email, false) {
|
||||
aH.logger.Debug("AddDefaultUser", "user email "+user.Email+" exists already")
|
||||
// Found a user → skip create
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert default admin user into the database
|
||||
|
||||
if err := aH.dbHandler.AddNewColum(user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
role := &models.Role{}
|
||||
if err := aH.dbHandler.GetByKey(role, "", "role", "admin", false); err != nil {
|
||||
return err
|
||||
}
|
||||
return aH.dbHandler.AddRelation(user, role, "Role")
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
@@ -73,7 +78,7 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Hash the provided password before saving
|
||||
hash, err := utils.HashPassword(user.Password)
|
||||
user.Password, err = utils.HashPassword(user.Password)
|
||||
if err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -83,16 +88,29 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
aH.logger.Debug("AddUser", "add default quasar user setting ")
|
||||
user.Settings.DefaultQuasarSettings()
|
||||
|
||||
aH.logger.Debug("AddUser", "add new user "+user.Name+" with role "+user.Role)
|
||||
aH.logger.Debug("AddUser", "add new user "+user.Name+" with role "+user.Role.Role)
|
||||
|
||||
if user.Role.Id != 0 {
|
||||
if err := aH.dbHandler.GetById(&user.Role, "", user.Role.Id); err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := aH.dbHandler.GetByKey(&user.Role, "", "role", user.Role.Role, false); err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
user.RoleID = &user.Role.Id
|
||||
|
||||
// Insert the new user record
|
||||
aH.dbHandler.AddNewColum(&models.User{
|
||||
Name: user.Name,
|
||||
Role: user.Role,
|
||||
Email: user.Email,
|
||||
Password: hash,
|
||||
Settings: user.Settings,
|
||||
})
|
||||
if err := aH.dbHandler.AddNewColum(&user); err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("user '%s' successfully added", user.Name),
|
||||
@@ -110,7 +128,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
|
||||
// get user to check ChangePassword
|
||||
var dbRecord models.User
|
||||
err = aH.dbHandler.GetById(&dbRecord, user.Id)
|
||||
err = aH.dbHandler.GetById(&dbRecord, "Role", user.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("ChangePassword", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -119,13 +137,11 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
|
||||
// Check if old password is correct
|
||||
if !utils.CheckPassword(user.Password, dbRecord.Password) {
|
||||
fmt.Println(123, dbRecord.Password, user.Password)
|
||||
// Found a user → skip create
|
||||
aH.logger.Error("ChangePassword", "wrong password entered for user: "+user.Name)
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse("invalid credentials"))
|
||||
return
|
||||
}
|
||||
fmt.Println(3)
|
||||
|
||||
// Hash the provided password before saving
|
||||
user.Password, err = utils.HashPassword(user.NewPassword)
|
||||
@@ -134,13 +150,11 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
fmt.Println(4)
|
||||
|
||||
aH.logger.Debug("ChangePassword", "change user "+user.Name+" password")
|
||||
|
||||
// Update user
|
||||
aH.dbHandler.UpdateValuesById(&user, user.Id)
|
||||
fmt.Println(5)
|
||||
aH.dbHandler.UpdateValuesById(&user, "Role", user.Id)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("password of user '%s' changed", user.Name),
|
||||
@@ -164,7 +178,7 @@ func (aH *AccessHandler) GetUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
err = aH.dbHandler.GetById(&users, uint(i))
|
||||
err = aH.dbHandler.GetById(&users, "Role", uint(i))
|
||||
if err != nil {
|
||||
aH.logger.Error("GetUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -180,7 +194,7 @@ func (aH *AccessHandler) UpdateUser(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
err := aH.dbHandler.UpdateValuesById(&user, user.Id)
|
||||
err := aH.dbHandler.UpdateValuesById(&user, "Role", user.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
|
||||
Reference in New Issue
Block a user