fix: 在线用户列表过滤角色数据范围
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"be.ems/src/framework/constants/cachekey"
|
"be.ems/src/framework/constants/cachekey"
|
||||||
|
"be.ems/src/framework/constants/roledatascope"
|
||||||
"be.ems/src/framework/i18n"
|
"be.ems/src/framework/i18n"
|
||||||
"be.ems/src/framework/redis"
|
"be.ems/src/framework/redis"
|
||||||
"be.ems/src/framework/utils/ctx"
|
"be.ems/src/framework/utils/ctx"
|
||||||
@@ -37,6 +38,43 @@ func (s *SysUserOnlineController) List(c *gin.Context) {
|
|||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
ipaddr := c.Query("ipaddr")
|
ipaddr := c.Query("ipaddr")
|
||||||
userName := c.Query("userName")
|
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
|
// 获取所有在线用户key
|
||||||
keys, _ := redis.GetKeys("", cachekey.LOGIN_TOKEN_KEY+"*")
|
keys, _ := redis.GetKeys("", cachekey.LOGIN_TOKEN_KEY+"*")
|
||||||
@@ -68,6 +106,10 @@ func (s *SysUserOnlineController) List(c *gin.Context) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !hasRoleDataScope(loginUser) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
onlineUser := s.sysUserOnlineService.LoginUserToUserOnline(loginUser)
|
onlineUser := s.sysUserOnlineService.LoginUserToUserOnline(loginUser)
|
||||||
if onlineUser.TokenID != "" {
|
if onlineUser.TokenID != "" {
|
||||||
onlineUser.LoginLocation = i18n.TKey(language, onlineUser.LoginLocation)
|
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
|
return filteredUserOnlines[j].LoginTime > filteredUserOnlines[i].LoginTime
|
||||||
})
|
})
|
||||||
|
|
||||||
c.JSON(200, result.Ok(map[string]any{
|
data["total"] = len(filteredUserOnlines)
|
||||||
"total": len(filteredUserOnlines),
|
data["rows"] = filteredUserOnlines
|
||||||
"rows": filteredUserOnlines,
|
c.JSON(200, result.Ok(data))
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在线用户强制退出
|
// 在线用户强制退出
|
||||||
|
|||||||
Reference in New Issue
Block a user