summaryrefslogtreecommitdiff
path: root/activate.go
blob: 59e7eed9d8e90fcb903ab8a547b96b6959a9cf5b (plain)
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
package main

import (
	"github.com/gorilla/sessions"
	"kch42.de/gostuff/mailremind/model"
	"log"
	"net/http"
)

func activate(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) {
	outdata := &msgTpldata{Title: "Activate Account", Class: "error"}

	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 outdata, user
	}

	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 outdata, user
	}

	switch user, err = dbcon.UserByID(userid); err {
	case nil:
	case model.NotFound:
		outdata.Msg = "User not found."
		return outdata, user
	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 outdata, user
	}

	if user.ActivationCode() != code {
		outdata.Msg = "Wrong activation code."
		return outdata, user
	}

	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 outdata, user
	}

	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, user
	}

	outdata.Class = "success"
	outdata.Msg = "Account activated!"
	return outdata, user
}