User delete endpoint

This commit is contained in:
Lukas Bachschwell 2018-11-15 18:11:09 +01:00
parent f7f7e20b69
commit c19f60f565

View File

@ -3,16 +3,18 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"multitenantStack/constants"
"multitenantStack/models" "multitenantStack/models"
"multitenantStack/services/companydb"
"strconv" "strconv"
"strings" "strings"
"github.com/astaxie/beego" "github.com/astaxie/beego/orm"
) )
// CompanyUserController operations for CompanyUser // CompanyUserController operations for CompanyUser
type CompanyUserController struct { type CompanyUserController struct {
beego.Controller BaseAPIController
} }
// URLMapping ... // URLMapping ...
@ -162,10 +164,37 @@ func (c *CompanyUserController) Put() {
func (c *CompanyUserController) Delete() { func (c *CompanyUserController) Delete() {
idStr := c.Ctx.Input.Param(":id") idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
if currentUser.Id != id {
c.ServeJSONError("You can not delete users other than yourself!")
c.ServeJSON()
}
if currentUser.Role == constants.RoleOwner {
c.ServeJSONError("You can not delete users other than yourself!")
c.ServeJSON()
}
if err := models.DeleteCompanyUser(o, id); err == nil { if err := models.DeleteCompanyUser(o, id); err == nil {
c.Data["json"] = "OK" c.Data["json"] = "OK"
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
// After deleting the user here we need to delete the same User in the system DB
userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, jwtSession.Email)
if err != nil {
c.ServeJSONError("Error deleting Company User")
return
}
systemDB := companydb.GetSystemDatabase()
systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
if err != nil {
c.ServeJSONError("Error deleting User Company Relation")
return
}
c.ServeJSON() c.ServeJSON()
} }