summaryrefslogtreecommitdiff
path: root/settings.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-09-12 22:40:52 +0200
committerKevin Chabowski <kevin@kch42.de>2013-09-12 22:40:52 +0200
commit0fe336b08f7868d0f7b531cb30c9facfea0f25f2 (patch)
tree0cb6a8aee4b3f0389c48d800b19f444f3aea4016 /settings.go
parentb0e40b8764eab7c40b3f5e6f994af3c423b92dfc (diff)
downloadmailremind-0fe336b08f7868d0f7b531cb30c9facfea0f25f2.tar.gz
mailremind-0fe336b08f7868d0f7b531cb30c9facfea0f25f2.tar.bz2
mailremind-0fe336b08f7868d0f7b531cb30c9facfea0f25f2.zip
Added settings page
Diffstat (limited to 'settings.go')
-rw-r--r--settings.go79
1 files changed, 79 insertions, 0 deletions
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
+}