Skip to content

Commit

Permalink
Merge pull request #533 from actiontech/main
Browse files Browse the repository at this point in the history
merge Main to ee
  • Loading branch information
HuangWeiCen authored Nov 23, 2022
2 parents e7e4945 + 5cbeaf3 commit 12c0a4c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 6 deletions.
4 changes: 3 additions & 1 deletion sqle/api/controller/v1/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions sqle/model/management_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}{}
Expand All @@ -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

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion sqle/model/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sqle/model/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions sqle/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 返回审核通过的工单数(工单状态是 待上线,正在上线,上线成功,上线失败 中任意一个表示工单通过审核)
// 工单状态是 待审核,已驳回,已关闭 中任意一个表示工单未通过审核
Expand Down
2 changes: 1 addition & 1 deletion sqle/pkg/scanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 12c0a4c

Please sign in to comment.