2018-06-10 11:11:41 +02:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-07-25 16:24:46 +02:00
|
|
|
"code.vikunja.io/api/models"
|
2018-06-10 11:11:41 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserDelete is the handler to delete a user
|
|
|
|
func UserDelete(c echo.Context) error {
|
|
|
|
|
|
|
|
// TODO: only allow users to allow itself
|
|
|
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
// Make int
|
|
|
|
userID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"User ID is invalid."})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user exists
|
2018-08-30 19:14:02 +02:00
|
|
|
_, err = models.GetUserByID(userID)
|
2018-06-10 11:11:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-08-30 19:14:02 +02:00
|
|
|
if models.IsErrUserDoesNotExist(err) {
|
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
|
|
|
|
}
|
2018-06-10 11:11:41 +02:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get user."})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the doer options
|
|
|
|
doer, err := models.GetCurrentUser(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
err = models.DeleteUserByID(userID, &doer)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIDCannotBeZero(err) {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Id cannot be 0"})
|
|
|
|
}
|
|
|
|
|
|
|
|
if models.IsErrCannotDeleteLastUser(err) {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Cannot delete last user."})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
|
|
|
}
|