This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
116 lines (97 loc) · 2.84 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package user
import (
"context"
"errors"
"net/http"
"strconv"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/go-chi/chi"
)
// delete - Delete application user by id
// @Summary Delete a application user
// @Description Delete application user by ID
// @Tags ApplicationUser
// @ID delete-application-user-by-id
// @Param X-User header string true "User ID"
// @Param application_id path string true "Application ID"
// @Param organisation_id path string true "Organisation ID"
// @Param user_id path string true "User ID"
// @Success 200
// @Router /organisations/{organisation_id}/applications/{application_id}/users/{user_id} [delete]
func delete(w http.ResponseWriter, r *http.Request) {
organisationID := chi.URLParam(r, "organisation_id")
orgID, err := strconv.Atoi(organisationID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
userID := chi.URLParam(r, "user_id")
uID, err := strconv.Atoi(userID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
var currentUID int
currentUID, err = strconv.Atoi(r.Header.Get("X-User"))
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
applicationID := chi.URLParam(r, "application_id")
appID, err := strconv.Atoi(applicationID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
// Check if logged in user is owner
err = util.CheckOwner(uint(currentUID), uint(orgID))
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.CannotSaveChanges()))
return
}
result := &model.Application{}
result.ID = uint(appID)
// Check if record exist
err = model.DB.Preload("Users").First(&result).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.RecordNotFound()))
return
}
newUsers := make([]model.User, 0)
flag := false
for _, user := range result.Users {
if user.ID == uint(uID) {
flag = true
} else {
newUsers = append(newUsers, user)
}
}
// if user not found for application
if !flag {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.RecordNotFound()))
return
}
// Check if the user to delete is not last user of application
if len(newUsers) < 1 {
loggerx.Error(errors.New("cannot delete last user of application"))
errorx.Render(w, errorx.Parser(errorx.CannotSaveChanges()))
return
}
if err = model.DB.WithContext(context.WithValue(r.Context(), userContext, currentUID)).Model(&result).Association("Users").Replace(&newUsers); err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
renderx.JSON(w, http.StatusOK, nil)
}