-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgrabc.go
92 lines (76 loc) · 2.6 KB
/
grabc.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
package grabc
import (
"github.com/astaxie/beego"
"github.com/codyi/grabc/controllers"
"github.com/codyi/grabc/libs"
"github.com/codyi/grabc/models"
"os"
"path/filepath"
)
//init function
func init() {
var c []beego.ControllerInterface
c = append(c, &controllers.RouteController{}, &controllers.RoleController{}, &controllers.PermissionController{}, &controllers.AssignmentController{}, &controllers.MenuController{})
for _, v := range c {
//将路由注册到beego
beego.AutoRouter(v)
//将路由注册到grabc
RegisterController(v)
}
//设置grabc默认视图、layout路径
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err.Error())
}
SetViewPath(filepath.Dir(dir) + "/github.com/codyi/grabc/views/")
SetLayout("main.html", filepath.Dir(dir)+"/github.com/codyi/grabc/views/layout/")
}
//注册需要检查的routes
func RegisterController(controllers ...beego.ControllerInterface) {
for _, controller := range controllers {
libs.RegisterControllers = append((libs.RegisterControllers), controller)
}
}
//注册用于获取登录用户ID的函数
func RegisterUserIdFunc(f func(c *beego.Controller) int) {
libs.RegisterUserIdFunc = f
}
//注册当前请求beego的Controller
func SetBeegoController(c *beego.Controller) {
libs.BeegoC = c
}
//注册用户的模型表,用于获取用户的id和用户名称
func RegisterUserModel(m models.IUserModel) {
models.UserModel = &m
}
//增加忽律检查的地址,例如site/login,这个就不需要检查权限
func AppendIgnoreRoute(controllerName, actionName string) {
if libs.IgnoreRoutes[controllerName] == nil {
libs.IgnoreRoutes[controllerName] = make([]string, 0)
}
libs.IgnoreRoutes[controllerName] = append(libs.IgnoreRoutes[controllerName], actionName)
}
//权限检查
func CheckAccess(controllerName, routeName string) bool {
return libs.CheckAccess(controllerName, routeName, libs.AccessRoutes())
}
//设置grabc的模板
func SetLayout(layoutName string, layoutPath string) {
libs.Template.LayoutName = layoutName
libs.Template.LayoutPath = layoutPath
beego.AddViewPath(libs.Template.LayoutPath)
}
//设置grabc的模板的数据
func AddLayoutData(name string, value interface{}) {
if libs.BeegoC.GetSession("grabc_layout_data") == nil {
libs.BeegoC.SetSession("grabc_layout_data", make(map[string]interface{}, 0))
}
data := libs.BeegoC.GetSession("grabc_layout_data").(map[string]interface{})
data[name] = value
libs.BeegoC.SetSession("grabc_layout_data", data)
}
//设置grabc模板的路径
func SetViewPath(path string) {
libs.Template.ViewPath = path
beego.AddViewPath(libs.Template.ViewPath)
}