/
api_handler_clienttags.go
98 lines (82 loc) · 2.52 KB
/
api_handler_clienttags.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
package chserver
import (
"net/http"
"sort"
"github.com/openrport/openrport/server/api"
"github.com/openrport/openrport/share/query"
)
var (
ClientTagsOptionsSupportedFields = map[string]map[string]bool{
"client_tags": {
"tag": true,
"client_ids": true,
},
}
)
type ClientTagPayload struct {
Tag *string `json:"tag,omitempty"`
ClientIDs *[]string `json:"client_ids,omitempty"`
tagForSort string `json:"-"`
}
func (al *APIListener) handleGetClientTags(w http.ResponseWriter, req *http.Request) {
options := query.GetListOptions(req)
errs := query.ValidateListOptions(options, nil /* sorts */, nil /* filters */, ClientTagsOptionsSupportedFields, &query.PaginationConfig{
MaxLimit: 500,
DefaultLimit: 50,
})
if errs != nil {
al.jsonError(w, errs)
return
}
curUser, err := al.getUserModelForAuth(req.Context())
if err != nil {
al.jsonError(w, err)
return
}
groups, err := al.clientGroupProvider.GetAll(req.Context())
if err != nil {
al.jsonErrorResponseWithError(w, http.StatusInternalServerError, "Failed to get client groups.", err)
return
}
clients := al.clientService.GetUserClients(groups, curUser)
clientIDsByTag := make(map[string][]string)
for _, client := range clients {
for _, tag := range client.Tags {
clientIDsByTag[tag] = append(clientIDsByTag[tag], client.ID)
}
}
payload := convertToClientTagsPayload(clientIDsByTag, options.Fields)
sort.Slice(payload, func(i, j int) bool { return payload[i].tagForSort < payload[j].tagForSort })
totalCount := len(payload)
start, end := options.Pagination.GetStartEnd(totalCount)
payload = payload[start:end]
al.writeJSONResponse(w, http.StatusOK, &api.SuccessPayload{
Data: payload,
Meta: api.NewMeta(totalCount),
})
}
func convertToClientTagsPayload(data map[string][]string, fields []query.FieldsOption) []ClientTagPayload {
result := make([]ClientTagPayload, 0, len(data))
for tag, clientIDs := range data {
result = append(result, convertToClientTagPayload(tag, clientIDs, fields))
}
return result
}
func convertToClientTagPayload(tag string, clientIDs []string, fields []query.FieldsOption) ClientTagPayload { //nolint:gocyclo
requestedFields := query.RequestedFields(fields, "client_tags")
payload := ClientTagPayload{
tagForSort: tag,
}
for field := range ClientTagsOptionsSupportedFields["client_tags"] {
if len(fields) > 0 && !requestedFields[field] {
continue
}
switch field {
case "tag":
payload.Tag = &tag
case "client_ids":
payload.ClientIDs = &clientIDs
}
}
return payload
}