summaryrefslogtreecommitdiff
path: root/app/controllers/app.go
blob: 87c8bd13f21bd3c3e82b4cc807f5a3cd366a876e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package controllers

import "github.com/revel/revel"

type App struct {
	*revel.Controller
}

func (c App) Index() revel.Result {
	return c.Render()
}

func (c App) Account() revel.Result {
	return c.Render()
}

func (c App) PrintLogin() revel.Result {
	return c.Render()
}

func (c App) Login(login, password string) revel.Result {

	c.Validation.Required(login).Message("Please provide a user name or email.")
	c.Validation.Required(password).Message("Please enter a password.")

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(App.PrintLogin)
	}

	u := User{}
	db.Where("name = ?", login).Or("email = ?", login).Find(&u)

	c.Validation.Required(VerifyPassword(u.Password, password)).Message("User / Password combination did not matched.")
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(App.PrintLogin)
	}

	c.Session["login"] = "true"
	c.Session["uid"] = string(u.Id)

	return c.Redirect(App.Account)
}

func (c App) Register(email, confirmEmail, user, password, confirmPassword string) revel.Result {
	c.Validation.Required(email).Message("Please provide a mail adress.")
	c.Validation.Required(email == confirmEmail).Message("The mail adresses do not match.")
	c.Validation.Required(user).Message("Please provide a user name.")

	if password != "" {
		c.Validation.Required(password == confirmPassword).Message("Passwords do not match.")
	}

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(App.PrintLogin)
	}

	p, _ := HashPassword(password)

	user := User{
		Name:     user,
		Email:    email,
		Password: p,
	}

	db.NewRecord(u)
	db.Create(&u)
	db.Save(&u)

	c.Flash.Success("Registration completed.")

	return c.Redirect(App.Account)
}