From c411d30db2994606c9da59f10db631f05d1a2576 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Wed, 3 Jul 2024 15:22:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E8=BF=87=E6=BB=A4=E8=A7=92=E8=89=B2=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/controller/sys_user_online.go | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/modules/monitor/controller/sys_user_online.go b/src/modules/monitor/controller/sys_user_online.go index 40a84193..fb279d14 100644 --- a/src/modules/monitor/controller/sys_user_online.go +++ b/src/modules/monitor/controller/sys_user_online.go @@ -6,6 +6,7 @@ import ( "strings" "be.ems/src/framework/constants/cachekey" + "be.ems/src/framework/constants/roledatascope" "be.ems/src/framework/i18n" "be.ems/src/framework/redis" "be.ems/src/framework/utils/ctx" @@ -37,6 +38,43 @@ func (s *SysUserOnlineController) List(c *gin.Context) { language := ctx.AcceptLanguage(c) ipaddr := c.Query("ipaddr") userName := c.Query("userName") + data := map[string]any{ + "total": 0, + "rows": []model.SysUserOnline{}, + } + + // 当前登录用户 + currentUser, _ := ctx.LoginUser(c) + currentRoleKey := "" + currentRoleDataScope := "" + if len(currentUser.User.Roles) > 0 { + role := currentUser.User.Roles[0] + currentRoleKey = role.RoleKey + currentRoleDataScope = role.DataScope + } + + if currentRoleKey == "" { + c.JSON(200, result.Ok(data)) + return + } + hasRoleDataScope := func(loginUser vo.LoginUser) bool { + if currentRoleDataScope == roledatascope.ALL { + return true + } + if currentRoleDataScope == roledatascope.CUSTOM { + return false + } + if currentRoleDataScope == roledatascope.DEPT && loginUser.DeptID == currentUser.DeptID { + return true + } + if currentRoleDataScope == roledatascope.DEPT_AND_CHILD && strings.Contains(loginUser.User.Dept.Ancestors, currentUser.DeptID) { + return true + } + if currentRoleDataScope == roledatascope.SELF && loginUser.UserID == currentUser.UserID { + return true + } + return false + } // 获取所有在线用户key keys, _ := redis.GetKeys("", cachekey.LOGIN_TOKEN_KEY+"*") @@ -68,6 +106,10 @@ func (s *SysUserOnlineController) List(c *gin.Context) { continue } + if !hasRoleDataScope(loginUser) { + continue + } + onlineUser := s.sysUserOnlineService.LoginUserToUserOnline(loginUser) if onlineUser.TokenID != "" { onlineUser.LoginLocation = i18n.TKey(language, onlineUser.LoginLocation) @@ -104,10 +146,9 @@ func (s *SysUserOnlineController) List(c *gin.Context) { return filteredUserOnlines[j].LoginTime > filteredUserOnlines[i].LoginTime }) - c.JSON(200, result.Ok(map[string]any{ - "total": len(filteredUserOnlines), - "rows": filteredUserOnlines, - })) + data["total"] = len(filteredUserOnlines) + data["rows"] = filteredUserOnlines + c.JSON(200, result.Ok(data)) } // 在线用户强制退出