mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
feat: add sidebar component with context and mobile support
- Implemented Sidebar component with collapsible functionality. - Added SidebarProvider for managing open state and keyboard shortcuts. - Created SidebarTrigger, SidebarRail, and various sidebar elements (Header, Footer, Content, etc.). - Integrated mobile responsiveness using Sheet component. - Added utility hooks for mobile detection. feat: create table component for structured data display - Developed Table component with subcomponents: TableHeader, TableBody, TableFooter, TableRow, TableCell, and TableCaption. - Enhanced styling for better readability and usability. feat: implement tabs component for navigation - Created Tabs component with TabsList, TabsTrigger, and TabsContent for tabbed navigation. - Ensured accessibility and responsive design. feat: add toggle group component for grouped toggle buttons - Developed ToggleGroup and ToggleGroupItem components for managing toggle states. - Integrated context for consistent styling and behavior. feat: create toggle component for binary state representation - Implemented Toggle component with variant and size options. - Enhanced user interaction with visual feedback. feat: add tooltip component for contextual information - Developed Tooltip, TooltipTrigger, and TooltipContent for displaying additional information on hover. - Integrated animations for a smoother user experience. feat: implement mobile detection hook - Created useIsMobile hook to determine if the user is on a mobile device. - Utilized matchMedia for responsive design adjustments.
This commit is contained in:
@ -2,6 +2,9 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/snowykami/neo-blog/internal/ctxutils"
|
||||
@ -9,8 +12,6 @@ import (
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"github.com/snowykami/neo-blog/pkg/resps"
|
||||
"github.com/snowykami/neo-blog/pkg/utils"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UseAuth(block bool) app.HandlerFunc {
|
||||
@ -79,6 +80,46 @@ func UseAuth(block bool) app.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// UseRole 检查用户角色是否符合要求,必须在 UseAuth 之后使用
|
||||
// requiredRole 可以是 "admin", "editor", "user" 等
|
||||
// admin包含editor, editor包含user
|
||||
func UseRole(requiredRole string) app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
currentUserID := ctx.Value(constant.ContextKeyUserID)
|
||||
if currentUserID == nil {
|
||||
resps.Unauthorized(c, resps.ErrUnauthorized)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
userID := currentUserID.(uint)
|
||||
user, err := repo.User.GetUserByID(userID)
|
||||
if err != nil {
|
||||
resps.InternalServerError(c, resps.ErrInternalServerError)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if user == nil {
|
||||
resps.Unauthorized(c, resps.ErrUnauthorized)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
roleHierarchy := map[string]int{
|
||||
constant.RoleUser: 1,
|
||||
constant.RoleEditor: 2,
|
||||
constant.RoleAdmin: 3,
|
||||
}
|
||||
userRoleLevel := roleHierarchy[user.Role]
|
||||
requiredRoleLevel := roleHierarchy[requiredRole]
|
||||
if userRoleLevel < requiredRoleLevel {
|
||||
resps.Forbidden(c, resps.ErrForbidden)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func isStatefulJwtValid(claims *utils.Claims) (bool, error) {
|
||||
if !claims.Stateful {
|
||||
return true, nil
|
||||
|
@ -1 +1,16 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
)
|
||||
|
||||
func UseTrack() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
ctx = context.WithValue(ctx, constant.ContextKeyRemoteAddr, c.ClientIP())
|
||||
ctx = context.WithValue(ctx, constant.ContextKeyUserAgent, c.UserAgent())
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user