-
Notifications
You must be signed in to change notification settings - Fork 189
/
route_handler.go
61 lines (48 loc) · 1.37 KB
/
route_handler.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
package admin
import (
"strings"
"github.com/qor/qor"
"github.com/qor/roles"
)
// RouteConfig config for admin routes
type RouteConfig struct {
Resource *Resource
Permissioner HasPermissioner
PermissionMode roles.PermissionMode
Values map[interface{}]interface{}
}
type requestHandler func(c *Context)
type routeHandler struct {
Path string
Handle requestHandler
Config *RouteConfig
}
func newRouteHandler(path string, handle requestHandler, configs ...*RouteConfig) *routeHandler {
handler := &routeHandler{
Path: "/" + strings.TrimPrefix(path, "/"),
Handle: handle,
}
for _, config := range configs {
handler.Config = config
}
if handler.Config == nil {
handler.Config = &RouteConfig{}
}
if handler.Config.Resource != nil {
if handler.Config.Permissioner == nil {
handler.Config.Permissioner = handler.Config.Resource
}
handler.Config.Resource.mounted = true
}
return handler
}
// HasPermission check has permission to access router handler or not
func (handler routeHandler) HasPermission(permissionMode roles.PermissionMode, context *qor.Context) bool {
if handler.Config.Permissioner == nil {
return true
}
if handler.Config.PermissionMode != "" {
return handler.Config.Permissioner.HasPermission(handler.Config.PermissionMode, context)
}
return handler.Config.Permissioner.HasPermission(permissionMode, context)
}