forked from jmoiron/monet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
82 lines (68 loc) · 1.94 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
package app
import (
"github.com/gorilla/sessions"
"github.com/hoisie/web"
"github.com/jmoiron/monet/conf"
"github.com/jmoiron/monet/template"
)
type AdminPanel interface {
Render() string
}
var Panels = []AdminPanel{}
func AddPanel(p AdminPanel) {
Panels = append(Panels, p)
}
var adminBase = template.Base{Path: "admin/base.mandira"}
var CookieStore = sessions.NewCookieStore([]byte(conf.Config.SessionSecret))
func AttachAdmin(url string) {
// auth
web.Get(url+"login/", login)
web.Post(url+"login/", login)
web.Get(url+"logout/", logout)
// users
/* too much unnecessary work?
web.Get(url + "users/", userList)
web.Get(url + "users/edit/(.*)", userEdit)
web.Get(url + "users/delete/(.*)", userDelete)
web.Get(url + "users/add/", userAdd)
web.Post(url + "users/add/", userAddPost)
*/
web.Get(url, adminIndex)
}
// Use this on all admin views to ensure the request is authenticated
func RequireAuthentication(ctx *web.Context) bool {
session, _ := CookieStore.Get(ctx.Request, "monet-session")
if session.Values["authenticated"] != true {
ctx.Redirect(302, "/admin/login/")
return true
}
return false
}
// views
func login(ctx *web.Context) string {
if ctx.Params != nil {
p := ctx.Params
if ValidateUser(p["username"], p["password"]) {
session, _ := CookieStore.Get(ctx.Request, "monet-session")
session.Values["authenticated"] = true
session.Save(ctx.Request, ctx.ResponseWriter)
ctx.Redirect(302, "/admin/")
}
}
return adminBase.Render("admin/login.mandira", ctx.Params, M{"login": true})
}
func logout(ctx *web.Context) string {
session, _ := CookieStore.Get(ctx.Request, "monet-session")
session.Values["authenticated"] = false
session.Save(ctx.Request, ctx.ResponseWriter)
ctx.Redirect(302, "/admin/login/")
return ""
}
func adminIndex(ctx *web.Context) string {
if RequireAuthentication(ctx) {
return ""
}
return adminBase.Render("admin/index.mandira", M{
"Panels": Panels,
})
}