-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadmin.go
More file actions
100 lines (91 loc) · 2.58 KB
/
Copy pathadmin.go
File metadata and controls
100 lines (91 loc) · 2.58 KB
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
// Admin is a series of controllers to manage a Mongolar site
package admin
import (
"github.com/mongolar/mongolar/controller"
"github.com/mongolar/mongolar/services"
"github.com/mongolar/mongolar/user"
"github.com/mongolar/mongolar/wrapper"
"net/http"
)
// Admin Map for controllers
type AdminMap controller.ControllerMap
// Structure of url links that appear on the admin page.
func (a *AdminMenu) AdminMenu(w *wrapper.Wrapper) {
w.SetContent(a)
w.Serve()
return
}
// Package function to return controller map
func GetControllerMap(cm controller.ControllerMap) {
amap, _ := NewAdmin()
cm["admin"] = amap.Admin
}
// A series of menu items to render on the admin page
type AdminMenu struct {
MenuItems []map[string]string `json:"menu_items"`
}
// Build a new admin instance and return AdminMap and menu to be altered
func NewAdmin() (*AdminMap, *AdminMenu) {
amenu := AdminMenu{
MenuItems: []map[string]string{
map[string]string{"title": "Home", "template": "admin/main_content_default.html"},
map[string]string{"title": "Content", "template": "admin/content_editor.html"},
map[string]string{"title": "Content Types", "template": "admin/content_types_editor.html"},
},
}
amap := &AdminMap{
"admin_menu": amenu.AdminMenu,
"paths": AdminPaths,
"path_elements": PathElements,
"path_editor": PathEditor,
"element": Element,
"element_editor": ElementEditor,
"add_child": AddChild,
"add_existing_child": AddExistingChild,
"all_content_types": GetAllContentTypes,
"edit_content_type": EditContentType,
"delete": Delete,
"sort_children": Sort,
"content": ContentEditor,
"menu": MenuEditor,
"content_type": ContentTypeEditor,
"orphans": OrphanElements,
"slug_url_editor": SlugUrlEditor,
}
return amap, &amenu
}
//Main controller for all admin functions
func (a AdminMap) Admin(w *wrapper.Wrapper) {
if c, ok := a[w.APIParams[0]]; ok {
if validateAdmin(w) {
w.Shift()
c(w)
}
return
} else {
http.Error(w.Writer, "Forbidden", 403)
return
}
}
// Validate admin has admin role.
func validateAdmin(w *wrapper.Wrapper) bool {
user := new(user.User)
err := user.Get(w)
loginurls := make(map[string]string)
w.SiteConfig.RawConfig.MarshalKey("LoginURLs", &loginurls)
if err != nil {
services.Redirect(loginurls["login"], w)
w.Serve()
return false
}
if user.Roles != nil {
for _, r := range user.Roles {
if r == "admin" {
return true
}
}
}
services.Redirect(loginurls["access_denied"], w)
w.Serve()
return false
}