api/pkg/routes/api/v1/user_delete.go

66 lines
1.8 KiB
Go
Raw Normal View History

// Vikunja is a todo-list application to facilitate your life.
2020-01-09 17:33:22 +00:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-11-26 20:17:33 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-11-26 20:17:33 +00:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-11-26 20:17:33 +00:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-11-26 20:17:33 +00:00
2018-06-10 09:11:41 +00:00
package v1
import (
2018-10-31 12:42:38 +00:00
"code.vikunja.io/api/pkg/models"
2018-11-30 23:26:56 +00:00
"code.vikunja.io/web/handler"
2019-05-07 19:42:24 +00:00
"github.com/labstack/echo/v4"
2018-06-10 09:11:41 +00:00
"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 17:14:02 +00:00
_, err = models.GetUserByID(userID)
2018-06-10 09:11:41 +00:00
if err != nil {
2018-08-30 17:14:02 +00:00
if models.IsErrUserDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
}
2018-06-10 09:11:41 +00: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
2018-11-30 23:26:56 +00:00
err = models.DeleteUserByID(userID, doer)
2018-06-10 09:11:41 +00:00
if err != nil {
2018-11-30 23:26:56 +00:00
return handler.HandleHTTPError(err, c)
2018-06-10 09:11:41 +00:00
}
return c.JSON(http.StatusOK, models.Message{"success"})
}