瀏覽代碼

权限校验方法提取

wangkaiyue 3 年之前
父節點
當前提交
88ddb32961
共有 3 個文件被更改,包括 32 次插入19 次删除
  1. 12 0
      common/util.go
  2. 15 11
      core/proxy/middleware/filterFuncs.go
  3. 5 8
      core/proxy/rpc/userCenter.go

+ 12 - 0
common/util.go

@@ -2,6 +2,7 @@ package common
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"math"
 	"net"
 	"net"
 )
 )
 
 
@@ -21,3 +22,14 @@ func GetLocalIP() (string, error) {
 	}
 	}
 	return "", fmt.Errorf("unable to determine local ip")
 	return "", fmt.Errorf("unable to determine local ip")
 }
 }
+
+// SplitPower 多维权限拆解
+// xyz【1需要 0 不需要】
+// SplitPower(1001)=[]bool{true,false,false,true}
+func SplitPower(power, num int) []bool {
+	boolArr := make([]bool, num)
+	for i := 0; i < num; i++ {
+		boolArr[num-i-1] = power/int(math.Pow(10, float64(i)))%10 == 1
+	}
+	return boolArr
+}

+ 15 - 11
core/proxy/middleware/filterFuncs.go

@@ -1,6 +1,7 @@
 package middleware
 package middleware
 
 
 import (
 import (
+	"bp.jydev.jianyu360.cn/BaseService/gateway/common"
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/rpc"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/rpc"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
@@ -18,18 +19,21 @@ import (
 func filterBefore(r *ghttp.Request) error {
 func filterBefore(r *ghttp.Request) error {
 	ctx := router.GetGContext(r.GetCtx())
 	ctx := router.GetGContext(r.GetCtx())
 	rule := ctx.RouterRule
 	rule := ctx.RouterRule
-	uCheck, eCheck := getPowerCheck(rule.SessCheck)
-	// 校验身份
-	if uCheck && ctx.Sess.UserId == "" {
-		return NewErrorWithCode(GLOBAL_ERR_NOTLOGIN)
-	}
-	if eCheck && ctx.Sess.EntId == 0 {
-		return NewErrorWithCode(GLOBAL_ERR_NOENT_SELECT)
-	}
 
 
-	//	用户身份注入请求体中
-	if uCheck || eCheck {
-		infusionIdentity(r, ctx.Sess.EntId, ctx.Sess.UserId)
+	if rule.SessCheck != 0 {
+		sessionPower := common.SplitPower(rule.SessCheck, 2)
+		uCheck, eCheck := sessionPower[0], sessionPower[1]
+		if uCheck && ctx.Sess.UserId == "" {
+			return NewErrorWithCode(GLOBAL_ERR_NOTLOGIN)
+		}
+		if eCheck && ctx.Sess.EntId == 0 {
+			return NewErrorWithCode(GLOBAL_ERR_NOENT_SELECT)
+		}
+
+		//	用户身份注入请求体中
+		if uCheck || eCheck {
+			infusionIdentity(r, ctx.Sess.EntId, ctx.Sess.UserId)
+		}
 	}
 	}
 
 
 	//校验账户状态,校验认证状态
 	//校验账户状态,校验认证状态

+ 5 - 8
core/proxy/rpc/userCenter.go

@@ -1,6 +1,7 @@
 package rpc
 package rpc
 
 
 import (
 import (
+	"bp.jydev.jianyu360.cn/BaseService/gateway/common"
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
@@ -30,8 +31,10 @@ func CheckAccountStatus(entId int64, userId string, authCheck, statusCheck int)
 		return nil
 		return nil
 	}
 	}
 
 
-	authUser, authEnt := getPCheck(authCheck)
-	statusUser, statusEnt := getPCheck(statusCheck)
+	authCheckArr := common.SplitPower(authCheck, 2)
+	statusCheckArr := common.SplitPower(statusCheck, 2)
+	authUser, authEnt := authCheckArr[0], authCheckArr[1]
+	statusUser, statusEnt := statusCheckArr[0], statusCheckArr[1]
 
 
 	//查询个人状态
 	//查询个人状态
 	if authUser || statusUser {
 	if authUser || statusUser {
@@ -82,9 +85,3 @@ func checkEntStatus(entId int64, authCheck, statusCheck bool) error {
 func checkPersonalStatus(userId string, authCheck, statusCheck bool) error {
 func checkPersonalStatus(userId string, authCheck, statusCheck bool) error {
 	return NewErrorWithCode(GLOBAL_ERR_UNFINISH, "checkPersonalStatus")
 	return NewErrorWithCode(GLOBAL_ERR_UNFINISH, "checkPersonalStatus")
 }
 }
-
-func getPCheck(c int) (check1 bool, check2 bool) {
-	check1 = c/10%10 == 1
-	check2 = c/1%10 == 1
-	return
-}