-
Notifications
You must be signed in to change notification settings - Fork 4
/
admin_users.go
115 lines (107 loc) · 3.21 KB
/
admin_users.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"errors"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// handleAdminDoLogin
// Verify the provided credentials, set up a cookie (if requested)
// and redirect back to /admin
// TODO: Set up the cookie
func handleAdminDoLogin(w http.ResponseWriter, req *http.Request) {
page := initAdminRequest(w, req)
// Fetch the login credentials
email := req.FormValue("email")
password := req.FormValue("password")
if err := doLogin(email, password); err != nil {
page.session.setFlashMessage("Invalid Login", "error")
} else {
page.session.setStringValue("email", email)
}
redirect("/admin", w, req)
}
// doLogin attempts to log in with the given email/password
// If it can't, it returns an error
func doLogin(email, password string) error {
if strings.TrimSpace(email) != "" && strings.TrimSpace(password) != "" {
return m.checkCredentials(email, password)
}
return errors.New("Invalid Credentials")
}
// handleAdminDoLogout
// Expire the session
func handleAdminDoLogout(w http.ResponseWriter, req *http.Request) {
page := initAdminRequest(w, req)
page.session.expireSession()
page.session.setFlashMessage("Logged Out", "success")
redirect("/admin", w, req)
}
// handleAdminUsers
func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
page.SubTitle = "Admin Users"
email := vars["id"]
if email == "new" {
switch vars["function"] {
case "save":
email = req.FormValue("email")
if m.isValidUserEmail(email) {
// User already exists
page.session.setFlashMessage("A user with email address "+email+" already exists!", "error")
} else {
password := req.FormValue("password")
if err := m.updateUserPassword(email, string(password)); err != nil {
page.session.setFlashMessage(err.Error(), "error")
} else {
page.session.setFlashMessage("User "+email+" created!", "success")
}
}
redirect("/admin/users", w, req)
default:
page.SubTitle = "Add Admin User"
page.show("admin-adduser.html", w)
}
} else if email != "" {
switch vars["function"] {
case "save":
var err error
if m.isValidUserEmail(email) {
password := req.FormValue("password")
if password != "" {
if err = m.updateUserPassword(email, password); err != nil {
page.session.setFlashMessage(err.Error(), "error")
} else {
page.session.setFlashMessage("User "+email+" created!", "success")
}
}
redirect("/admin/users", w, req)
}
case "delete":
var err error
if m.isValidUserEmail(email) {
if err = m.deleteUser(email); err != nil {
page.session.setFlashMessage(err.Error(), "error")
} else {
page.session.setFlashMessage("User "+email+" deleted!", "success")
}
}
redirect("/admin/users", w, req)
default:
page.SubTitle = "Edit Admin User"
if !m.isValidUserEmail(email) {
page.session.setFlashMessage("Couldn't find the requested user, please try again.", "error")
redirect("/admin/users", w, req)
}
page.TemplateData = email
page.show("admin-edituser.html", w)
}
} else {
type usersPageData struct {
Users []string
}
page.TemplateData = usersPageData{Users: m.getAllUsers()}
page.SubTitle = "Admin Users"
page.show("admin-users.html", w)
}
}