-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathevents.go
237 lines (226 loc) · 6.27 KB
/
events.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2013-2017, Jeremy Bingham (<[email protected]>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Serve up the list of logged events, and individual events as well.
package main
import (
"encoding/json"
"fmt"
"github.com/ctdk/goiardi/actor"
"github.com/ctdk/goiardi/loginfo"
"github.com/ctdk/goiardi/reqctx"
"github.com/ctdk/goiardi/util"
"net/http"
"strconv"
)
// The whole list
func eventListHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
opUser, oerr := reqctx.CtxReqUser(r.Context())
if oerr != nil {
jsonErrorReport(w, r, oerr.Error(), oerr.Status())
return
}
// Look for offset and limit parameters
r.ParseForm()
var offset, limit, purgeFrom int
if o, found := r.Form["offset"]; found {
if len(o) < 0 {
jsonErrorReport(w, r, "invalid offsets", http.StatusBadRequest)
return
}
var err error
offset, err = strconv.Atoi(o[0])
if err != nil {
jsonErrorReport(w, r, "invalid offset conversion to int", http.StatusBadRequest)
return
}
if offset < 0 {
jsonErrorReport(w, r, "invalid negative offset value", http.StatusBadRequest)
return
}
} else {
offset = 0
}
var limitFound bool
if l, found := r.Form["limit"]; found {
limitFound = true
if len(l) < 0 {
jsonErrorReport(w, r, "invalid limit", http.StatusBadRequest)
return
}
var err error
limit, err = strconv.Atoi(l[0])
if err != nil {
jsonErrorReport(w, r, "invalid limit conversion to int", http.StatusBadRequest)
return
}
if limit < 0 {
jsonErrorReport(w, r, "invalid negative limit value", http.StatusBadRequest)
return
}
}
if p, found := r.Form["purge"]; found {
if len(p) < 0 {
jsonErrorReport(w, r, "invalid purge id", http.StatusBadRequest)
return
}
var err error
purgeFrom, err = strconv.Atoi(p[0])
if err != nil {
jsonErrorReport(w, r, "invalid purge from conversion to int", http.StatusBadRequest)
return
}
if purgeFrom < 0 {
jsonErrorReport(w, r, "invalid negative purgeFrom value", http.StatusBadRequest)
return
}
}
paramStrs := []string{"from", "until", "action", "object_type", "object_name", "doer"}
searchParams := make(map[string]string, 6)
for _, v := range paramStrs {
if st, found := r.Form[v]; found {
if len(st) < 0 {
jsonErrorReport(w, r, "invalid "+v, http.StatusBadRequest)
return
}
searchParams[v] = st[0]
}
}
switch r.Method {
case http.MethodHead:
if !opUser.IsAdmin() {
headResponse(w, r, http.StatusForbidden)
return
}
headDefaultResponse(w, r)
return
case http.MethodGet:
if !opUser.IsAdmin() {
jsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
return
}
var leList []*loginfo.LogInfo
var err error
if limitFound {
leList, err = loginfo.GetLogInfos(searchParams, offset, limit)
} else {
leList, err = loginfo.GetLogInfos(searchParams, offset)
}
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusBadRequest)
return
}
leResp := make([]map[string]interface{}, len(leList))
for i, v := range leList {
leResp[i] = make(map[string]interface{})
leResp[i]["event"] = v
leURL := fmt.Sprintf("/events/%d", v.ID)
leResp[i]["url"] = util.CustomURL(leURL)
}
enc := json.NewEncoder(w)
if err := enc.Encode(&leResp); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
case http.MethodDelete:
if !opUser.IsAdmin() {
jsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
return
}
purged, err := loginfo.PurgeLogInfos(purgeFrom)
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusBadRequest)
}
leResp := make(map[string]string)
leResp["purged"] = fmt.Sprintf("Purged %d logged events", purged)
enc := json.NewEncoder(w)
if err := enc.Encode(&leResp); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
default:
jsonErrorReport(w, r, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}
// Individual log events
func eventHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
opUser, oerr := reqctx.CtxReqUser(r.Context())
if oerr != nil {
jsonErrorReport(w, r, oerr.Error(), oerr.Status())
return
}
eventIDstr := r.URL.Path[8:]
eventID, aerr := strconv.Atoi(eventIDstr)
if aerr != nil {
jsonErrorReport(w, r, aerr.Error(), http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodHead:
pcheck := func(r *http.Request, eventIDstr string, opUser actor.Actor) util.Gerror {
if !opUser.IsAdmin() {
return headForbidden()
}
return nil
}
if !opUser.IsAdmin() {
return
}
headChecking(w, r, opUser, eventIDstr, loginfo.DoesExist, pcheck)
return
case http.MethodGet:
if !opUser.IsAdmin() {
jsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
return
}
le, err := loginfo.Get(eventID)
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusNotFound)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(&le); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
case http.MethodDelete:
if !opUser.IsAdmin() {
jsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
return
}
le, err := loginfo.Get(eventID)
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusNotFound)
return
}
err = le.Delete()
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(&le); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
default:
jsonErrorReport(w, r, "Method not allowed", http.StatusMethodNotAllowed)
return
}
return
}