refactor: replace standard log with zap logger and optimize code patterns
- Migrate all log.Printf/log.Println calls to structured zap logging - Use cmp.Or for cleaner default value handling - Replace manual loops with slices.ContainsFunc/IndexFunc - Add batch query methods (IsLikedBatch, IsFavoritedBatch) to solve N+1 problem - Update JSON tags from omitempty to omitzero for numeric fields - Use errgroup-style wg.Go for worker goroutines - Remove duplicate casbin/v2 dependency (keeping v3) - Add plans/ to gitignore
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"carrot_bbs/internal/service"
|
||||
@@ -104,18 +105,9 @@ func RequireRole(casbinService service.CasbinService, requiredRoles ...string) g
|
||||
}
|
||||
|
||||
// 检查用户是否拥有任一所需角色
|
||||
roleMap := make(map[string]bool)
|
||||
for _, role := range userRoles {
|
||||
roleMap[role] = true
|
||||
}
|
||||
|
||||
hasRole := false
|
||||
for _, required := range requiredRoles {
|
||||
if roleMap[required] {
|
||||
hasRole = true
|
||||
break
|
||||
}
|
||||
}
|
||||
hasRole := slices.ContainsFunc(requiredRoles, func(required string) bool {
|
||||
return slices.Contains(userRoles, required)
|
||||
})
|
||||
|
||||
if !hasRole {
|
||||
c.AbortWithStatusJSON(403, gin.H{
|
||||
|
||||
Reference in New Issue
Block a user