// UpdateUser allows to update a user
UpdateUser allows to update a user
// UpdateUser allows to update a user
func (UserController) UpdateUser(c *gin.Context) {
	type UserInput struct {
		ID        string `form:"id" json:"id"`
		Key       string `form:"key" json:"key"`
		Password  string `form:"password" json:"password"`
		Firstname string `form:"firstname" json:"firstname"`
		Lastname  string `form:"lastname" json:"lastname"`
	}
	var userInput UserInput
	if c.ShouldBind(&userInput) != nil {
		return
	}
	dbUser, err := store.GetUserByID(c, userInput.ID)
	if err != nil {
		c.AbortWithError(http.StatusNotFound, helpers.ErrorWithCode("user_not_found", "The user does not exist", err))
		return
	}
	if userInput.Key != dbUser.Key {
		c.AbortWithError(http.StatusUnauthorized, helpers.ErrorWithCode("invalid_key", "Invalid key", err))
		return
	}
	dbUser.Key = helpers.RandomString(40)
	if dbUser.Status == "created" {
		dbUser.Status = "activated"
	} else if dbUser.Status == "activated" {
		dbUser.Status = "modified"
	}
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(userInput.Password), bcrypt.DefaultCost)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, helpers.ErrorWithCode("encryption_failed", "Failed to generate the crypted password", err))
		return
	}
	dbUser.Password = string(hashedPassword)
	if userInput.Firstname != "" {
		dbUser.FirstName = userInput.Firstname
	}
	if userInput.Lastname != "" {
		dbUser.LastName = userInput.Lastname
	}
	dbUser.LastModification = time.Now().Unix()
	err = store.UpdateUser(c, dbUser.ID, dbUser)
	utils.CheckErr(err)
	c.JSON(http.StatusOK, "User well modified")
}
func ( UserController ) UpdateUser ( c * gin . Context ) { type UserInput struct { ID string `form:"id" json:"id"` Key string `form:"key" json:"key"` Password string `form:"password" json:"password"` Firstname string `form:"firstname" json:"firstname"` Lastname string `form:"lastname" json:"lastname"` } var userInput UserInput if c . ShouldBind ( & userInput ) != nil { return } dbUser , err := store . GetUserByID ( c , userInput . ID ) if err != nil { c . AbortWithError ( http . StatusNotFound , helpers . ErrorWithCode ( " " , " " , err ) ) return } if userInput . Key != dbUser . Key { c . AbortWithError ( http . StatusUnauthorized , helpers . ErrorWithCode ( " " , " " , err ) ) return } dbUser . Key = helpers . RandomString ( 40 ) if dbUser . Status == " " { dbUser . Status = " " } else if dbUser . Status == " " { dbUser . Status = " " } hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( userInput . Password ) , bcrypt . DefaultCost ) if err != nil { c . AbortWithError ( http . StatusInternalServerError , helpers . ErrorWithCode ( " " , " " , err ) ) return } dbUser . Password = string ( hashedPassword ) if userInput . Firstname != " " { dbUser . FirstName = userInput . Firstname } if userInput . Lastname != " " { dbUser . LastName = userInput . Lastname } dbUser . LastModification = time . Now ( ) . Unix ( ) err = store . UpdateUser ( c , dbUser . ID , dbUser ) utils . CheckErr ( err ) c . JSON ( http . StatusOK , " " ) }
UpdateUser allows to update a user
UpdateUser allows to update a user