diff options
-rw-r--r-- | main.go | 1 | ||||
-rw-r--r-- | settings.go | 79 | ||||
-rw-r--r-- | tpls.go | 2 | ||||
-rw-r--r-- | tpls/master.tpl | 1 | ||||
-rw-r--r-- | tpls/settings.tpl | 32 |
5 files changed, 115 insertions, 0 deletions
@@ -94,6 +94,7 @@ func main() { router.HandleFunc("/jobs", mkHttpHandler(jobs, tplJobs)) router.HandleFunc("/jobedit", mkHttpHandler(jobedit, tplJobedit)) router.HandleFunc("/jobedit/{ID}", mkHttpHandler(jobedit, tplJobedit)) + router.HandleFunc("/settings", mkHttpHandler(settings, tplSettings)) http.Handle("/", router) diff --git a/settings.go b/settings.go new file mode 100644 index 0000000..a10b901 --- /dev/null +++ b/settings.go @@ -0,0 +1,79 @@ +package main + +import ( + "code.google.com/p/go.crypto/bcrypt" + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/model" + "log" + "net/http" + "time" +) + +type settingsTpldata struct { + Success, Error string + Fatal bool + Timezones map[string]bool +} + +func settings(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) { + if user == nil { + return &settingsTpldata{Error: "You need to be logged in to do that.", Fatal: true}, nil + } + + outdata := &settingsTpldata{Timezones: make(map[string]bool)} + tznow := user.Location().String() + for _, tz := range timeLocs { + outdata.Timezones[tz] = (tz == tznow) + } + + if req.Method != "POST" { + return outdata, user + } + + if err := req.ParseForm(); err != nil { + outdata.Error = "Could not parse form" + return outdata, user + } + + switch req.FormValue("M") { + case "setpasswd": + if req.FormValue("Password") == "" { + outdata.Error = "Password must not be empty." + return outdata, user + } + + if req.FormValue("Password") != req.FormValue("RepeatPassword") { + outdata.Error = "Passwords must be equal." + return outdata, user + } + + hash, err := bcrypt.GenerateFromPassword([]byte(req.FormValue("Password")), bcrypt.DefaultCost) + if err != nil { + log.Printf("Error hashing password: %s", err) + outdata.Error = "Error while hashing password data." + return outdata.Error, user + } + + if err := user.SetPWHash(hash); err != nil { + log.Printf("Error setting pwhash: %s", err) + outdata.Error = "Could not save new password." + } else { + outdata.Success = "Password changed" + } + case "settimezone": + loc, err := time.LoadLocation(req.FormValue("Timezone")) + if err != nil { + outdata.Error = "Unknown Timezone" + return outdata, user + } + + if err := user.SetLocation(loc); err != nil { + log.Printf("Error setting location: %s", err) + outdata.Error = "Could not save new timezone." + } else { + outdata.Success = "New timezone saved." + } + } + + return outdata, user +} @@ -25,6 +25,7 @@ var ( tplForgotpw *template.Template tplJobs *template.Template tplJobedit *template.Template + tplSettings *template.Template ) func initTpls() { @@ -41,6 +42,7 @@ func initTpls() { tplForgotpw = loadTpl(tplpath, "forgotpw") tplJobs = loadTpl(tplpath, "jobs") tplJobedit = loadTpl(tplpath, "jobedit") + tplSettings = loadTpl(tplpath, "settings") } type msgTpldata struct { diff --git a/tpls/master.tpl b/tpls/master.tpl index a79f5c6..7d6a249 100644 --- a/tpls/master.tpl +++ b/tpls/master.tpl @@ -13,6 +13,7 @@ {{if .Mail}} <li><a href="/jobedit">new job</a></li> <li><a href="/jobs">list jobs</a></li> + <li><a href="/settings">settings</a></li> <li><a href="/logout">logout</a></li> {{else}} <li><a href="/register">register</a></li> diff --git a/tpls/settings.tpl b/tpls/settings.tpl new file mode 100644 index 0000000..605a596 --- /dev/null +++ b/tpls/settings.tpl @@ -0,0 +1,32 @@ +{{define "title"}}User Settings{{end}} + +{{define "content"}} + {{if .Error}}<div class="error">{{.Error}}</div>{{end}} + + {{if not .Fatal}} + {{if .Success}}<div class="success">{{.Success}}</div>{{end}} + + <form action="/settings?M=setpasswd" method="post" accept-charset="UTF-8"> + <h2>Set Password</h2> + + <p><strong>Password:</strong> <input type="password" name="Password" /></p> + <p><strong>Repeat Password:</strong> <input type="password" name="RepeatPassword" /></p> + <p><input type="submit" /></p> + </form> + + <form action="/settings?M=settimezone" method="post" accept-charset="UTF-8"> + <h2>Set Timezone</h2> + + <p> + <strong>Timezone:</strong> + <select size="0" name="Timezone"> + {{range $tz, $active := .Timezones}}<option value="{{$tz}}"{{if $active}} selected="selected"{{end}}>{{$tz}}</option>{{end}} + </select> + </p> + <p><input type="submit" /></p> + </form> + + <h2>Delete Account</h2> + <p><a href="/delete-acc">Delete Account</a></p> + {{end}} +{{end}}
\ No newline at end of file |