-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathadmin.go
441 lines (386 loc) · 14.6 KB
/
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
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
package lobster
import "github.com/gorilla/mux"
import "fmt"
import "net/http"
import "strconv"
type AdminFormParams struct {
Frame FrameParams
Token string
}
type AdminHandlerFunc func(http.ResponseWriter, *http.Request, *Session, FrameParams)
func adminWrap(h AdminHandlerFunc) func(http.ResponseWriter, *http.Request, *Session) {
return func(w http.ResponseWriter, r *http.Request, session *Session) {
if !session.IsLoggedIn() {
http.Redirect(w, r, "/login", 303)
return
}
// revert login as another user
if session.OriginalId != 0 {
session.UserId = session.OriginalId
session.OriginalId = 0
}
// confirm session admin and also user still admin
user := UserDetails(session.UserId)
if !user.Admin || !session.Admin {
http.Redirect(w, r, "/panel/dashboard", 303)
return
}
var frameParams FrameParams
if r.URL.Query()["message"] != nil {
frameParams.Message.Text = r.URL.Query()["message"][0]
if r.URL.Query()["type"] != nil {
frameParams.Message.Type = r.URL.Query()["type"][0]
} else {
frameParams.Message.Type = "info"
}
}
h(w, r, session, frameParams)
}
}
func adminDashboard(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
RenderTemplate(w, "admin", "dashboard", frameParams)
}
type AdminUsersParams struct {
Frame FrameParams
Users []*User
}
func adminUsers(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
params := AdminUsersParams{}
params.Frame = frameParams
params.Users = UserList()
RenderTemplate(w, "admin", "users", params)
}
type AdminUserProcessFunc func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User)
func adminUserProcess(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, f AdminUserProcessFunc) {
userId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/users", L.FormattedError("invalid_user"))
return
}
user := UserDetails(userId)
if user == nil {
RedirectMessage(w, r, "/admin/users", L.FormattedError("user_not_found"))
return
} else {
f(w, r, session, frameParams, user)
}
}
type AdminUserParams struct {
Frame FrameParams
User *User
StatusAction string // action that admin can take on this user, either "disable" or "enable" depending on current user status
VirtualMachines []*VirtualMachine
Token string
}
func adminUser(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
params := AdminUserParams{}
params.Frame = frameParams
params.User = user
if user.Status == "disabled" {
params.StatusAction = "enable"
} else {
params.StatusAction = "disable"
}
params.VirtualMachines = vmList(user.Id)
params.Token = CSRFGenerate(session)
RenderTemplate(w, "admin", "user", params)
})
}
func adminUserLogin(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
session.OriginalId = session.UserId
session.UserId = user.Id
http.Redirect(w, r, "/panel/dashboard", 303)
})
}
type AdminUserCreditForm struct {
Credit float64 `schema:"credit"`
Description string `schema:"description"`
}
func adminUserCredit(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
form := new(AdminUserCreditForm)
err := decoder.Decode(form, r.PostForm)
if err != nil {
http.Redirect(w, r, fmt.Sprintf("/admin/user/%d", user.Id), 303)
return
}
creditInt := int64(form.Credit * BILLING_PRECISION)
UserApplyCredit(user.Id, creditInt, form.Description)
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.Success("credit_applied"))
})
}
func adminUserPassword(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
if r.PostFormValue("password") != r.PostFormValue("password_confirm") {
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.FormattedError("password_mismatch"))
return
} else if r.PostFormValue("password") == "" {
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.FormattedError("password_empty"))
return
}
authForceChangePassword(user.Id, r.PostFormValue("password"))
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.Success("password_reset"))
})
}
func adminUserDisable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
db.Exec("UPDATE users SET status = 'disabled' WHERE id = ?", user.Id)
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.Success("user_disabled"))
})
}
func adminUserEnable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
adminUserProcess(w, r, session, frameParams, func(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams, user *User) {
db.Exec("UPDATE users SET status = 'active' WHERE id = ?", user.Id)
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", user.Id), L.Success("user_enabled"))
})
}
type AdminVirtualMachinesParams struct {
Frame FrameParams
VirtualMachines []*VirtualMachine
}
func adminVirtualMachines(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
params := AdminVirtualMachinesParams{}
params.Frame = frameParams
params.VirtualMachines = vmListAll()
RenderTemplate(w, "admin", "vms", params)
}
// virtual machine actions
func adminVMProcess(r *http.Request) (*VirtualMachine, error) {
vmId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
return nil, fmt.Errorf("invalid VM ID")
}
vm := vmGet(vmId)
if vm == nil {
return nil, fmt.Errorf("VM does not exist")
}
return vm, nil
}
func adminVMSuspend(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
vm, err := adminVMProcess(r)
if err != nil {
RedirectMessage(w, r, "/admin/users", L.FormatError(err))
}
vm.Suspend(false)
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", vm.UserId), L.Success("vm_suspended"))
}
func adminVMUnsuspend(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
vm, err := adminVMProcess(r)
if err != nil {
RedirectMessage(w, r, "/admin/users", L.FormatError(err))
}
err = vm.Unsuspend()
if err != nil {
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", vm.UserId), L.FormatError(err))
} else {
RedirectMessage(w, r, fmt.Sprintf("/admin/user/%d", vm.UserId), L.Success("vm_unsuspended"))
}
}
type AdminPlansParams struct {
Frame FrameParams
Plans []*Plan
Regions []string
Token string
}
func adminPlans(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
params := AdminPlansParams{}
params.Frame = frameParams
params.Plans = planList()
params.Regions = regionList()
params.Token = CSRFGenerate(session)
RenderTemplate(w, "admin", "plans", params)
}
type AdminPlansAddForm struct {
Name string `schema:"name"`
Price float64 `schema:"price"`
Ram int `schema:"ram"`
Cpu int `schema:"cpu"`
Storage int `schema:"storage"`
Bandwidth int `schema:"bandwidth"`
Global string `schema:"global"`
}
func adminPlansAdd(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
form := new(AdminPlansAddForm)
err := decoder.Decode(form, r.PostForm)
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormatError(err))
return
}
planCreate(form.Name, int64(form.Price*BILLING_PRECISION), form.Ram, form.Cpu, form.Storage, form.Bandwidth, form.Global != "")
RedirectMessage(w, r, "/admin/plans", L.Success("plan_created"))
}
func adminPlansAutopopulate(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
err := planAutopopulate(r.PostFormValue("region"))
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormatError(err))
return
} else {
RedirectMessage(w, r, "/admin/plans", L.Success("plan_autopopulate_success"))
}
}
type AdminPlanParams struct {
Frame FrameParams
Plan *Plan
Regions []string
Token string
}
func adminPlan(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
plan := planGet(planId)
if plan == nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("plan_not_found"))
return
}
plan.LoadRegionPlans()
plan.LoadMetadata()
params := AdminPlanParams{}
params.Frame = frameParams
params.Plan = plan
params.Regions = regionList()
params.Token = CSRFGenerate(session)
RenderTemplate(w, "admin", "plan", params)
}
func adminPlanDelete(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planDelete(planId)
RedirectMessage(w, r, "/admin/plans", L.Success("plan_deleted"))
}
func adminPlanEnable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planEnable(planId)
RedirectMessage(w, r, "/admin/plans", L.Success("plan_enabled"))
}
func adminPlanDisable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planDisable(planId)
RedirectMessage(w, r, "/admin/plans", L.Success("plan_disabled"))
}
func adminPlanAssociateRegion(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
err = planAssociateRegion(planId, r.PostFormValue("region"), r.PostFormValue("identification"))
if err != nil {
RedirectMessage(w, r, fmt.Sprintf("/admin/plan/%d", planId), L.FormatError(err))
} else {
RedirectMessage(w, r, fmt.Sprintf("/admin/plan/%d", planId), L.Success("plan_region_associated"))
}
}
func adminPlanDeassociateRegion(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planDeassociateRegion(planId, mux.Vars(r)["region"])
RedirectMessage(w, r, fmt.Sprintf("/admin/plan/%d", planId), L.Success("plan_region_deassociated"))
}
func adminPlanSetMetadata(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planSetMetadata(planId, r.PostFormValue("k"), r.PostFormValue("v"))
RedirectMessage(w, r, fmt.Sprintf("/admin/plan/%d", planId), L.Success("plan_updated"))
}
func adminPlanUnsetMetadata(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
planId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/plans", L.FormattedError("invalid_plan"))
return
}
planUnsetMetadata(planId, r.PostFormValue("k"))
RedirectMessage(w, r, fmt.Sprintf("/admin/plan/%d", planId), L.Success("plan_updated"))
}
type AdminRegionsParams struct {
Frame FrameParams
Regions []Region
Token string
}
func adminRegions(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
params := AdminRegionsParams{}
params.Frame = frameParams
params.Regions = regionListAll()
params.Token = CSRFGenerate(session)
RenderTemplate(w, "admin", "regions", params)
}
func adminRegionEnable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
enableRegion(mux.Vars(r)["region"])
RedirectMessage(w, r, "/admin/regions", L.Success("region_enabled"))
}
func adminRegionDisable(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
disableRegion(mux.Vars(r)["region"])
RedirectMessage(w, r, "/admin/regions", L.Success("region_disabled"))
}
type AdminImagesParams struct {
Frame FrameParams
Images []*Image
Regions []string
Token string
}
func adminImages(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
params := AdminImagesParams{}
params.Frame = frameParams
params.Images = imageListAll()
params.Regions = regionList()
params.Token = CSRFGenerate(session)
RenderTemplate(w, "admin", "images", params)
}
type AdminImagesAddForm struct {
Name string `schema:"name"`
Region string `schema:"region"`
Identification string `schema:"identification"`
}
func adminImagesAdd(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
form := new(AdminImagesAddForm)
err := decoder.Decode(form, r.PostForm)
if err != nil {
http.Redirect(w, r, "/admin/images", 303)
return
}
imageAdd(form.Name, form.Region, form.Identification)
RedirectMessage(w, r, "/admin/images", L.Success("image_added"))
}
func adminImageDelete(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
imageId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
RedirectMessage(w, r, "/admin/images", L.FormattedError("invalid_plan"))
return
}
err = imageDeleteForce(imageId)
if err != nil {
RedirectMessage(w, r, "/admin/images", L.FormatError(err))
} else {
RedirectMessage(w, r, "/admin/images", L.Success("image_deleted"))
}
}
func adminImagesAutopopulate(w http.ResponseWriter, r *http.Request, session *Session, frameParams FrameParams) {
err := imageAutopopulate(r.PostFormValue("region"))
if err != nil {
RedirectMessage(w, r, "/admin/images", L.FormatError(err))
return
} else {
RedirectMessage(w, r, "/admin/images", L.Success("image_autopopulate_success"))
}
}