Added route to update a user's password
the build failed Details

This commit is contained in:
konrad 2018-01-23 15:53:38 +01:00 committed by kolaente
parent a92d711b00
commit 434856a44f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 93 additions and 2 deletions

View File

@ -84,5 +84,29 @@ func UpdateUser(user User) (updatedUser User, err error) {
// UpdateUserPassword updates the password of a user
func UpdateUserPassword(userID int64, newPassword string) (err error) {
// Get all user details
user, exists, err := GetUserByID(userID)
if err != nil {
return err
}
if !exists {
return ErrUserDoesNotExist{userID}
}
// Hash the new password and set it
hashed, err := hashPassword(newPassword)
if err != nil {
return err
}
user.Password = hashed
// Update it
_, err = x.Id(user.ID).Update(user)
if err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,67 @@
package v1
import (
"net/http"
"strconv"
"github.com/labstack/echo"
"git.mowie.cc/konrad/Library/models"
)
type datPassword struct {
Password string `json:"password"`
}
// UserChangePassword is the handler to add a user
func UserChangePassword(c echo.Context) error {
// Check if the user is admin
if !models.IsAdmin(c) {
return echo.ErrUnauthorized
}
// Check for Request Content
pwFromString := c.FormValue("password")
var datPw datPassword
if pwFromString == "" {
if err := c.Bind(&datPw); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No password provided."})
}
} else {
// Take the value directly from the input
datPw.Password = pwFromString
}
user := c.Param("id")
if user == "" {
return c.JSON(http.StatusBadRequest, models.Message{"User ID cannot be empty."})
}
// Make int
userID, err := strconv.ParseInt(user, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."})
}
// Get User Infos
_, exists, err := models.GetUserByID(userID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."})
}
// Check if it exists
if !exists {
return c.JSON(http.StatusNotFound, models.Message{"User not found."})
}
err = models.UpdateUserPassword(userID, datPw.Password)
if err != nil {
return err
}
return c.JSON(http.StatusOK, models.Message{"The password was updated successfully"})
}

View File

@ -108,13 +108,13 @@ func RegisterRoutes(e *echo.Echo) {
// ====== Admin Routes ======
// Manage Users
a.GET("/users", apiv1.UsersList)
a.PUT("/users", apiv1.UserAddOrUpdate)
a.POST("/users/:id", apiv1.UserAddOrUpdate)
a.GET("/users/:id", apiv1.UserShow)
a.DELETE("/users/:id", apiv1.UserDelete)
// Manage Users
a.POST("/users/:id/password", apiv1.UserChangePassword)
/*
Alles nur mit Api machen, davor dann einen onepager mit vue.js.