Forráskód Böngészése

fix: 修改用户验证为根据角色判断

jimmyyem 2 hónapja
szülő
commit
bcccf0dd01

+ 2 - 12
internal/logic/User/do_gpts_user_login_logic.go

@@ -2,12 +2,10 @@ package User
 
 import (
 	"context"
-	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 	"github.com/suyuan32/simple-admin-common/utils/pointy"
 	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 	"github.com/zeromicro/go-zero/core/errorx"
-	"strings"
 	"time"
 	"wechat-api/internal/utils"
 	"wechat-api/internal/utils/jwt"
@@ -45,14 +43,8 @@ func (l *DoGptsUserLoginLogic) DoGptsUserLogin(req *types.GptsUserLoginReq) (res
 		return nil, errorx.NewCodeInvalidArgumentError("login.wrongUsernameOrPassword")
 	}
 
-	department, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{
-		Id: *user.DepartmentId,
-	})
-	if err != nil {
-		return nil, err
-	}
-	fmt.Printf("department=%v\n", department)
-	if *department.Name != "前台用户" {
+	valid := utils.CheckGptLogin(user.RoleIds)
+	if !valid {
 		return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
 	}
 
@@ -61,8 +53,6 @@ func (l *DoGptsUserLoginLogic) DoGptsUserLogin(req *types.GptsUserLoginReq) (res
 		time.Now().Unix(),
 		l.svcCtx.Config.Auth.AccessExpire,
 		jwt.WithOption("userId", user.Id),
-		jwt.WithOption("roleId", strings.Join(user.RoleCodes, ",")),
-		jwt.WithOption("deptId", user.DepartmentId),
 	)
 	if err != nil {
 		return nil, err

+ 8 - 3
internal/logic/User/get_gpts_user_info_logic.go

@@ -6,6 +6,7 @@ import (
 	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
+	"wechat-api/internal/utils"
 	jwtutils "wechat-api/internal/utils/jwt"
 
 	"github.com/zeromicro/go-zero/core/logx"
@@ -32,14 +33,18 @@ func (l *GetGptsUserInfoLogic) GetGptsUserInfo(tokenStr string) (*types.UserInfo
 		return nil, errorx.NewInvalidArgumentError("用户未登录")
 	}
 
-	userId := claims["userId"].(string)
-	departmentId := claims["deptId"].(float64)
+	userId, ok := claims["userId"].(string)
 
-	if userId == "" || departmentId != float64(15) {
+	if !ok || userId == "" {
 		return nil, errorx.NewInvalidArgumentError("用户需要登录")
 	}
 
 	userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
+	valid := utils.CheckGptLogin(userInfo.RoleIds)
+	if !valid {
+		return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
+	}
+
 	//department, _ := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: uint64(departmentId)})
 	//resp.Data = fmt.Sprintf("userId=%v departmentId=%v departmentName=%v ", userId, departmentId, *department.Name)
 	resp.Data.Username = userInfo.Username

+ 9 - 5
internal/logic/User/update_gpts_user_pwd_logic.go

@@ -2,10 +2,10 @@ package User
 
 import (
 	"context"
-	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/internal/utils"
 	jwtutils "wechat-api/internal/utils/jwt"
 
 	"wechat-api/internal/svc"
@@ -33,14 +33,18 @@ func (l *UpdateGptsUserPwdLogic) UpdateGptsUserPwd(tokenStr string, req *types.P
 		return nil, errorx.NewInvalidArgumentError("用户未登录")
 	}
 
-	userId := claims["userId"].(string)
-	departmentId := claims["deptId"].(float64)
-	fmt.Printf("user=%v depid=%v \n", userId, departmentId)
+	userId, ok := claims["userId"].(string)
 
-	if userId == "" || departmentId != float64(15) {
+	if !ok || userId == "" {
 		return nil, errorx.NewInvalidArgumentError("用户需要登录")
 	}
 
+	userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
+	valid := utils.CheckGptLogin(userInfo.RoleIds)
+	if !valid {
+		return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
+	}
+
 	if req.Password2 == nil || req.Password == nil {
 		return nil, errorx.NewInvalidArgumentError("参数不能为空")
 	}

+ 13 - 1
internal/logic/chatrecords/gpts_get_api_message_logic.go

@@ -3,9 +3,11 @@ package chatrecords
 import (
 	"context"
 	"github.com/alibabacloud-go/tea/tea"
+	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 	"github.com/zeromicro/go-zero/core/errorx"
 	"strconv"
 	"wechat-api/hook/dify"
+	"wechat-api/internal/utils"
 	jwtutils "wechat-api/internal/utils/jwt"
 
 	"wechat-api/internal/svc"
@@ -34,7 +36,17 @@ func (l *GptsGetApiMessageLogic) GptsGetApiMessage(req *types.GptMessageReq, tok
 	if err != nil {
 		return nil, errorx.NewInvalidArgumentError("用户未登录")
 	}
-	userId := claims["userId"].(string)
+	userId, ok := claims["userId"].(string)
+
+	if !ok || userId == "" {
+		return nil, errorx.NewInvalidArgumentError("用户需要登录")
+	}
+
+	userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
+	valid := utils.CheckGptLogin(userInfo.RoleIds)
+	if !valid {
+		return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
+	}
 
 	var firstId, limit string
 	if req.FirstId != nil && *req.FirstId != "" {

+ 13 - 3
internal/logic/chatrecords/gpts_get_api_session_logic.go

@@ -2,11 +2,12 @@ package chatrecords
 
 import (
 	"context"
-	"fmt"
 	"github.com/alibabacloud-go/tea/tea"
+	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 	"github.com/zeromicro/go-zero/core/errorx"
 	"strconv"
 	"wechat-api/hook/dify"
+	"wechat-api/internal/utils"
 	jwtutils "wechat-api/internal/utils/jwt"
 
 	"wechat-api/internal/svc"
@@ -35,8 +36,17 @@ func (l *GptsGetApiSessionLogic) GptsGetApiSession(req *types.GptsSessionReq, to
 	if err != nil {
 		return nil, errorx.NewInvalidArgumentError("用户未登录")
 	}
-	fmt.Printf("claims=%v \n", claims)
-	userId := claims["userId"].(string)
+	userId, ok := claims["userId"].(string)
+
+	if !ok || userId == "" {
+		return nil, errorx.NewInvalidArgumentError("用户需要登录")
+	}
+
+	userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
+	valid := utils.CheckGptLogin(userInfo.RoleIds)
+	if !valid {
+		return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
+	}
 
 	limit := strconv.Itoa(*req.Limit)
 	var lastId string

+ 9 - 5
internal/logic/chatrecords/gpts_submit_api_chat_logic.go

@@ -6,11 +6,13 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
+	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 	"io"
 	"net/http"
 	"strings"
 	"time"
 	"wechat-api/hook/dify"
+	"wechat-api/internal/utils"
 	jwtutils "wechat-api/internal/utils/jwt"
 
 	"wechat-api/internal/svc"
@@ -49,11 +51,8 @@ func (l *GptsSubmitApiChatLogic) GptsSubmitApiChat(tokenStr string, req *types.G
 		return
 	}
 
-	userId := claims["userId"].(string) //用户的uuid
-	departmentId := claims["deptId"].(float64)
-	//fmt.Println(userId, departmentId, departmentId == float64(15))
-	if departmentId == float64(15) {
-	} else {
+	userId, ok := claims["userId"].(string) //用户的uuid
+	if !ok || userId == "" {
 		jsonData := ChatMessage{}
 		jsonData.NeedPay = true
 		jsonData.Finish = true
@@ -62,6 +61,11 @@ func (l *GptsSubmitApiChatLogic) GptsSubmitApiChat(tokenStr string, req *types.G
 		flusher.Flush()
 		return
 	}
+	userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
+	valid := utils.CheckGptLogin(userInfo.RoleIds)
+	if !valid {
+		return
+	}
 
 	var chatReq dify.ChatReq
 	chatReq.ResponseMode = "streaming"

+ 13 - 0
internal/utils/GptUserLogin.go

@@ -0,0 +1,13 @@
+package utils
+
+// CheckGptLogin 检测是否为前台用户角色
+func CheckGptLogin(codes []uint64) bool {
+	var valid bool
+	for _, roleCode := range codes {
+		if roleCode == uint64(6) {
+			valid = true
+		}
+	}
+
+	return valid
+}