feat: 查询条件优化和 xorm session 修复
This commit is contained in:
@@ -45,8 +45,21 @@ func (h *AppHandler) List(c *gin.Context) {
|
||||
}
|
||||
|
||||
var apps []model.Application
|
||||
total, _ := query.Count(&model.Application{})
|
||||
query.Limit(req.PageSize, (req.PageNo-1)*req.PageSize).Find(&apps)
|
||||
whereClause := "enabled = 1"
|
||||
if !userInfo.IsSuperAdmin() {
|
||||
if userInfo.IsGroupAdmin() && userInfo.GroupId != nil {
|
||||
whereClause += " AND group_id = " + strconv.Itoa(*userInfo.GroupId)
|
||||
} else {
|
||||
userId, _ := strconv.Atoi(userInfo.Id)
|
||||
whereClause += " AND (owner_id = " + strconv.Itoa(userId) + " OR id IN (SELECT app_id FROM user_permissions WHERE user_id = " + strconv.Itoa(userId) + "))"
|
||||
}
|
||||
}
|
||||
if req.AppType != "" {
|
||||
whereClause += " AND service_id = '" + req.AppType + "'"
|
||||
}
|
||||
|
||||
total, _ := dao.Engine.Where(whereClause).Count(&model.Application{})
|
||||
dao.Engine.Where(whereClause).Limit(req.PageSize, (req.PageNo-1)*req.PageSize).Find(&apps)
|
||||
|
||||
// load group names - batch query to avoid N+1
|
||||
groupIds := make(map[int]bool)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"kenaito-conf/server/internal/service"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -36,34 +37,48 @@ func (h *ConfigHandler) List(c *gin.Context) {
|
||||
}
|
||||
|
||||
userInfo := middleware.GetUserInfo(c)
|
||||
query := dao.Engine.Where("enabled = 1")
|
||||
|
||||
if req.Env != "" {
|
||||
query = query.And("env = ?", req.Env)
|
||||
}
|
||||
if req.AppId != nil {
|
||||
if *req.AppId <= 0 {
|
||||
query = query.And("(app_id is null OR app_id = 0)")
|
||||
} else {
|
||||
query = query.And("app_id = ?", *req.AppId)
|
||||
}
|
||||
}
|
||||
|
||||
// permission filter
|
||||
// 检查权限
|
||||
if !userInfo.IsSuperAdmin() {
|
||||
if userInfo.IsGroupAdmin() && userInfo.GroupId != nil {
|
||||
query = query.And("group_id = ?", *userInfo.GroupId)
|
||||
} else if len(userInfo.GrantAppIds) > 0 {
|
||||
query = query.In("app_id", userInfo.GrantAppIds)
|
||||
} else {
|
||||
if !userInfo.IsGroupAdmin() && len(userInfo.GrantAppIds) == 0 {
|
||||
c.JSON(http.StatusOK, model.PageResult{PageNo: req.PageNo, PageSize: req.PageSize, Total: 0, Data: []interface{}{}})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询条件
|
||||
buildConditions := func() []string {
|
||||
conditions := []string{"enabled = 1"}
|
||||
if req.Env != "" {
|
||||
conditions = append(conditions, "env = '"+req.Env+"'")
|
||||
}
|
||||
if req.AppId != nil {
|
||||
if *req.AppId <= 0 {
|
||||
conditions = append(conditions, "(app_id is null OR app_id = 0)")
|
||||
} else {
|
||||
conditions = append(conditions, "app_id = "+strconv.Itoa(*req.AppId))
|
||||
}
|
||||
}
|
||||
if !userInfo.IsSuperAdmin() {
|
||||
if userInfo.IsGroupAdmin() && userInfo.GroupId != nil {
|
||||
conditions = append(conditions, "group_id = "+strconv.Itoa(*userInfo.GroupId))
|
||||
} else if len(userInfo.GrantAppIds) > 0 {
|
||||
ids := make([]string, len(userInfo.GrantAppIds))
|
||||
for i, id := range userInfo.GrantAppIds {
|
||||
ids[i] = strconv.Itoa(id)
|
||||
}
|
||||
conditions = append(conditions, "app_id in ("+strings.Join(ids, ",")+")")
|
||||
}
|
||||
}
|
||||
return conditions
|
||||
}
|
||||
|
||||
conditions := buildConditions()
|
||||
whereClause := strings.Join(conditions, " AND ")
|
||||
|
||||
var configs []model.AppConfig
|
||||
total, _ := query.Count(&model.AppConfig{})
|
||||
query.Limit(req.PageSize, (req.PageNo-1)*req.PageSize).Find(&configs)
|
||||
total, _ := dao.Engine.Where(whereClause).Count(&model.AppConfig{})
|
||||
dao.Engine.Where(whereClause).Limit(req.PageSize, (req.PageNo-1)*req.PageSize).Find(&configs)
|
||||
|
||||
// build app names
|
||||
for i := range configs {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"crypto/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -24,20 +25,35 @@ func NewUserHandler(perm *service.PermissionService) *UserHandler {
|
||||
|
||||
func (h *UserHandler) List(c *gin.Context) {
|
||||
groupIdStr := c.Query("groupId")
|
||||
name := c.Query("name")
|
||||
userType := c.Query("type")
|
||||
enabledStr := c.Query("enabled")
|
||||
userInfo := middleware.GetUserInfo(c)
|
||||
|
||||
buildConditions := func() []string {
|
||||
conditions := []string{"1=1"}
|
||||
if !userInfo.IsSuperAdmin() && userInfo.GroupId != nil {
|
||||
conditions = append(conditions, "group_id = "+strconv.Itoa(*userInfo.GroupId))
|
||||
}
|
||||
if groupIdStr != "" {
|
||||
conditions = append(conditions, "group_id = "+groupIdStr)
|
||||
}
|
||||
if name != "" {
|
||||
conditions = append(conditions, "name like '%"+name+"%'")
|
||||
}
|
||||
if userType != "" {
|
||||
conditions = append(conditions, "type = '"+userType+"'")
|
||||
}
|
||||
if enabledStr != "" {
|
||||
conditions = append(conditions, "enabled = "+enabledStr)
|
||||
}
|
||||
return conditions
|
||||
}
|
||||
|
||||
whereClause := strings.Join(buildConditions(), " AND ")
|
||||
|
||||
var users []model.User
|
||||
query := dao.Engine.Where("enabled = 1")
|
||||
|
||||
if !userInfo.IsSuperAdmin() && userInfo.GroupId != nil {
|
||||
query = query.And("group_id = ?", *userInfo.GroupId)
|
||||
}
|
||||
if groupIdStr != "" {
|
||||
groupId, _ := strconv.Atoi(groupIdStr)
|
||||
query = query.And("group_id = ?", groupId)
|
||||
}
|
||||
|
||||
query.Find(&users)
|
||||
dao.Engine.Where(whereClause).Find(&users)
|
||||
|
||||
// load group names
|
||||
var groups []model.BusinessGroup
|
||||
|
||||
Reference in New Issue
Block a user