forked from ZJGSU-Open-Source/GoOnlineJudge
/
filter.go
132 lines (122 loc) · 3.27 KB
/
filter.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
package main
import (
"GoOnlineJudge/config"
"GoOnlineJudge/model"
"html/template"
"net/http"
"restweb"
"strconv"
"strings"
"time"
)
func requireAdmin(ctx *restweb.Context) bool {
uid := ctx.GetSession("Uid")
if uid == "" {
ctx.Redirect("/sess", http.StatusFound)
return true
}
prv := ctx.GetSession("Privilege")
prt, err := strconv.Atoi(prv)
if err != nil {
Err400(ctx.W, "Admin", "Error occured")
return true
}
if prt < config.PrivilegeTC {
Err400(ctx.W, "Admin", "No privilege")
return true
}
return false
}
func requireLogin(ctx *restweb.Context) bool {
uid := ctx.GetSession("Uid")
if uid == "" {
if ctx.R.Method == restweb.GET {
ctx.Redirect("/sess", http.StatusFound)
} else {
ctx.W.WriteHeader(http.StatusUnauthorized)
}
return true
}
return false
}
func requireContest(ctx *restweb.Context) bool {
url := ctx.R.URL.Path
if restweb.GetAction(url, 2) == "password" {
return false
}
Cid := restweb.GetAction(url, 1)
cid, err := strconv.Atoi(Cid)
if err != nil {
return true
}
contestModel := model.ContestModel{}
ContestDetail, err := contestModel.Detail(cid)
if err != nil {
return true
}
prvs := "0" + ctx.GetSession("Privilege")
prv, err := strconv.Atoi(prvs)
if err != nil {
restweb.Logger.Debug(err)
return true
}
Uid := ctx.GetSession("Uid")
restweb.Logger.Debug(Uid, prv)
if prv < config.PrivilegeTC {
if time.Now().Unix() < ContestDetail.Start || ContestDetail.Status == config.StatusReverse {
info := "The contest has not started yet"
if ContestDetail.Status == config.StatusReverse {
info = "No such contest"
}
Err400(ctx.W, "Contest", info)
return true
} else if ContestDetail.Encrypt == config.EncryptPW {
if Uid == "" {
ctx.Redirect("/sess", http.StatusFound)
} else if ctx.GetSession(Cid+"pass") != ContestDetail.Argument.(string) {
ctx.Redirect("/contests/"+Cid+"/password", http.StatusFound)
} else {
return false
}
return true
} else if ContestDetail.Encrypt == config.EncryptPT {
if Uid == "" {
ctx.Redirect("/sess", http.StatusFound)
return true
} else {
userlist := strings.Split(ContestDetail.Argument.(string), "\r\n")
flag := false
for _, user := range userlist {
if user == Uid {
flag = true
break
}
}
if flag == false {
Err400(ctx.W, "Contest",
"Sorry, the contest is private and you are not granted to participate in the contest.")
return true
}
}
}
}
return false
}
func Err400(w http.ResponseWriter, title string, info string) {
Output := make(map[string]interface{})
Output["Title"] = title
Output["Info"] = info
t, err := template.ParseFiles("view/layout.tpl", "view/400.tpl")
if err == nil {
err = t.Execute(w, Output)
}
}
func init() {
restweb.RegisterFilters(restweb.ANY, `^/admin`, restweb.Before, requireAdmin)
restweb.RegisterFilters(restweb.POST, `^/problems/\d+`, restweb.Before, requireLogin)
restweb.RegisterFilters(restweb.ANY, `^/account`, restweb.Before, requireLogin)
restweb.RegisterFilters(restweb.GET, `^/(settings|profile)`, restweb.Before, requireLogin)
restweb.RegisterFilters(restweb.POST, `^/users/\w+`, restweb.Before, requireLogin)
restweb.RegisterFilters(restweb.ANY, `^/contests/\d+`, restweb.Before, requireContest)
restweb.AddFile("/static/", ".")
}