forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_handler.go
83 lines (77 loc) · 1.72 KB
/
main_handler.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
package uadmin
import (
"net/http"
"net/url"
"strings"
)
// mainHandler is the main handler for the admin
func mainHandler(w http.ResponseWriter, r *http.Request) {
if !CheckRateLimit(r) {
w.Write([]byte("Slow down. You are going too fast!"))
return
}
if !ValidateIP(r, AllowedIPs, BlockedIPs) {
if r.Form == nil {
r.Form = url.Values{}
}
r.Form.Set("err_msg", "Your IP Address ("+r.RemoteAddr+") is not Allowed to Access this Page")
r.Form.Set("err_code", "403")
pageErrorHandler(w, r, nil)
return
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, RootURL)
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
URLParts := strings.Split(r.URL.Path, "/")
if URLParts[0] == "resetpassword" {
passwordResetHandler(w, r)
return
}
// Authentecation
// This session is preloaded with a user
session := IsAuthenticated(r)
if session == nil {
loginHandler(w, r)
return
}
// Check remote access
if !(isLocal(r.RemoteAddr) || session.User.RemoteAccess) {
if r.Form == nil {
r.Form = url.Values{}
}
r.Form.Set("err_msg", "Remote Access Denied")
pageErrorHandler(w, r, nil)
return
}
if r.URL.Path == "" {
homeHandler(w, r, session)
return
}
if len(URLParts) == 1 {
if URLParts[0] == "logout" {
logoutHandler(w, r, session)
return
}
if URLParts[0] == "export" {
exportHandler(w, r, session)
return
}
if URLParts[0] == "cropper" {
cropImageHandler(w, r, session)
return
}
if URLParts[0] == "profile" {
profileHandler(w, r, session)
return
}
if URLParts[0] == "settings" {
settingsHandler(w, r, session)
return
}
listHandler(w, r, session)
return
} else if len(URLParts) == 2 {
formHandler(w, r, session)
return
}
pageErrorHandler(w, r, session)
}