/
profile.go
174 lines (149 loc) · 3.88 KB
/
profile.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
package annote
import (
"log"
"net/http"
"regexp"
"strings"
"github.com/julienschmidt/httprouter"
)
type profilePage struct {
User *User
Message string
NewUser *User
}
func ProfileShow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
username, _, _ := r.BasicAuth()
user := FindUser(username)
DoTemplate(w, "profile", profilePage{User: user})
}
func ProfileUpdate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
username, _, _ := r.BasicAuth()
user := FindUser(username)
v := profilePage{User: user}
// figure out what is desired...
cp := r.FormValue("changepass")
if cp != "" {
Datasource.RecordEvent("ask-pw-reset", user, "")
t, err := CreateResetToken(username)
if err != nil {
v.Message = err.Error()
DoTemplate(w, "profile", v)
return
}
http.Redirect(w, r, "/reset?r="+t, 302)
return
}
// no idea. display page again
DoTemplate(w, "profile", v)
}
func ProfileEditShow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
username, _, _ := r.BasicAuth()
user := FindUser(username)
DoTemplate(w, "profile_edit", profilePage{User: user})
}
var (
orcidRE = regexp.MustCompile(`\d{4}-\d{4}-\d{4}-\d{3}[0-9X]`)
)
func ProfileEditUpdate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var err error
username, _, _ := r.BasicAuth()
user := FindUser(username)
v := profilePage{User: user}
var messages []string
newusername := r.FormValue("username")
if newusername != username {
otheruser := FindUser(newusername)
if otheruser != nil {
messages = append(messages, "Username already exists")
goto do_orcid
}
Datasource.RecordEvent("user-change-username", user, newusername)
user.Username = newusername
}
do_orcid:
orcid := r.FormValue("orcid")
if orcid != "" {
neworcid := orcidRE.FindString(orcid)
if neworcid == "" {
messages = append(messages, "no orcid in "+orcid)
goto out
}
Datasource.RecordEvent("user-update-orcid", user, "")
user.ORCID = neworcid
} else {
user.ORCID = ""
}
err = SaveUser(user)
if err != nil {
messages = append(messages, err.Error())
log.Println(err)
}
if newusername != username {
ClearUserFromCache(username)
}
out:
if len(messages) > 0 {
v.Message = strings.Join(messages, " // ")
DoTemplate(w, "profile_edit", v)
return
}
http.Redirect(w, r, "/profile", 302)
}
type NewUserData struct {
Message string
Username string
ORCID string
}
func ProfileNewShow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
DoTemplate(w, "user-new", &NewUserData{})
}
func ProfileNewPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// make sure data has been entered, if so, try to make new user
// either return to the sign-up page or redirect to the password reset page
username := r.FormValue("username")
orcid := r.FormValue("orcid")
// prepopulate this data in case there was an error and we want
// to return it to the user so it can be fixed.
v := &NewUserData{
Username: username,
ORCID: orcid,
}
// set up a deferred func so we can return early from errors
defer func() {
if v.Message != "" {
DoTemplate(w, "user-new", v)
}
}()
if username == "" {
v.Message = "A user name is required"
return
}
// normalize the ORCID identifier
if orcid != "" {
neworcid := orcidRE.FindString(orcid)
if neworcid == "" {
v.Message = "Provided ORCID is not well formed"
return
}
orcid = neworcid
}
Datasource.RecordEvent("user-create-account", &User{Username: username}, "")
// try to create this user. CreateNewUser() returns an error
// if the username already exists.
user, err := CreateNewUser(username)
if err != nil {
log.Println(err)
v.Message = err.Error()
return
}
if orcid != "" {
user.ORCID = orcid
err = SaveUser(user)
if err != nil {
log.Println(err)
v.Message = err.Error()
return
}
}
http.Redirect(w, r, "/reset?r="+user.HashedPassword, 302)
}