add new function replace realtions
This commit is contained in:
@@ -45,6 +45,7 @@ func (aH *AccessHandler) AddDefaultUser() (err error) {
|
||||
if err := aH.dbHandler.GetByKey(role, "role", "admin", false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return aH.dbHandler.AddRelation(user, role, "Role")
|
||||
}
|
||||
|
||||
@@ -132,7 +133,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
|
||||
// get user to check ChangePassword
|
||||
var dbRecord models.User
|
||||
err = aH.dbHandler.GetById(&dbRecord, user.Id, "Role")
|
||||
err = aH.dbHandler.GetById(&dbRecord, user.Id, "Role", "Workspaces")
|
||||
if err != nil {
|
||||
aH.logger.Error("ChangePassword", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -158,7 +159,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
aH.logger.Debug("ChangePassword", "change user "+user.Name+" password")
|
||||
|
||||
// Update user
|
||||
aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
|
||||
aH.dbHandler.UpdateValuesById(&user, user.Id, "Role", "Workspaces")
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("password of user '%s' changed", user.Name),
|
||||
@@ -182,12 +183,18 @@ func (aH *AccessHandler) GetUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
err = aH.dbHandler.GetById(&users, uint(i), "Role")
|
||||
err = aH.dbHandler.GetById(&users, uint(i), "Role", "Workspaces")
|
||||
if err != nil {
|
||||
aH.logger.Error("GetUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
//remove password
|
||||
for i := range users {
|
||||
users[i].Password = ""
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
@@ -198,14 +205,27 @@ func (aH *AccessHandler) UpdateUser(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
err := aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
|
||||
|
||||
var currentUser models.User
|
||||
err := aH.dbHandler.GetById(¤tUser, user.Id, "Role", "Workspace")
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
if len(user.Workspaces) > 0 {
|
||||
aH.dbHandler.AddRelation(&user, user.Workspaces, "Workspaces")
|
||||
|
||||
err = aH.dbHandler.ReplaceRelation(&user, "Workspaces", user.Workspaces)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = aH.dbHandler.UpdateValuesById(&user, user.Id, "Role", "Workspaces")
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated user '"+user.Email+"'"))
|
||||
|
||||
Reference in New Issue
Block a user