1
0

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
4 changed files with 16 additions and 46 deletions

View File

@ -1,29 +1,26 @@
package models
// DeleteNamespaceByID deletes a namespace and takes its id as an argument
func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) {
// Delete deletes a namespace
func (n *Namespace) Delete(id int64) (err error) {
// Check if the namespace exists
namespace, err := GetNamespaceByID(namespaceID)
if err != nil {
return
}
// Check if the user is namespace admin
err = doer.IsNamespaceAdmin(&namespace)
_, err = GetNamespaceByID(id)
if err != nil {
return
}
// Delete the namespace
_, err = x.ID(namespaceID).Delete(&Namespace{})
_, err = x.ID(id).Delete(&Namespace{})
if err != nil {
return
}
// Delete all lists with their items
lists, err := GetListsByNamespaceID(namespaceID)
lists, err := GetListsByNamespaceID(id)
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 {
listIDs = append(listIDs, list.ID)
}

View File

@ -88,6 +88,12 @@ func (n *Namespace) CanUpdate(user *User, id int64) bool {
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
func (n *Namespace) CanCreate(user *User, id int64) bool {
// This is currently a dummy function, later on we could imagine global limits etc.