Implemented deletion of namespaces

This commit is contained in:
konrad 2018-07-12 23:33:37 +02:00 committed by kolaente
parent 55c02bc973
commit c5888093fe
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 16 additions and 46 deletions

View file

@ -1,29 +1,26 @@
package models package models
// DeleteNamespaceByID deletes a namespace and takes its id as an argument // Delete deletes a namespace
func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) { func (n *Namespace) Delete(id int64) (err error) {
// Check if the namespace exists // Check if the namespace exists
namespace, err := GetNamespaceByID(namespaceID) _, err = GetNamespaceByID(id)
if err != nil {
return
}
// Check if the user is namespace admin
err = doer.IsNamespaceAdmin(&namespace)
if err != nil { if err != nil {
return return
} }
// Delete the namespace // Delete the namespace
_, err = x.ID(namespaceID).Delete(&Namespace{}) _, err = x.ID(id).Delete(&Namespace{})
if err != nil { if err != nil {
return return
} }
// Delete all lists with their items // Delete all lists with their items
lists, err := GetListsByNamespaceID(namespaceID) lists, err := GetListsByNamespaceID(id)
var listIDs []int64 var listIDs []int64
// We need to do that for here because we need the list ids to delete two times:
// 1) to delete the lists itself
// 2) to delete the list items
for _, list := range lists { for _, list := range lists {
listIDs = append(listIDs, list.ID) listIDs = append(listIDs, list.ID)
} }

View file

@ -88,6 +88,12 @@ func (n *Namespace) CanUpdate(user *User, id int64) bool {
return nn.IsAdmin(user) return nn.IsAdmin(user)
} }
// CanDelete checks if the user can delete a namespace
func (n *Namespace) CanDelete(user *User, id int64) bool {
nn, _ := GetNamespaceByID(id)
return nn.IsAdmin(user)
}
// CanCreate checks if the user can create a new namespace // CanCreate checks if the user can create a new namespace
func (n *Namespace) CanCreate(user *User, id int64) bool { func (n *Namespace) CanCreate(user *User, id int64) bool {
// This is currently a dummy function, later on we could imagine global limits etc. // This is currently a dummy function, later on we could imagine global limits etc.

View file

@ -1,10 +1,8 @@
package v1 package v1
import ( import (
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strconv"
) )
// DeleteNamespaceByID ... // DeleteNamespaceByID ...
@ -34,36 +32,5 @@ func DeleteNamespaceByID(c echo.Context) error {
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// Check if we have our ID return echo.NewHTTPError(http.StatusNotImplemented)
id := c.Param("id")
// Make int
itemID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
}
// Check if the user has the right to delete that namespace
user, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
err = models.DeleteNamespaceByID(itemID, &user)
if err != nil {
if models.IsErrNeedToBeNamespaceOwner(err) {
return c.JSON(http.StatusForbidden, models.Message{"You need to be the namespace owner to delete a namespace."})
}
if models.IsErrNamespaceDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"This namespace does not exist."})
}
if models.IsErrUserNeedsToBeNamespaceAdmin(err) {
return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to delete a namespace."})
}
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
return c.JSON(http.StatusOK, models.Message{"The namespace was deleted with success."})
} }

View file

@ -110,6 +110,6 @@ func RegisterRoutes(e *echo.Echo) {
a.PUT("/namespaces", namespaceHandler.CreateWeb) a.PUT("/namespaces", namespaceHandler.CreateWeb)
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb) a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb) a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID) a.DELETE("/namespaces/:id", namespaceHandler.DeleteWeb)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID) a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
} }