diff --git a/admin.go b/admin.go index ec482f5..c7be41f 100644 --- a/admin.go +++ b/admin.go @@ -6,6 +6,36 @@ import ( "strings" ) +type settingsData struct { + S Status +} + +func settingsHandler(w http.ResponseWriter, r *http.Request) { + sess := GetSession(r) + if sess.User == "" { + http.NotFound(w, r) + return + } + if r.Method == "POST" { + current_pass := r.FormValue("currpass") + pass1 := r.FormValue("password1") + pass2 := r.FormValue("password2") + switch { + case !db.UserValid(sess.User, current_pass): + sess.Notify("Password error!", "The current password given don't match with the user password. Try again", "error") + case pass1 != pass2: + sess.Notify("Passwords don't match!", "The new password and the confirmation password don't match. Try again", "error") + default: + db.SetPassword(sess.User, pass1) + sess.Notify("Password updated!", "Your new password is correctly set.", "success") + } + } + + var data settingsData + data.S = GetStatus(w, r) + loadTemplate(w, "settings", data) +} + func deleteHandler(w http.ResponseWriter, r *http.Request) { sess := GetSession(r) if sess.User == "" { diff --git a/database.go b/database.go index 9844ce9..d2d8527 100644 --- a/database.go +++ b/database.go @@ -56,9 +56,19 @@ func (d *DB) Close() { d.session.Close() } -func (d *DB) UserValid(user string, pass string) bool { +func md5Pass(pass string) []byte { h := md5.New() hash := h.Sum(([]byte)(PASS_SALT + pass)) + return hash +} + +func (d *DB) SetPassword(user string, pass string) error { + hash := md5Pass(pass) + return d.user.Update(bson.M{"user": user}, bson.M{"$set": bson.M{"pass": hash}}) +} + +func (d *DB) UserValid(user string, pass string) bool { + hash := md5Pass(pass) n, err := d.user.Find(bson.M{"user": user, "pass": hash}).Count() if err != nil { return false diff --git a/template.go b/template.go index 71c2f43..d35184f 100644 --- a/template.go +++ b/template.go @@ -33,6 +33,7 @@ var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html", TEMPLATE_PATH+"new.html", TEMPLATE_PATH+"read.html", TEMPLATE_PATH+"edit.html", + TEMPLATE_PATH+"settings.html", )) func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { diff --git a/templates/header.html b/templates/header.html index 9b230b6..aaa590c 100644 --- a/templates/header.html +++ b/templates/header.html @@ -67,6 +67,7 @@ diff --git a/templates/settings.html b/templates/settings.html new file mode 100644 index 0000000..ffa01e3 --- /dev/null +++ b/templates/settings.html @@ -0,0 +1,30 @@ +{{template "header.html" .S}} + +

Settings

+ +
+ Change your pasword +
+ +
+
+
+
+
+ +
+ +
+ +
+ +
+
+
+
+ +
+
+
+ +{{template "footer.html"}} diff --git a/trantor.go b/trantor.go index 727e11a..c9540c5 100644 --- a/trantor.go +++ b/trantor.go @@ -143,6 +143,7 @@ func main() { http.HandleFunc("/delete/", deleteHandler) http.HandleFunc("/about/", aboutHandler) http.HandleFunc("/books/", downloadHandler) + http.HandleFunc("/settings/", settingsHandler) fileHandler("/img/") fileHandler("/cover/") fileHandler("/css/")