forked from ZJGSU-Open-Source/GoOnlineJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
110 lines (103 loc) · 2.47 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
package main
import (
"GoOnlineJudge/config"
"GoOnlineJudge/model"
"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 {
ctx.Error("Error occured", http.StatusForbidden)
return true
}
if prt < config.PrivilegeTC {
ctx.Error("No privilege", http.StatusForbidden)
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"
}
ctx.Error(info, http.StatusForbidden)
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 {
ctx.Error(
"Sorry, the contest is private and you are not granted to participate in the contest.", http.StatusForbidden)
return true
}
}
}
}
return false
}