-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_admin.go
301 lines (268 loc) · 7.58 KB
/
handle_admin.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"log"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"github.com/gwd/session-scheduler/event"
)
const (
FlagTestMode = "ServeTestMode"
FlagActive = "ServeActive"
FlagScheduleActive = "ServeScheduleActive"
FlagVerificationCodeSent = "ServeVerificationCodeSent"
FlagRequireVerification = "ServeRequireVerification"
)
func HandleAdminConsole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user := RequestUser(r)
if user == nil || !user.IsAdmin {
return
}
content := map[string]interface{}{"User": user}
tmpl := ps.ByName("template")
switch tmpl {
default:
return
case "console":
content["Vcode"], _ = kvs.Get(VerificationCode)
content["SinceLastSchedule"] = event.SchedLastUpdate()
switch event.SchedGetState() {
case event.SchedStateRunning:
content["IsInProgress"] = true
case event.SchedStateModified:
content["IsStale"] = true
default:
content["IsCurrent"] = true
}
ls := event.TimetableGetLockedSlots()
SlotsSetTimeDisplay(ls, slotTimeFormat)
content["LockedSlots"] = ls
fallthrough
case "locations":
var err error
content["Locations"], err = event.LocationGetAll()
if err != nil {
log.Printf("Error getting locations: %v", err)
}
}
content[tmpl] = true
RenderTemplate(w, r, "admin/"+tmpl, content)
}
var OptSearchAlgo string
func HandleAdminAction(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user := RequestUser(r)
if user == nil || !user.IsAdmin {
return
}
action := ps.ByName("action")
if !(action == "runschedule" ||
action == "setvcode" ||
action == "setstatus" ||
action == "resetEventData" ||
action == "setLocked" ||
action == "newLocation" ||
action == "updateLocation") {
return
}
switch action {
case "runschedule":
err := MakeSchedule(false)
if err == nil {
http.Redirect(w, r, "console?flash=Schedule+Finished", http.StatusFound)
} else {
log.Printf("Error generating schedule: %v", err)
http.Redirect(w, r, "console?flash=Error+starting+schedule: See Log", http.StatusFound)
}
case "setvcode":
newvcode := r.FormValue("vcode")
if newvcode == "" {
vcode, _ := kvs.Get(VerificationCode)
RenderTemplate(w, r, "console?flash=Invalid+Vcode",
map[string]interface{}{
"User": user,
"console": true,
"Vcode": vcode,
})
return
}
log.Printf("New vcode: %s", newvcode)
err := kvs.Set(VerificationCode, newvcode)
flash := "Verification+code+updated"
if err != nil {
flash = "Verification+code+not+updated"
log.Printf("Error setting verification code: %v", err)
}
http.Redirect(w, r, "console?flash="+flash, http.StatusFound)
return
case "setstatus":
r.ParseForm()
statuses := r.Form["status"]
newval := map[string]bool{
"website": false,
"schedule": false,
"verification": false,
"vcodesent": false}
for _, status := range statuses {
switch status {
case "websiteActive":
newval["website"] = true
case "scheduleActive":
newval["schedule"] = true
case "vcodeSent":
newval["vcodesent"] = true
case "requireVerification":
newval["verification"] = true
default:
log.Printf("Unexpected status value: %v", status)
flash := "Invalid form result: Report this error to the admin"
http.Redirect(w, r, "console?flash="+flash, http.StatusFound)
return
}
}
flashAccumulator := func(flash string, err error, fkey string, dbkey string, ftt string, ttf string) (string, error) {
if err != nil {
return flash, err
}
nv := newval[fkey]
ov, err := kvs.ExchangeBool(dbkey, nv)
if ov != nv {
if flash != "" {
flash += ", "
}
if nv {
flash += ftt
} else {
flash += ttf
}
}
return flash, nil
}
flash, err := flashAccumulator("", nil, "website",
FlagActive, "Website+Activated", "Website+Deactivated")
flash, err = flashAccumulator(flash, err, "schedule",
FlagScheduleActive, "Schedule+Activated", "Schedule+Deactivated")
flash, err = flashAccumulator(flash, err, "vcodesent",
FlagVerificationCodeSent, "Verification+Code+Sent", "Verification+Code+Not+Sent")
flash, err = flashAccumulator(flash, err, "verification",
FlagRequireVerification, "Verification+Required", "Verification+Not+Required")
http.Redirect(w, r, "console?flash="+flash, http.StatusFound)
return
case "setLocked":
r.ParseForm()
locked, err := FormCheckToSlotID(r.Form["locked"])
if err != nil {
return
}
log.Printf("New locked slots: %v", locked)
err = event.TimetableSetLockedSlots(locked)
if err != nil {
log.Printf("INTERNAL ERROR setting slots: %v", err)
http.Redirect(w, r, "console?flash=Error+setting+slots", http.StatusFound)
} else {
http.Redirect(w, r, "console?flash=Locked+slots+updated", http.StatusFound)
}
return
case "newLocation", "updateLocation":
l := event.Location{
LocationName: r.FormValue("locName"),
LocationURL: r.FormValue("locURL")}
// FIXME: This trashes thee contents of the form and doesn't give very
// informative error messages. See if we can do something better.
flash := ""
if action == "updateLocation" {
lidString := r.FormValue("locID")
lid, err := strconv.Atoi(lidString)
if err != nil {
log.Printf("Error parsing locationid: %v", err)
flash = "?flash=Website+Error"
} else {
l.LocationID = event.LocationID(lid)
}
}
if flash == "" {
cstring := r.FormValue("locCapacity")
if cstring != "" {
capacity, err := strconv.Atoi(cstring)
if err != nil {
log.Printf("Error parsing capacity: %v", err)
flash = "?flash=Capacity+must+be+a+number"
}
l.IsPlace = true
l.Capacity = capacity
}
}
if flash == "" {
var err error
switch action {
case "newLocation":
_, err = event.NewLocation(&l)
case "updateLocation":
err = event.LocationUpdate(&l)
}
if event.IsValidationError(err) {
log.Printf("Error creating new location: %v", err)
flash = "?flash=Validation+Error"
} else if err != nil {
log.Printf("Error creating new location: %v", err)
flash = "?flash=Internal+Error"
}
}
http.Redirect(w, r, "locations"+flash, http.StatusFound)
}
}
func HandleTestAction(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user := RequestUser(r)
if user == nil || !user.IsAdmin {
return
}
flash := "Success"
action := ps.ByName("action")
if !kvs.GetBoolDef(FlagTestMode) {
if action != "enabletest" {
return
}
if r.FormValue("confirm") != "SafetyOff" {
RenderTemplate(w, r, "admin/test", map[string]interface{}{
"MustConfirm": true,
})
return
}
err := kvs.SetBool(FlagTestMode, true)
if err != nil {
log.Printf("ERROR: Setting test mode failed: %v", err)
flash = "Set+test+mode+failed"
} else {
flash = "Test+mode+enabled"
}
} else {
switch action {
case "disabletest":
err := kvs.SetBool(FlagTestMode, false)
if err != nil {
log.Printf("ERROR: Setting test mode failed: %v", err)
flash = "Set+test+mode+failed"
} else {
flash = "Test+mode+disabled"
}
case "enabletest":
flash = "Test+mode+already+disabled"
// case "gendiscussion":
// countString := r.FormValue("count")
// count, err := strconv.Atoi(countString)
// if err != nil || !(count > 0) {
// flash = "Bad+input"
// } else {
// for i := 0; i < count; i++ {
// event.NewTestDiscussion(nil)
// }
// flash = countString + " discussions generated"
// }
// case "geninterest":
// event.TestGenerateInterest()
// flash = "Interest generated"
default:
return
}
}
http.Redirect(w, r, "/admin/test?flash="+flash, http.StatusFound)
}