96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package mid
 | |
| 
 | |
| //var publicRoutes = map[string]bool{
 | |
| //	"/home.html":          true,
 | |
| //	"/dashboard":          true,
 | |
| //	"/system/menus":       true,
 | |
| //	"/upload/img":         true,
 | |
| //	"/upload/file":        true,
 | |
| //	"/upload/multi_files": true,
 | |
| //	"/system/pear.json":   true,
 | |
| //	"/logout":             true,
 | |
| //}
 | |
| //
 | |
| //var m sync.Map
 | |
| //
 | |
| //func Authorize(
 | |
| //	sess session.Manager,
 | |
| //	menuService v1.MenuService,
 | |
| //) func(http.Handler) http.Handler {
 | |
| //	return func(next http.Handler) http.Handler {
 | |
| //		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 | |
| //			ctx := r.Context()
 | |
| //			path := r.URL.Path
 | |
| //
 | |
| //			// 登陆检查
 | |
| //			user, err := sess.GetUser(ctx, know.StoreName)
 | |
| //			if err != nil || user.ID == 0 {
 | |
| //				http.Redirect(w, r, "/", http.StatusFound)
 | |
| //				return
 | |
| //			}
 | |
| //
 | |
| //			// 公共路由放行
 | |
| //			if publicRoutes[path] {
 | |
| //				ctx = setUser(ctx, user)
 | |
| //				next.ServeHTTP(w, r.WithContext(ctx))
 | |
| //				return
 | |
| //			}
 | |
| //
 | |
| //			n1 := time.Now()
 | |
| //			// 权限检查
 | |
| //			var menus map[string]*dto.OwnerMenuDto
 | |
| //			cacheKey := fmt.Sprintf("user_menus:%d", user.RoleID)
 | |
| //			if value, ok := m.Load(cacheKey); ok {
 | |
| //				menus = value.(map[string]*dto.OwnerMenuDto)
 | |
| //				log.Printf("map (from cache): %s", time.Since(n1).String())
 | |
| //			} else {
 | |
| //				menus, err = menuService.ListByRoleIDToMap(ctx, user.RoleID)
 | |
| //				if err == nil {
 | |
| //					m.Store(cacheKey, menus)
 | |
| //				}
 | |
| //				log.Printf("map (from DB, then cached): %s", time.Since(n1).String())
 | |
| //			}
 | |
| //
 | |
| //			if !hasPermission(menus, path) {
 | |
| //				http.Error(w, "Forbidden", http.StatusForbidden)
 | |
| //				return
 | |
| //			}
 | |
| //
 | |
| //			cur := getCurrentMenus(menus, path)
 | |
| //
 | |
| //			ctx = setUser(ctx, user)
 | |
| //			ctx = setCurMenus(ctx, cur)
 | |
| //
 | |
| //			next.ServeHTTP(w, r.WithContext(ctx))
 | |
| //		})
 | |
| //	}
 | |
| //}
 | |
| //
 | |
| //func hasPermission(menus map[string]*dto.OwnerMenuDto, path string) bool {
 | |
| //	_, ok := menus[path]
 | |
| //	return ok
 | |
| //}
 | |
| //
 | |
| //func getCurrentMenus(data map[string]*dto.OwnerMenuDto, path string) []dto.OwnerMenuDto {
 | |
| //	var res []dto.OwnerMenuDto
 | |
| //
 | |
| //	menu, ok := data[path]
 | |
| //	if !ok {
 | |
| //		return res
 | |
| //	}
 | |
| //
 | |
| //	for _, item := range data {
 | |
| //		if menu.IsList {
 | |
| //			if item.ParentID == menu.ID || item.ID == menu.ID {
 | |
| //				res = append(res, *item)
 | |
| //			}
 | |
| //		} else {
 | |
| //			if item.ParentID == menu.ParentID {
 | |
| //				res = append(res, *item)
 | |
| //			}
 | |
| //		}
 | |
| //	}
 | |
| //
 | |
| //	return res
 | |
| //}
 |