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
/
application.go
74 lines (61 loc) · 1.87 KB
/
application.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
package user
import (
"net/http"
"strconv"
"github.com/factly/kavach-server/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
)
type paging struct {
Nodes []model.User `json:"nodes"`
Total int `json:"total"`
}
// list - Get list of users for application
// @Summary Get list of users for application
// @Description Get list of users for application
// @Tags User
// @ID get-user-list
// @Produce json
// @Param X-User header string true "User ID"
// @Param X-Organisation header string true "Organisation ID"
// @Param application query string true "Application Slug"
// @Success 200 {object} paging
// @Router /users/application [get]
func list(w http.ResponseWriter, r *http.Request) {
userID, err := strconv.Atoi(r.Header.Get("X-User"))
if err != nil {
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
orgID, err := strconv.Atoi(r.Header.Get("X-Organisation"))
if err != nil {
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
appSlug := r.URL.Query().Get("application")
if appSlug == "" {
errorx.Render(w, errorx.Parser(errorx.GetMessage("invalid application query param", http.StatusBadRequest)))
return
}
// Check if user is part of organisation
permission := &model.OrganisationUser{}
err = model.DB.Model(&model.OrganisationUser{}).Where(&model.OrganisationUser{
OrganisationID: uint(orgID),
UserID: uint(userID),
}).First(permission).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
app := &model.Application{}
model.DB.Model(&model.Application{}).Where(model.Application{
Slug: appSlug,
OrganisationID: uint(orgID),
}).Preload("Users").Preload("Users.Medium").First(&app)
result := paging{}
result.Nodes = app.Users
result.Total = len(app.Users)
renderx.JSON(w, http.StatusOK, result)
}