summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-29 22:42:14 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-29 22:42:14 +0200
commit231ad8195ca2dde0bf560a188614bcdcc26398cc (patch)
treec5798865ebb19a8b092256bc493184d0f5613fd6
parent61f137d2cc8ae0199c99493701023b4d862a34ad (diff)
downloadmailremind-231ad8195ca2dde0bf560a188614bcdcc26398cc.tar.gz
mailremind-231ad8195ca2dde0bf560a188614bcdcc26398cc.tar.bz2
mailremind-231ad8195ca2dde0bf560a188614bcdcc26398cc.zip
Account activation done
-rw-r--r--activate.go64
-rw-r--r--main.go1
-rw-r--r--tpls.go10
-rw-r--r--tpls/master.tpl4
-rw-r--r--tpls/msg.tpl9
5 files changed, 85 insertions, 3 deletions
diff --git a/activate.go b/activate.go
new file mode 100644
index 0000000..ae392d1
--- /dev/null
+++ b/activate.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "kch42.de/gostuff/mailremind/model"
+ "log"
+ "net/http"
+)
+
+func activate(rw http.ResponseWriter, req *http.Request) {
+ outdata := &msgTpldata{Title: "Activate Account", Class: "error"}
+ defer func() {
+ if err := tplMsg.Execute(rw, outdata); err != nil {
+ log.Printf("Could not execute template in activate: %s", err)
+ }
+ }()
+
+ req.ParseForm()
+
+ _userid := req.FormValue("U")
+ code := req.FormValue("Code")
+
+ if (_userid == "") || (code == "") {
+ outdata.Msg = "User or code invalid. Check, if the activation link was correctly copied from the mail."
+ return
+ }
+
+ userid, err := db.ParseDBID(_userid)
+ if err != nil {
+ outdata.Msg = "User or code invalid. Check, if the activation link was correctly copied from the mail."
+ return
+ }
+
+ user, err := dbcon.UserByID(userid)
+ switch err {
+ case nil:
+ case model.NotFound:
+ outdata.Msg = "User not found."
+ return
+ default:
+ log.Printf("Error while getting user by ID <%s>: %s", userid, err)
+ outdata.Msg = "An error occurred while loading user data. Send a message to the support, if this happens again."
+ return
+ }
+
+ if user.ActivationCode() != code {
+ outdata.Msg = "Wrong activation code."
+ return
+ }
+
+ if err := user.SetActivationCode(""); err != nil {
+ log.Printf("Error while resetting activation code: %s", err)
+ outdata.Msg = "An error occurred while activating the user. Send a message to the support, if this happens again."
+ return
+ }
+
+ if err := user.SetActive(true); err != nil {
+ log.Printf("Error while resetting activation code: %s", err)
+ outdata.Msg = "An error occurred while activating the user. Send a message to the support, if this happens again."
+ return
+ }
+
+ outdata.Class = "success"
+ outdata.Msg = "Account activated!"
+}
diff --git a/main.go b/main.go
index 9eaebcc..4eb58c2 100644
--- a/main.go
+++ b/main.go
@@ -50,6 +50,7 @@ func main() {
router := mux.NewRouter()
router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticpath))))
router.HandleFunc("/register", register)
+ router.HandleFunc("/activate", activate)
http.Handle("/", router)
diff --git a/tpls.go b/tpls.go
index e7298fa..846bdda 100644
--- a/tpls.go
+++ b/tpls.go
@@ -16,7 +16,10 @@ func loadTpl(tplpath, name string) *template.Template {
return tpl
}
-var tplRegister *template.Template
+var (
+ tplRegister *template.Template
+ tplMsg *template.Template
+)
func initTpls() {
tplpath, err := conf.GetString("paths", "tpls")
@@ -25,4 +28,9 @@ func initTpls() {
}
tplRegister = loadTpl(tplpath, "register")
+ tplMsg = loadTpl(tplpath, "msg")
+}
+
+type msgTpldata struct {
+ Title, Class, Msg string
}
diff --git a/tpls/master.tpl b/tpls/master.tpl
index b675598..4e2b90f 100644
--- a/tpls/master.tpl
+++ b/tpls/master.tpl
@@ -1,9 +1,9 @@
<html>
<head>
- <title>{{template "title"}} – mailremind</title>
+ <title>{{template "title" .}} – mailremind</title>
</head>
<body>
- <h1>{{template "title"}}</h1>
+ <h1>{{template "title" .}}</h1>
<div class="content">
{{template "content" .}}
diff --git a/tpls/msg.tpl b/tpls/msg.tpl
new file mode 100644
index 0000000..f7e75f3
--- /dev/null
+++ b/tpls/msg.tpl
@@ -0,0 +1,9 @@
+{{define "title"}}{{.Title}}{{end}}
+
+{{define "content"}}
+ {{if .Class}}
+ <div class="{{.Class}}">{{.Msg}}</div>
+ {{else}}
+ <div>{{.Msg}}</div>
+ {{end}}
+{{end}} \ No newline at end of file