From 4af22becf484efb190f3d9c536c9d783ec66181b Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Tue, 22 Nov 2022 18:03:08 +0800 Subject: [PATCH 1/3] ee_515 add new model func --- sqle/model/management_permission.go | 26 +++++++++++++++++++++++--- sqle/model/workflow.go | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/sqle/model/management_permission.go b/sqle/model/management_permission.go index b6ebdc4770..d35c6c60ea 100644 --- a/sqle/model/management_permission.go +++ b/sqle/model/management_permission.go @@ -34,7 +34,7 @@ func GetManagementPermissionDesc(code uint) string { return desc } -func GetManagementPermission() map[uint /*code*/ ]string /*desc*/ { +func GetManagementPermission() map[uint] /*code*/ string /*desc*/ { resp := map[uint]string{} for u, s := range managementPermission2Desc { resp[u] = s @@ -68,7 +68,7 @@ func updateManagementPermission(txDB *gorm.DB, userID uint, permissionCode []uin return nil } -func (s *Storage) GetManagementPermissionByUserID(userID uint) ([]uint, error, ) { +func (s *Storage) GetManagementPermissionByUserID(userID uint) ([]uint, error) { code := []*struct { PermissionCode uint }{} @@ -80,7 +80,7 @@ func (s *Storage) GetManagementPermissionByUserID(userID uint) ([]uint, error, ) return resp, errors.New(errors.ConnectStorageError, err) } -func (s *Storage) GetManagementPermissionByUserIDs(userIDs []uint) (map[uint /*user id*/ ][]uint /*codes*/, error, ) { +func (s *Storage) GetManagementPermissionByUserIDs(userIDs []uint) (map[uint] /*user id*/ []uint /*codes*/, error) { p := []*ManagementPermission{} err := s.db.Table("management_permissions").Select("user_id,permission_code").Where("user_id in (?)", userIDs).Where("deleted_at IS NULL").Scan(&p).Error @@ -95,3 +95,23 @@ func (s *Storage) GetManagementPermissionByUserIDs(userIDs []uint) (map[uint /*u return resp, errors.New(errors.ConnectStorageError, err) } + +func (s *Storage) CheckUserHaveManagementPermission(userID uint, code []uint) (bool, error) { + code = utils.RemoveDuplicateUint(code) + + user, _, err := s.GetUserByID(userID) + if err != nil { + return false, err + } + if user.Name == DefaultAdminUser { + return true, nil + } + + var count int + err = s.db.Model(&ManagementPermission{}). + Where("user_id = ?", userID). + Where("permission_code in (?)", code). + Count(&count).Error + + return count == len(code), errors.ConnectStorageErrWrapper(err) +} \ No newline at end of file diff --git a/sqle/model/workflow.go b/sqle/model/workflow.go index 4c7c50f9a7..263ffd7a32 100644 --- a/sqle/model/workflow.go +++ b/sqle/model/workflow.go @@ -904,6 +904,20 @@ func (s *Storage) GetWorkflowCountByStatus(status string) (int, error) { return count, nil } +// 执行成功, 执行失败, 已取消三种工单会被当作已结束工单 +func (s *Storage) HasNotEndWorkflowByProjectName(projectName string) (bool, error) { + endStatus := []string{WorkflowStatusExecFailed, WorkflowStatusFinish, WorkflowStatusCancel} + + var count int + err := s.db.Table("workflows"). + Joins("LEFT JOIN workflow_records ON workflows.workflow_record_id = workflow_records.id"). + Joins("LEFT JOIN projects ON projects.id = workflows.project_id"). + Where("workflow_records.status NOT IN (?)", endStatus). + Where("projects.name = ?", projectName). + Count(&count).Error + return count > 0, err +} + // GetApprovedWorkflowCount // 返回审核通过的工单数(工单状态是 待上线,正在上线,上线成功,上线失败 中任意一个表示工单通过审核) // 工单状态是 待审核,已驳回,已关闭 中任意一个表示工单未通过审核 From 744b16c5fa0a2a6180943ba48e67954d4f204469 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Wed, 23 Nov 2022 13:21:22 +0800 Subject: [PATCH 2/3] issue 1130 api --- sqle/api/controller/v1/rule.go | 4 +++- sqle/docs/docs.go | 6 ++++++ sqle/docs/swagger.json | 6 ++++++ sqle/docs/swagger.yaml | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sqle/api/controller/v1/rule.go b/sqle/api/controller/v1/rule.go index c83b996d8d..296c7f6d9b 100644 --- a/sqle/api/controller/v1/rule.go +++ b/sqle/api/controller/v1/rule.go @@ -400,7 +400,8 @@ func convertRuleTemplatesToRes(templateNameToInstanceIds map[string][]uint, } type GetRulesReqV1 struct { - FilterDBType string `json:"filter_db_type" query:"filter_db_type"` + FilterDBType string `json:"filter_db_type" query:"filter_db_type"` + FilterGlobalRuleTemplateName string `json:"filter_global_rule_template_name" query:"filter_global_rule_template_name"` } type GetRulesResV1 struct { @@ -463,6 +464,7 @@ func convertRulesToRes(rules []*model.Rule) []RuleResV1 { // @Tags rule_template // @Security ApiKeyAuth // @Param filter_db_type query string false "filter db type" +// @Param filter_global_rule_template_name query string false "filter global rule template name" // @Success 200 {object} v1.GetRulesResV1 // @router /v1/rules [get] func GetRules(c echo.Context) error { diff --git a/sqle/docs/docs.go b/sqle/docs/docs.go index 49c7123f78..e08192f152 100644 --- a/sqle/docs/docs.go +++ b/sqle/docs/docs.go @@ -4573,6 +4573,12 @@ var doc = `{ "description": "filter db type", "name": "filter_db_type", "in": "query" + }, + { + "type": "string", + "description": "filter global rule template name", + "name": "filter_global_rule_template_name", + "in": "query" } ], "responses": { diff --git a/sqle/docs/swagger.json b/sqle/docs/swagger.json index 24e3a2f5b8..45c3564aa0 100644 --- a/sqle/docs/swagger.json +++ b/sqle/docs/swagger.json @@ -4557,6 +4557,12 @@ "description": "filter db type", "name": "filter_db_type", "in": "query" + }, + { + "type": "string", + "description": "filter global rule template name", + "name": "filter_global_rule_template_name", + "in": "query" } ], "responses": { diff --git a/sqle/docs/swagger.yaml b/sqle/docs/swagger.yaml index 48b087b93c..c0dc35c57c 100644 --- a/sqle/docs/swagger.yaml +++ b/sqle/docs/swagger.yaml @@ -6081,6 +6081,10 @@ paths: in: query name: filter_db_type type: string + - description: filter global rule template name + in: query + name: filter_global_rule_template_name + type: string responses: "200": description: OK From 30d3e491d534d1ab4914a6bb79c396fd4cff8953 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Wed, 23 Nov 2022 13:44:46 +0800 Subject: [PATCH 3/3] ee 515 fix old bug --- sqle/model/project.go | 2 +- sqle/model/role.go | 2 ++ sqle/pkg/scanner/client.go | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sqle/model/project.go b/sqle/model/project.go index 2d52c0e829..0db18a4bd5 100644 --- a/sqle/model/project.go +++ b/sqle/model/project.go @@ -105,7 +105,7 @@ func (s *Storage) GetProjectByID(projectID uint) (*Project, bool, error) { return p, true, errors.New(errors.ConnectStorageError, err) } -func (s Storage) GetProjectByName(projectName string) (*Project, bool, error) { +func (s *Storage) GetProjectByName(projectName string) (*Project, bool, error) { p := &Project{} err := s.db.Preload("CreateUser").Preload("Members").Preload("Managers").Preload("Instances"). Where("name = ?", projectName).First(p).Error diff --git a/sqle/model/role.go b/sqle/model/role.go index c586d39e6b..f89a2b82a3 100644 --- a/sqle/model/role.go +++ b/sqle/model/role.go @@ -82,6 +82,7 @@ DELETE project_member_roles FROM project_member_roles LEFT JOIN project_user ON project_user.user_id = project_member_roles.user_id LEFT JOIN projects ON projects.id = project_user.project_id +JOIN instances ON projects.id = instances.project_id AND project_member_roles.instance_id = instances.id WHERE project_member_roles.user_id = ? AND projects.name = ? `, user.ID, projectName).Error @@ -152,6 +153,7 @@ DELETE project_member_group_roles FROM project_member_group_roles LEFT JOIN project_user_group ON project_user_group.user_group_id = project_member_group_roles.user_group_id LEFT JOIN projects ON projects.id = project_user_group.project_id +JOIN instances ON projects.id = instances.project_id AND project_member_group_roles.instance_id = instances.id WHERE project_member_group_roles.user_group_id = ? AND projects.name = ? `, group.ID, projectName).Error diff --git a/sqle/pkg/scanner/client.go b/sqle/pkg/scanner/client.go index 337e863f12..b5dcaf9664 100644 --- a/sqle/pkg/scanner/client.go +++ b/sqle/pkg/scanner/client.go @@ -29,7 +29,7 @@ const ( // Post PartialUpload = "/v1/projects/%v/audit_plans/%s/sqls/partial" // Get %v=report_id - GetAuditReport = "/v1/projects/%v/audit_plans/%s/report/%v/?page_index=%d&page_size=%d" + GetAuditReport = "/v1/projects/%v/audit_plans/%s/reports/%v/sqls?page_index=%d&page_size=%d" ) type (