-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
488 lines (410 loc) · 10.6 KB
/
accounts.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package main
import (
"errors"
"log"
"net/http"
"strconv"
"github.com/ejv2/prepper/data"
"github.com/ejv2/prepper/isams"
"github.com/gin-gonic/gin"
)
// handleAccounts is the handler for "/account/".
func handleAccounts(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
if !ddat.User.Can(data.CapManageUsers) {
c.String(http.StatusForbidden, "Access Denied")
return
}
us, err := data.GetUsers(Database)
if err != nil {
internalError(c, err)
return
}
dat := struct {
DashboardData
Users []data.User
SessionCount int
}{ddat, us, Sessions.Len()}
c.HTML(http.StatusOK, "accounts.gohtml", dat)
}
// handleEditAccount is the handler for "/account/[ID]".
//
// Returns the HTML editor page for the given account.
func handleEditAccount(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
suid := c.Param("id")
if suid == "" {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
if uint(uid) != ddat.User.ID && !ddat.User.Can(data.CapManageUsers) {
c.String(http.StatusForbidden, "Access Denied")
return
}
us, err := data.GetUser(Database, uint(uid))
if err != nil {
if errors.Is(err, data.ErrUserNotFound) {
c.String(http.StatusNotFound, "User Not Found")
return
}
internalError(c, err)
return
}
dat := struct {
DashboardData
TargetUser data.User
}{ddat, us}
c.HTML(http.StatusOK, "accounts-edit.gohtml", dat)
}
func handleNewAccount(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
role := data.UserTeacher
if _, t := c.GetQuery("technician"); t {
role = data.UserTechnician
}
if _, t := c.GetQuery("admin"); t {
role = data.UserAdmin
}
us, err := data.GetUser(Database, s.UserID)
if err != nil {
internalError(c, err)
return
}
if !us.Can(data.CapManageUsers) {
c.String(http.StatusForbidden, "Access Denied")
return
}
u, err := data.NewUser(Database, data.UserRole(role))
if err != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, "/account/"+strconv.FormatUint(uint64(u.ID), 10))
}
// handleAccountSwitch is the handler for "/account/switch".
//
// Shows an HTML account selection page which allows the choice of which
// account to impersonate. The switch itself is done by another route.
func handleAccountSwitch(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
if !ddat.User.Can(data.CapImpersonate) {
c.String(http.StatusForbidden, "Access Denied")
return
}
if c.Query("user") != "" {
suid := c.Query("user")
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.Redirect(http.StatusFound, "/account/switch?error")
return
}
u, err := data.GetUser(Database, uint(uid))
if err != nil {
c.Redirect(http.StatusFound, "/account/switch?error")
return
}
s.SignIn(u.ID)
ddat, err = NewDashboardData(s)
if err != nil {
c.Redirect(http.StatusFound, "/account/switch?error")
return
}
c.HTML(http.StatusOK, "switch_success.gohtml", ddat)
return
}
users, err := data.GetUsers(Database)
if err != nil {
internalError(c, err)
return
}
_, e := c.GetQuery("error")
dat := struct {
DashboardData
Users []data.User
Error bool
}{ddat, users, e}
c.HTML(http.StatusOK, "switch.gohtml", dat)
}
// handleChangePassword is the handler for "/account/password".
//
// This only allows changes for the current user and such is not an
// authenticated route.
func handleChangePassword(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
_, erro := c.GetQuery("error")
_, success := c.GetQuery("success")
dat := struct {
DashboardData
Error bool
Success bool
}{ddat, erro, success}
c.HTML(http.StatusOK, "password.gohtml", dat)
}
// handleChangePasswordAttempt is the handler for POST @ "/account/password".
//
// This route actually changes the password given by the user.
func handleChangePasswordAttempt(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
oldPass, ok := c.GetPostForm("old_password")
if !ok {
c.Redirect(http.StatusFound, "/account/password?error")
return
}
newpass, ok := c.GetPostForm("new_password")
if !ok {
c.Redirect(http.StatusFound, "/account/password?error")
return
}
if !ddat.User.Password.Matches(oldPass) {
c.Redirect(http.StatusFound, "/account/password?error")
return
}
if ddat.User.SetPassword(newpass) != nil {
c.Redirect(http.StatusFound, "/account/password?error")
return
}
if Database.Updates(&ddat.User).Error != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, "/account/password?success")
}
// handleAccountTimetable is the handler for "/account/[ID]/timetable"
//
// This allows changes to a user's timetable. Changes to the current user's
// timetable are allowed unauthenticated, but to change others the user must be
// at least a technician.
func handleAccountTimetable(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
suid := c.Param("id")
if suid == "" {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
// Need to be technician to manage another's timetable.
if uint(uid) != s.UserID && !ddat.User.Can(data.CapManageTimetable) {
c.String(http.StatusForbidden, "Permission Denied")
return
}
usr, err := data.GetUser(Database, uint(uid))
if err != nil {
c.String(http.StatusNotFound, "User not Found")
return
}
var iusr *isams.User
var iusrs []isams.User
var isched *isams.UserTimetable
if Config.HasISAMS() {
if usr.IsamsID != nil {
// NOTE: deliberately ignoring error here to use nil as a
// sentinel. very naughty!
iusr, _ = ISAMS.FindUser(*usr.IsamsID)
if iusr != nil {
isched = iusr.Timetable(ISAMS)
}
}
iusrs = ISAMS.Users
}
dat := struct {
DashboardData
TargetUser data.User
ISAMSEnabled bool
ISAMSUser *isams.User
ISAMSUsers []isams.User
ISAMSSchedule *isams.UserTimetable
}{ddat, usr, Config.HasISAMS(), iusr, iusrs, isched}
c.HTML(http.StatusOK, "link.gohtml", dat)
}
// handleAccountUnlink is the handler for "/account/[ID]/unlink".
//
// Simply removes the iSAMS ID field from a user's database record. This is
// harder using GORM than I thought...
func handleAccountUnlink(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
suid := c.Param("id")
if suid == "" {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
// Need to be technician to link another's timetable
if uint(uid) != s.UserID && !ddat.User.Can(data.CapManageTimetable) {
c.String(http.StatusForbidden, "Permission Denied")
return
}
usr, err := data.GetUser(Database, uint(uid))
if err != nil {
c.String(http.StatusNotFound, "User not Found")
return
}
usr.IsamsID = nil
if err := Database.Model(&usr).Where(&usr).Update("isams_id", nil).Error; err != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, "/account/"+suid+"/timetable")
}
// handleAccountLink is the handler for "/account/[ID]/link".
//
// One GET parameter is expected containing the iSAMS UserCode which can be
// added to the user's account.
func handleAccountLink(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
suid := c.Param("id")
if suid == "" {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
// Need to be technician to link another's timetable
if uint(uid) != s.UserID && !ddat.User.Can(data.CapManageTimetable) {
c.String(http.StatusForbidden, "Permission Denied")
return
}
usr, err := data.GetUser(Database, uint(uid))
if err != nil {
c.String(http.StatusNotFound, "User not Found")
return
}
if usr.IsamsID != nil {
c.String(http.StatusForbidden, "User already has iSAMS ID. Must unlink existing first!")
return
}
id, ok := c.GetQuery("id")
if !ok {
c.String(http.StatusBadRequest, "Expected ID parameter")
return
}
iusr, err := ISAMS.FindUser(id)
if err != nil {
c.String(http.StatusNotFound, "No such iSAMS user")
return
}
usr.IsamsID = &iusr.UserCode
if err = Database.Updates(&usr).Error; err != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, "/account/"+suid+"/timetable")
}
// handleAccountSync is the handler for "/account/[ID]/sync".
//
// Handles the syncing of account details between here and iSAMS.
func handleAccountSync(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
suid := c.Param("id")
if suid == "" {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
uid, err := strconv.ParseUint(suid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid user ID")
return
}
// Need to be technician to link another's timetable
if uint(uid) != s.UserID && !ddat.User.Can(data.CapManageTimetable) {
c.String(http.StatusForbidden, "Permission Denied")
return
}
usr, err := data.GetUser(Database, uint(uid))
if err != nil {
c.String(http.StatusNotFound, "User not Found")
return
}
if usr.IsamsID == nil {
c.String(http.StatusBadRequest, "Cannot sync on an unlinked account")
return
}
iusr, err := ISAMS.FindUser(*usr.IsamsID)
if err != nil {
internalError(c, err)
return
}
// Names
usr.Title = iusr.Title
usr.FirstName = iusr.Forename
usr.LastName = iusr.Surname
// Contact
usr.Email = iusr.SchoolEmailAddress
usr.Telephone = iusr.SchoolMobileNumber
log.Println(c.RemoteIP(), "syncs", usr.Username, "with iSAMS", iusr.UserCode, "--", usr)
if err := Database.Updates(&usr).Error; err != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, "/account/"+strconv.FormatUint(uint64(usr.ID), 10))
}