Implement deletion of users

This commit is contained in:
eikendev 2020-07-27 00:53:32 +02:00
parent 18d11677ac
commit f6ca287d0b
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
10 changed files with 112 additions and 35 deletions

View file

@ -21,7 +21,7 @@ type ApplicationDatabase interface {
// The ApplicationDispatcher interface for relaying notifications.
type ApplicationDispatcher interface {
RegisterApplication(name, user string) (string, error)
DeregisterApplication(matrixID string) error
DeregisterApplication(a *model.Application) error
}
// ApplicationHandler holds information for processing requests about applications.
@ -35,7 +35,7 @@ func (h *ApplicationHandler) applicationExists(token string) bool {
return application != nil
}
// CreateApplication creates a user.
// CreateApplication creates an application.
func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
var createApplication model.CreateApplication
@ -66,7 +66,7 @@ func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &application)
}
// DeleteApplication deletes a user with a certain ID.
// DeleteApplication deletes an application with a certain ID.
func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
var deleteApplication model.DeleteApplication
@ -76,13 +76,13 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
application, err := h.DB.GetApplicationByID(deleteApplication.ID)
log.Printf("Deleting application %s.\n", application.Name)
if success := successOrAbort(ctx, http.StatusBadRequest, err); !success {
return
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.Dispatcher.DeregisterApplication(application.MatrixID)); !success {
log.Printf("Deleting application %s.\n", application.Name)
if success := successOrAbort(ctx, http.StatusInternalServerError, h.Dispatcher.DeregisterApplication(application)); !success {
return
}

View file

@ -2,6 +2,7 @@ package api
import (
"errors"
"log"
"net/http"
"github.com/eikendev/pushbits/model"
@ -12,12 +13,21 @@ import (
// The UserDatabase interface for encapsulating database access.
type UserDatabase interface {
CreateUser(user *model.User) error
DeleteUser(user *model.User) error
GetUserByID(ID uint) (*model.User, error)
GetUserByName(name string) (*model.User, error)
GetApplications(user *model.User) ([]model.Application, error)
}
// The UserDispatcher interface for relaying notifications.
type UserDispatcher interface {
DeregisterApplication(a *model.Application) error
}
// UserHandler holds information for processing requests about users.
type UserHandler struct {
DB UserDatabase
DB UserDatabase
Dispatcher ApplicationDispatcher
}
func (h *UserHandler) userExists(name string) bool {
@ -46,3 +56,36 @@ func (h *UserHandler) CreateUser(ctx *gin.Context) {
ctx.JSON(http.StatusOK, user.IntoExternalUser())
}
// DeleteUser deletes a user with a certain ID.
func (h *UserHandler) DeleteUser(ctx *gin.Context) {
var deleteUser model.DeleteUser
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.BindUri(&deleteUser)); !success {
return
}
user, err := h.DB.GetUserByID(deleteUser.ID)
if success := successOrAbort(ctx, http.StatusBadRequest, err); !success {
return
}
log.Printf("Deleting user %s.\n", user.Name)
applications, err := h.DB.GetApplications(user)
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
return
}
for _, app := range applications {
if success := successOrAbort(ctx, http.StatusInternalServerError, h.Dispatcher.DeregisterApplication(&app)); !success {
return
}
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DB.DeleteUser(user)); !success {
return
}
ctx.JSON(http.StatusOK, gin.H{})
}