Browse Source

中间件中增加是否为超管的判断

boweniac 8 months ago
parent
commit
a6135c896c

+ 3 - 0
internal/logic/label/get_label_list_logic.go

@@ -2,6 +2,7 @@ package label
 
 import (
 	"context"
+	"fmt"
 	"wechat-api/ent"
 
 	"wechat-api/ent/label"
@@ -58,6 +59,8 @@ func convertContactToContactInfo(label *ent.Contact) types.ContactInfo {
 
 func (l *GetLabelListLogic) GetLabelList(req *types.LabelListReq) (*types.LabelListResp, error) {
 	organizationId := l.ctx.Value("organizationId").(uint64)
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	fmt.Printf("---------------isAdmin----------------: %+v\n\n", isAdmin)
 	var predicates []predicate.Label
 	predicates = append(predicates, label.OrganizationIDEQ(organizationId))
 	if req.Type != nil {

+ 14 - 2
internal/middleware/authority_middleware.go

@@ -57,9 +57,9 @@ func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
 		result := batchCheck(m.Cbn, roleIds, act, obj)
 
 		if result {
-			logx.Infow("HTTP/HTTPS Request", logx.Field("UUID", r.Context().Value("userId").(string)),
-				logx.Field("path", obj), logx.Field("method", act))
 			userIdStr := r.Context().Value("userId").(string)
+			logx.Infow("HTTP/HTTPS Request", logx.Field("UUID", userIdStr),
+				logx.Field("path", obj), logx.Field("method", act))
 			data, err := m.CoreRpc.GetUserById(r.Context(), &core.UUIDReq{Id: userIdStr})
 			if err != nil {
 				logx.Errorw("get user info error", logx.Field("detail", err.Error()))
@@ -69,6 +69,7 @@ func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
 			// 将 data.DepartmentID 插入上下文,以供后续接口使用
 			//fmt.Printf("---------------departmentId----------------: %d\n\n", *data.DepartmentId)
 			r = r.WithContext(context.WithValue(r.Context(), "organizationId", *data.DepartmentId))
+			r = r.WithContext(context.WithValue(r.Context(), "isAdmin", stringInSlice(roleIds, []string{"001"})))
 			next(w, r)
 			return
 		} else {
@@ -100,3 +101,14 @@ func batchCheck(cbn *casbin.Enforcer, roleIds, act, obj string) bool {
 
 	return false
 }
+
+func stringInSlice(roleIds string, list []string) bool {
+	for _, r := range strings.Split(roleIds, ",") {
+		for _, v := range list {
+			if v == r {
+				return true
+			}
+		}
+	}
+	return false
+}