/
main.go
157 lines (128 loc) · 5.48 KB
/
main.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
package randomization
import (
"fmt"
"html/template"
"net/http"
"strings"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
)
var tmpl = template.Must(template.ParseGlob("html_templates/*.html"))
func init() {
http.HandleFunc("/", informationPage)
http.HandleFunc("/dashboard", requireLogin(dashboard))
// Project creation pages
http.HandleFunc("/create_project_step1", requireLogin(createProjectStep1))
http.HandleFunc("/create_project_step2", requireLogin(createProjectStep2))
http.HandleFunc("/create_project_step3", requireLogin(createProjectStep3))
http.HandleFunc("/create_project_step4", requireLogin(createProjectStep4))
http.HandleFunc("/create_project_step5", requireLogin(createProjectStep5))
http.HandleFunc("/create_project_step6", requireLogin(createProjectStep6))
http.HandleFunc("/create_project_step7", requireLogin(createProjectStep7))
http.HandleFunc("/create_project_step8", requireLogin(createProjectStep8))
http.HandleFunc("/create_project_step9", requireLogin(createProjectStep9))
// Copy project pages
http.HandleFunc("/copy_project", requireLogin(copyProject))
http.HandleFunc("/copy_project_completed", requireLogin(copyProjectCompleted))
// Project deletion pages
http.HandleFunc("/delete_project_step1", requireLogin(deleteProjectStep1))
http.HandleFunc("/delete_project_step2", requireLogin(deleteProjectStep2))
http.HandleFunc("/delete_project_step3", requireLogin(deleteProjectStep3))
http.HandleFunc("/project_dashboard", requireLogin(projectDashboard))
http.HandleFunc("/edit_sharing", requireLogin(editSharing))
http.HandleFunc("/edit_sharing_confirm", requireLogin(editSharingConfirm))
// Treatment assignment pages
http.HandleFunc("/assign_treatment_input", requireLogin(assignTreatmentInput))
http.HandleFunc("/assign_treatment_confirm", requireLogin(assignTreatmentConfirm))
http.HandleFunc("/assign_treatment", requireLogin(assignTreatment))
http.HandleFunc("/view_statistics", requireLogin(viewStatistics))
http.HandleFunc("/view_comments", requireLogin(viewComments))
http.HandleFunc("/add_comment", requireLogin(addComment))
http.HandleFunc("/confirm_add_comment", requireLogin(confirmAddComment))
http.HandleFunc("/view_complete_data", requireLogin(viewCompleteData))
// Remove subject pages
http.HandleFunc("/remove_subject", requireLogin(removeSubject))
http.HandleFunc("/remove_subject_confirm", requireLogin(removeSubjectConfirm))
http.HandleFunc("/remove_subject_completed", requireLogin(removeSubjectCompleted))
// Edit assignment pages
http.HandleFunc("/edit_assignment", requireLogin(editAssignment))
http.HandleFunc("/edit_assignment_confirm", requireLogin(editAssignmentConfirm))
http.HandleFunc("/edit_assignment_completed", requireLogin(editAssignmentCompleted))
// Close or open project for enrollment pages
http.HandleFunc("/openclose_project", requireLogin(openCloseProject))
http.HandleFunc("/openclose_completed", requireLogin(openCloseCompleted))
}
// checkAccess determines whether the given user has permission to
// access the given project.
func checkAccess(ctx context.Context, user *user.User, pkey string, w *http.ResponseWriter, r *http.Request) bool {
userName := strings.ToLower(user.String())
keyparts := strings.Split(pkey, "::")
owner := keyparts[0]
// A user can always access his or her own projects.
if userName == strings.ToLower(owner) {
return true
}
// Otherwise, check if the project is shared with the user.
key := datastore.NewKey(ctx, "SharingByUser", userName, 0, nil)
var sbuser SharingByUser
err := datastore.Get(ctx, key, &sbuser)
if err == datastore.ErrNoSuchEntity {
checkAccessFailed(ctx, nil, w, r, user)
return false
} else if err != nil {
checkAccessFailed(ctx, &err, w, r, user)
return false
}
L := cleanSplit(sbuser.Projects, ",")
for _, x := range L {
if pkey == x {
return true
}
}
checkAccessFailed(ctx, nil, w, r, user)
return false
}
// checkAccessFailed displays an error message when a project cannot be accessed.
func checkAccessFailed(ctx context.Context, err *error, w *http.ResponseWriter, r *http.Request, user *user.User) {
if err != nil {
msg := "A datastore error occured. Ask the administrator to check the log for error details."
log.Errorf(ctx, "check_access_failed: %v", err)
rmsg := "Return to dashboard"
messagePage(*w, r, user, msg, rmsg, "/dashboard")
return
}
msg := "You don't have access to this project."
rmsg := "Return to dashboard"
log.Infof(ctx, fmt.Sprintf("Failed access: %v\n", user))
messagePage(*w, r, user, msg, rmsg, "/dashboard")
}
type handler func(http.ResponseWriter, *http.Request)
// requireLogin is a wrapper for a function that serves web pages.
// By wrapping the function in require_login, the user is forced to
// log in to their Google account in order to access the system.
func requireLogin(H handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// Force the person to log in.
client := user.Current(ctx)
if client == nil {
U := strings.Split(r.URL.String(), "/")
V := U[len(U)-1]
url, err := user.LoginURL(ctx, V)
if err != nil {
http.Error(w, "Error",
http.StatusInternalServerError)
return
}
log.Infof(ctx, fmt.Sprintf("Login url: %s", url))
msg := "To use this site, you must be logged into your Google account."
rmsg := "Continue to login page"
messagePage(w, r, nil, msg, rmsg, url)
return
}
H(w, r)
}
}