You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			48 lines
		
	
	
		
			982 B
		
	
	
	
		
			Go
		
	
			
		
		
	
	
			48 lines
		
	
	
		
			982 B
		
	
	
	
		
			Go
		
	
| package middleware
 | |
| 
 | |
| import (
 | |
| 	"github.com/casbin/casbin/v2"
 | |
| 	"github.com/zeromicro/go-zero/core/logx"
 | |
| 	"github.com/zeromicro/go-zero/core/stores/redis"
 | |
| 	"net/http"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| type AuthorityMiddleware struct {
 | |
| 	Cbn *casbin.Enforcer
 | |
| 	Rds *redis.Redis
 | |
| }
 | |
| 
 | |
| func NewAuthorityMiddleware() *AuthorityMiddleware {
 | |
| 	return &AuthorityMiddleware{}
 | |
| }
 | |
| 
 | |
| func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		//obj := r.URL.Path
 | |
| 		//act := r.Method
 | |
| 		next(w, r)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (m *AuthorityMiddleware) batchCheck(cbn *casbin.Enforcer, roles, act, obj string) bool {
 | |
| 	var checkReq [][]any
 | |
| 	for _, v := range strings.Split(roles, ",") {
 | |
| 		checkReq = append(checkReq, []any{v, obj, act})
 | |
| 	}
 | |
| 
 | |
| 	result, err := cbn.BatchEnforce(checkReq)
 | |
| 	if err != nil {
 | |
| 		logx.Errorw("Casbin enforce error", logx.Field("detail", err.Error()))
 | |
| 		return false
 | |
| 	}
 | |
| 
 | |
| 	for _, v := range result {
 | |
| 		if v {
 | |
| 			return true
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return false
 | |
| }
 |