forked from bullteam/zeus-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perm.go
39 lines (36 loc) · 938 Bytes
/
perm.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
package middleware
import (
"fmt"
"github.com/beego/i18n"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"github.com/CMLiang/zeus-admin/pkg/api/log"
)
// Ignored permissions
var ignoredPerms = map[string]bool{}
// PermCheck - check permission automatically
func PermCheck(c *gin.Context) {
route := strings.Split(c.Request.URL.RequestURI(), "?")[0]
for _, p := range c.Params {
route = strings.Replace(route, "/"+p.Value, "/:"+p.Key, 1)
}
route = strings.ToLower(c.Request.Method) + "@" + route
uid := fmt.Sprintf("%#v", c.Value("userId"))
if _, ok := ignoredPerms[route]; ok {
c.Next()
return
}
if !accountService.CheckPermission(uid, "root", route) {
log.Warn(fmt.Sprintf("No permission for %s", route))
c.JSON(http.StatusOK, gin.H{
"code": 403,
"msg": i18n.Tr(GetLang(), "err.Err403"),
})
c.Abort()
return
} else {
log.Info(fmt.Sprintf("Pass permission check for %s", route))
}
c.Next()
}