This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
87 lines (74 loc) · 2.85 KB
/
admin.go
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
79
80
81
82
83
84
85
86
87
package handler
import (
"code.google.com/p/go.crypto/bcrypt"
"github.com/pascalj/disgo/models"
"net/http"
"strconv"
"strings"
)
// AdminIndex shows the overview of the admin interface with latest comments.
func AdminIndex(w http.ResponseWriter, req *http.Request, app *App) {
qry := req.URL.Query()
page, err := strconv.Atoi(qry.Get("page"))
if err != nil {
page = 0
}
comments := paginatedComments(app.Db, page)
render(w, "admin/index", map[string]interface{}{"comments": comments}, app)
}
// GetRegister shows the register form.
func GetRegister(w http.ResponseWriter, req *http.Request, app *App) {
render(w, "admin/register", nil, app)
}
// PostUser will create a new user when no other users are in the database.
// If users are present, it will redirect to the login.
func PostUser(w http.ResponseWriter, req *http.Request, app *App) {
if models.UserCount(app.Db) == 0 {
email, password := req.FormValue("email"), req.FormValue("password")
user := models.NewUser(email, password)
err := user.Save(app.Db)
if err != nil {
http.Redirect(w, req, app.Config.General.Prefix+"/register", http.StatusFound)
} else {
http.Redirect(w, req, app.Config.General.Prefix+"/login", http.StatusFound)
}
}
}
// GetLogin shows the login form for the backend.
func GetLogin(w http.ResponseWriter, req *http.Request, app *App) {
if models.UserCount(app.Db) > 0 {
render(w, "admin/login", map[string]interface{}{"hideNav": true}, app)
} else {
http.Redirect(w, req, app.Config.General.Prefix+"/register", http.StatusSeeOther)
}
}
// PostLogin takes the email and password parameter and logs the user in if they are correct.
func PostSession(w http.ResponseWriter, req *http.Request, app *App) {
var user models.User
email, password := req.FormValue("email"), req.FormValue("password")
user, err := models.UserByEmail(app.Db, email)
if err != nil || bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) != nil {
http.Redirect(w, req, app.Config.General.Prefix+"/login", http.StatusSeeOther)
return
}
session, _ := app.SessionStore.Get(req, SessionName)
session.Values["userId"] = user.Id
session.Save(req, w)
http.Redirect(w, req, app.Config.General.Prefix+"/admin/", http.StatusSeeOther)
}
// GetIndex shows a simple introduction.
func GetIndex(w http.ResponseWriter, req *http.Request, app *App) {
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
base := []string{scheme, "://", req.Host, app.Config.General.Prefix}
render(w, "index", map[string]interface{}{"base": strings.Join(base, ""), "hideNav": true}, app)
}
// PostLogout logs the user out and redirects to the login page.
func PostLogout(w http.ResponseWriter, req *http.Request, app *App) {
session, _ := app.SessionStore.Get(req, SessionName)
session.Values["userId"] = nil
session.Save(req, w)
http.Redirect(w, req, app.Config.General.Prefix+"/login", http.StatusFound)
}