-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
113 lines (99 loc) · 2.32 KB
/
settings.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
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
)
func settings(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
cookies := r.Cookies()
var tokenCookie *http.Cookie
for _, cookie := range cookies {
if cookie.Name == "token" {
tokenCookie = cookie
break
}
}
if tokenCookie == nil {
http.Redirect(w, r, "/register", http.StatusSeeOther)
return
}
buf, err := pages.ReadFile("pages/settings.html")
if err != nil {
logError(err)
giveError(w, err)
return
}
var body string = string(buf)
tmpl, err := template.New("settings").Parse(body)
if err != nil {
logError(err)
giveError(w, err)
return
}
tmpl.Execute(w, nil)
case http.MethodPost:
buf, err := pages.ReadFile("pages/settings.html")
if err != nil {
logError(err)
giveError(w, err)
return
}
var body string = string(buf)
tmpl, err := template.New("settings").Parse(body)
if err != nil {
logError(err)
giveError(w, err)
return
}
cookies := r.Cookies()
var tokenCookie *http.Cookie
for _, cookie := range cookies {
if cookie.Name == "token" {
tokenCookie = cookie
break
}
}
if tokenCookie == nil {
http.Redirect(w, r, "/register", http.StatusSeeOther)
return
}
response := struct {
Message string `json:"Message"`
}{}
if err := r.ParseForm(); err != nil {
logError(err)
giveError(w, err)
return
}
user, err := userFromToken(tokenCookie.Value, permSystem)
if err != nil {
logError(err)
giveError(w, err)
return
}
if r.FormValue("username") != "" {
_, err := idFromUsername(r.FormValue("username"))
if err == sql.ErrNoRows {
user.Username = r.FormValue("username")
} else {
response.Message = "That username already exists."
tmpl.Execute(w, response)
return
}
}
if r.FormValue("password") != "" {
if len(r.FormValue("password")) < minimumPasswordLength || len(r.FormValue("password")) > maximumPasswordLength {
response.Message = fmt.Sprintf("Password minimum length: %s, Maximum length: %s\n", fmt.Sprint(minimumPasswordLength), fmt.Sprint(maximumPasswordLength))
tmpl.Execute(w, response)
return
}
user.Password = r.FormValue("password")
}
updateFromId(user.Id, user)
response.Message = "Updated."
tmpl.Execute(w, response)
}
}