-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofile.go
More file actions
174 lines (154 loc) · 4.17 KB
/
profile.go
File metadata and controls
174 lines (154 loc) · 4.17 KB
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 main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"github.com/gorilla/csrf"
)
type ProfileData struct {
Base BaseTemplate
Profile *User
Users []User
Highlight string
CsrfField template.HTML
}
func updateProfile(ctx context.Context, r *http.Request, data *ProfileData, user *User) error {
form := r.Form
var id string
if user.Superuser && len(form["edit"]) > 0 {
id = form["edit"][0]
} else {
id = user.ID.Name
}
if user.ID.Name != id && !user.Superuser {
return fmt.Errorf("User %v can't edit %v", user, id)
}
profile, err := getUser(ctx, id)
if err != nil {
return fmt.Errorf("Can't find user %v: %v", id, err)
}
// Make sure the form is filled out
for _, f := range []string{"name", "location"} {
if len(form[f]) == 0 {
return fmt.Errorf("Missing form field %s", f)
}
}
var changes []string
if profile.Name != form["name"][0] {
changes = append(changes,
fmt.Sprintf("%s: %q -> %q", "Name", profile.Name, form["name"][0]))
profile.Name = form["name"][0]
}
if profile.DefaultLocation != form["location"][0] {
changes = append(changes,
fmt.Sprintf("%s: %q -> %q", "Location", profile.DefaultLocation, form["location"][0]))
profile.DefaultLocation = form["location"][0]
}
if profile.Emails != (len(form["nag"]) > 0) {
changes = append(changes,
fmt.Sprintf("%s: %v -> %v", "Nag", profile.Emails, !profile.Emails))
profile.Emails = len(form["nag"]) > 0
}
if profile.Invite != (len(form["invite"]) > 0) {
changes = append(changes,
fmt.Sprintf("%s: %v -> %v", "Invite", profile.Invite, !profile.Invite))
profile.Invite = len(form["invite"]) > 0
}
if profile.Notify != (len(form["notify"]) > 0) {
changes = append(changes,
fmt.Sprintf("%s: %v -> %v", "Notify", profile.Notify, !profile.Notify))
profile.Notify = len(form["notify"]) > 0
}
if devServer(ctx) {
if !user.Superuser && profile.ID.ID == user.ID.ID {
changes = append(changes, "Auto-grant superuser in dev server")
profile.Superuser = true
}
}
if len(changes) > 0 {
if devServer(ctx) {
log.Printf("Changes to %s:\n", profile.ID.Name)
for _, c := range changes {
log.Print(c)
}
}
_, err := dsClient.Put(ctx, profile.ID, profile)
if err != nil {
return fmt.Errorf("Error updating user: %v", err)
}
data.Base.Msg = "Profile updated"
}
return nil
}
func handleProfile(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user, _ := getUserSession(ctx, r)
data := &ProfileData{
Profile: user,
Base: BaseTemplate{
Tab: "profile",
User: user,
DevServer: devServer(ctx),
},
CsrfField: csrf.TemplateField(r),
}
tmpl := template.Must(template.ParseFiles(
"templates/base.html",
"templates/profile.html"))
// Parse form data
if err := r.ParseForm(); err != nil {
data.Base.Error = fmt.Sprintf("Error parsing form: %v", err)
tmpl.ExecuteTemplate(w, "invite.html", data)
return
}
if hi := r.FormValue("hi"); hi != "" {
data.Highlight = hi
}
if r.Method == http.MethodPost {
err := updateProfile(ctx, r, data, user)
if err != nil {
if data.Base.Error == "" {
data.Base.Error = "Internal error"
}
log.Printf("Error processing POST: %v", err)
tmpl.ExecuteTemplate(w, "profile.html", data)
return
}
// Redirect to clear form
if user.Superuser {
var pid = user.ID.Name
if r.FormValue("edit") != "" {
pid = r.FormValue("edit")
}
http.Redirect(w, r,
fmt.Sprintf("/profile?edit=%s&msg=%s", url.QueryEscape(pid), url.QueryEscape(data.Base.Msg)), http.StatusFound)
} else {
http.Redirect(w, r, "/profile?msg="+url.QueryEscape(data.Base.Msg), http.StatusFound)
}
return
}
data.Base.Msg = r.FormValue("msg")
if user.Superuser {
pid := r.FormValue("edit")
if pid != "" {
profile, err := getUser(ctx, pid)
if err != nil {
log.Printf("Error loading user %q: %v", pid, err)
data.Base.Error = "Can't load user"
}
data.Profile = profile
}
users, err := getAllUsers(ctx)
if err != nil {
log.Print(err)
data.Base.Error = "Error querying users"
}
data.Users = users
}
if err := tmpl.ExecuteTemplate(w, "profile.html", data); err != nil {
log.Printf("Error executing profile.html: %v", err)
}
}