fix: 在线用户列表过滤角色数据范围

This commit is contained in:
TsMask
2024-07-03 15:22:05 +08:00
parent ab681e7724
commit c411d30db2

View File

@@ -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))
}
// 在线用户强制退出