155 lines
3.7 KiB
Go
155 lines
3.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"server/services"
|
|
"strings"
|
|
|
|
"github.com/beego/beego/v2/server/web/context"
|
|
)
|
|
|
|
func PermissionMiddleware() func(ctx *context.Context) {
|
|
return func(ctx *context.Context) {
|
|
path := ctx.Input.URL()
|
|
|
|
publicPaths := []string{
|
|
"/api/login",
|
|
"/api/logout",
|
|
"/api/reset-password",
|
|
"/api/program-categories/public",
|
|
"/api/program-infos/public",
|
|
"/api/files/public",
|
|
}
|
|
|
|
for _, p := range publicPaths {
|
|
if path == p {
|
|
return
|
|
}
|
|
}
|
|
|
|
if strings.HasPrefix(path, "/api/files/public-preview/") {
|
|
return
|
|
}
|
|
|
|
userIdData := ctx.Input.GetData("userId")
|
|
if userIdData == nil {
|
|
return
|
|
}
|
|
|
|
userId, ok := userIdData.(int)
|
|
if !ok {
|
|
ctx.Output.JSON(map[string]interface{}{
|
|
"success": false,
|
|
"message": "用户ID格式错误",
|
|
}, false, false)
|
|
return
|
|
}
|
|
|
|
permission := getPermissionByPath(path, ctx.Input.Method())
|
|
|
|
if permission == "" {
|
|
return
|
|
}
|
|
|
|
hasPermission, err := services.CheckUserPermission(userId, permission)
|
|
if err != nil {
|
|
ctx.Output.JSON(map[string]interface{}{
|
|
"success": false,
|
|
"message": "权限验证失败",
|
|
"error": err.Error(),
|
|
}, false, false)
|
|
return
|
|
}
|
|
|
|
if !hasPermission {
|
|
ctx.Output.JSON(map[string]interface{}{
|
|
"success": false,
|
|
"message": "您没有权限访问此接口",
|
|
"code": 403,
|
|
}, false, false)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func getPermissionByPath(path, method string) string {
|
|
permissionMap := map[string]string{
|
|
"GET:/api/allUsers": "user:list",
|
|
"GET:/api/user/:id": "user:detail",
|
|
"POST:/api/addUser": "user:add",
|
|
"POST:/api/editUser/:id": "user:edit",
|
|
"DELETE:/api/deleteUser/:id": "user:delete",
|
|
"POST:/api/changePassword/:id": "user:changePassword",
|
|
|
|
"GET:/api/allmenu": "menu:list",
|
|
"POST:/api/menu": "menu:create",
|
|
"PUT:/api/menu/:id": "menu:update",
|
|
"DELETE:/api/menu/:id": "menu:delete",
|
|
|
|
"GET:/api/files": "file:list",
|
|
"POST:/api/files": "file:upload",
|
|
"GET:/api/files/my": "file:my",
|
|
"GET:/api/files/download/:id": "file:download",
|
|
"GET:/api/files/preview/:id": "file:preview",
|
|
"GET:/api/files/:id": "file:detail",
|
|
"PUT:/api/files/:id": "file:update",
|
|
"DELETE:/api/files/:id": "file:delete",
|
|
"GET:/api/files/search": "file:search",
|
|
"GET:/api/files/statistics": "file:statistics",
|
|
|
|
"GET:/api/tenant/list": "tenant:list",
|
|
"POST:/api/tenant": "tenant:create",
|
|
"PUT:/api/tenant/:id": "tenant:update",
|
|
"DELETE:/api/tenant/:id": "tenant:delete",
|
|
"POST:/api/tenant/:id/audit": "tenant:audit",
|
|
"GET:/api/tenant/:id": "tenant:detail",
|
|
|
|
"GET:/api/knowledge/list": "knowledge:list",
|
|
"GET:/api/knowledge/detail": "knowledge:detail",
|
|
"POST:/api/knowledge/create": "knowledge:create",
|
|
"POST:/api/knowledge/update": "knowledge:update",
|
|
"POST:/api/knowledge/delete": "knowledge:delete",
|
|
}
|
|
|
|
key := method + ":" + path
|
|
if perm, ok := permissionMap[key]; ok {
|
|
return perm
|
|
}
|
|
|
|
pathParts := strings.Split(path, "/")
|
|
for pattern, perm := range permissionMap {
|
|
parts := strings.Split(pattern, ":")
|
|
if len(parts) != 2 {
|
|
continue
|
|
}
|
|
|
|
methodPart := parts[0]
|
|
pathPattern := parts[1]
|
|
|
|
if methodPart != method {
|
|
continue
|
|
}
|
|
|
|
patternParts := strings.Split(pathPattern, "/")
|
|
if len(patternParts) != len(pathParts) {
|
|
continue
|
|
}
|
|
|
|
match := true
|
|
for i, part := range patternParts {
|
|
if strings.HasPrefix(part, ":") {
|
|
continue
|
|
}
|
|
if part != pathParts[i] {
|
|
match = false
|
|
break
|
|
}
|
|
}
|
|
|
|
if match {
|
|
return perm
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|