change dlete role api from id to key 'role'
This commit is contained in:
55
api.go
55
api.go
@@ -237,61 +237,54 @@ func (aH *AccessHandlerAPI) UpdateRole(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (aH *AccessHandlerAPI) DeleteRole(c *gin.Context) {
|
||||
queryId := c.Query("id")
|
||||
|
||||
if queryId == "" || queryId == "null" || queryId == "undefined" {
|
||||
aH.logger.Error("DeleteRole", "id query missing or wrong value: "+queryId)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "id query missing or wrong value: " + queryId,
|
||||
})
|
||||
queryRole := c.Query("role")
|
||||
if queryRole == "" || queryRole == "null" || queryRole == "undefined" {
|
||||
aH.logger.Error("DeleteRole", "id query missing or wrong value: "+queryRole)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var request struct {
|
||||
Ids []int `json:"ids"`
|
||||
Roles []string `json:"roles"`
|
||||
}
|
||||
|
||||
err := c.BindJSON(&request)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteRole", "id query missing or wrong value: "+queryId)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
aH.logger.Error("DeleteRole", err)
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if len(request.Ids) == 0 {
|
||||
if len(request.Roles) == 0 {
|
||||
aH.logger.Error("DeleteRole", "no ids given to be deleted")
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "no ids given to be deleted",
|
||||
})
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse("no roles given to be deleted"))
|
||||
return
|
||||
}
|
||||
|
||||
var ownId string
|
||||
removeIds := make([]uint, len(request.Ids))
|
||||
for i, id := range request.Ids {
|
||||
if queryId == fmt.Sprint(id) {
|
||||
ownId = queryId
|
||||
var ownRole string
|
||||
|
||||
for _, role := range request.Roles {
|
||||
if queryRole == role {
|
||||
ownRole = role
|
||||
continue
|
||||
}
|
||||
removeIds[i] = uint(id)
|
||||
err = aH.dbHandler.DeleteByKey(&models.Role{}, "role", role, false)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if ownId != "" {
|
||||
aH.logger.Error("DeleteRole", "can not delete logged in member role id: "+queryId)
|
||||
if ownRole != "" {
|
||||
aH.logger.Error("DeleteRole", "can not delete logged in role id: "+ownRole)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "can not delete logged in member id: " + queryId,
|
||||
"id": queryId,
|
||||
"message": "can not delete logged in role id: " + ownRole,
|
||||
"role": ownRole,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = aH.dbHandler.DeleteById(&models.Role{}, removeIds...)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "role(s) deleted",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user