From 737c6aec779ae3aff59e59fdf82fcadd06dfc985 Mon Sep 17 00:00:00 2001 From: BugsGuru Date: Fri, 1 Nov 2024 10:27:26 +0800 Subject: [PATCH 1/3] read sqle OptimizationConfig to get IsOptimizationEnabled --- internal/apiserver/conf/options.go | 14 ++++++++++++-- internal/apiserver/conf/options_ce.go | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 internal/apiserver/conf/options_ce.go diff --git a/internal/apiserver/conf/options.go b/internal/apiserver/conf/options.go index 93a1d60a..cbc07330 100644 --- a/internal/apiserver/conf/options.go +++ b/internal/apiserver/conf/options.go @@ -7,8 +7,18 @@ import ( pkgParams "github.com/actiontech/dms/pkg/params" ) +var IsOptimizationEnabled bool + type Options struct { - DMS DMSOptions `yaml:"dms" validate:"required"` + DMS DMSOptions `yaml:"dms" validate:"required"` + SQLE SQLEOptions `yaml:"sqle"` +} + +type SQLEOptions struct { + OptimizationConfig struct { + OptimizationKey string `yaml:"optimization_key"` + OptimizationUrl string `yaml:"optimization_url"` + } `yaml:"optimization_config"` } type DMSOptions struct { @@ -55,6 +65,6 @@ func ReadOptions(log utilLog.Logger, path string) (*DMSOptions, error) { if err := utilConf.ParseYamlFile(log, path, &opts); err != nil { return nil, err } - + IsOptimizationEnabled = getOptimizationEnabled(&opts) return &opts.DMS, nil } diff --git a/internal/apiserver/conf/options_ce.go b/internal/apiserver/conf/options_ce.go new file mode 100644 index 00000000..540b204a --- /dev/null +++ b/internal/apiserver/conf/options_ce.go @@ -0,0 +1,7 @@ +//go:build !enterprise + +package conf + +func getOptimizationEnabled(opt *Options) bool { + return false +} From 2743a0a226dcf9441b29914f6b0fe226159dbfa2 Mon Sep 17 00:00:00 2001 From: BugsGuru Date: Fri, 1 Nov 2024 10:28:16 +0800 Subject: [PATCH 2/3] hide Optimization when not supported --- internal/dms/service/op_permission.go | 16 ++++++++++++++++ internal/dms/service/role.go | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/internal/dms/service/op_permission.go b/internal/dms/service/op_permission.go index 4089d33c..5dea77d5 100644 --- a/internal/dms/service/op_permission.go +++ b/internal/dms/service/op_permission.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/actiontech/dms/internal/apiserver/conf" pkgConst "github.com/actiontech/dms/internal/dms/pkg/constant" "github.com/actiontech/dms/internal/pkg/locale" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -68,6 +69,21 @@ func (d *DMSService) ListOpPermissions(ctx context.Context, req *dmsV1.ListOpPer OrderBy: orderBy, } + // 不支持智能调优时,隐藏相关权限 + if !conf.IsOptimizationEnabled { + listOption.FilterBy = append(listOption.FilterBy, + pkgConst.FilterCondition{ + Field: string(biz.OpPermissionFieldUID), + Operator: pkgConst.FilterOperatorNotEqual, + Value: pkgConst.UIDOfOpPermissionCreateOptimization, + }, + pkgConst.FilterCondition{ + Field: string(biz.OpPermissionFieldUID), + Operator: pkgConst.FilterOperatorNotEqual, + Value: pkgConst.UIDOfOpPermissionViewOthersOptimization, + }) + } + var ops []*biz.OpPermission var total int64 switch req.FilterByTarget { diff --git a/internal/dms/service/role.go b/internal/dms/service/role.go index 7a7a0f7f..8a31d8e4 100644 --- a/internal/dms/service/role.go +++ b/internal/dms/service/role.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/actiontech/dms/internal/apiserver/conf" "github.com/actiontech/dms/internal/pkg/locale" dmsCommonV1 "github.com/actiontech/dms/pkg/dms-common/api/dms/v1" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -137,6 +138,11 @@ func (d *DMSService) ListRoles(ctx context.Context, req *dmsV1.ListRoleReq) (rep return nil, err } for _, op := range ops { + // 不支持智能调优时,隐藏相关权限 + if !conf.IsOptimizationEnabled && + (op.UID == pkgConst.UIDOfOpPermissionCreateOptimization || op.UID == pkgConst.UIDOfOpPermissionViewOthersOptimization) { + continue + } ret[i].OpPermissions = append(ret[i].OpPermissions, dmsV1.UidWithName{ Uid: op.GetUID(), Name: locale.Bundle.LocalizeMsgByCtx(ctx, OpPermissionNameByUID[op.GetUID()]), From f553cbafc36e97937dcdadeaf30b231fc2cd5cda Mon Sep 17 00:00:00 2001 From: BugsGuru Date: Thu, 7 Nov 2024 14:23:13 +0800 Subject: [PATCH 3/3] make var optimizationEnabled private --- internal/apiserver/conf/options.go | 10 +++++++--- internal/dms/service/op_permission.go | 2 +- internal/dms/service/role.go | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/apiserver/conf/options.go b/internal/apiserver/conf/options.go index cbc07330..59ef2b3c 100644 --- a/internal/apiserver/conf/options.go +++ b/internal/apiserver/conf/options.go @@ -7,8 +7,6 @@ import ( pkgParams "github.com/actiontech/dms/pkg/params" ) -var IsOptimizationEnabled bool - type Options struct { DMS DMSOptions `yaml:"dms" validate:"required"` SQLE SQLEOptions `yaml:"sqle"` @@ -60,11 +58,17 @@ type DatabaseDriverOption struct { Params pkgParams.Params `yaml:"params"` } +var optimizationEnabled bool + +func IsOptimizationEnabled() bool { + return optimizationEnabled +} + func ReadOptions(log utilLog.Logger, path string) (*DMSOptions, error) { var opts Options if err := utilConf.ParseYamlFile(log, path, &opts); err != nil { return nil, err } - IsOptimizationEnabled = getOptimizationEnabled(&opts) + optimizationEnabled = getOptimizationEnabled(&opts) return &opts.DMS, nil } diff --git a/internal/dms/service/op_permission.go b/internal/dms/service/op_permission.go index 5dea77d5..00d83fa8 100644 --- a/internal/dms/service/op_permission.go +++ b/internal/dms/service/op_permission.go @@ -70,7 +70,7 @@ func (d *DMSService) ListOpPermissions(ctx context.Context, req *dmsV1.ListOpPer } // 不支持智能调优时,隐藏相关权限 - if !conf.IsOptimizationEnabled { + if !conf.IsOptimizationEnabled() { listOption.FilterBy = append(listOption.FilterBy, pkgConst.FilterCondition{ Field: string(biz.OpPermissionFieldUID), diff --git a/internal/dms/service/role.go b/internal/dms/service/role.go index 8a31d8e4..7c9b49ee 100644 --- a/internal/dms/service/role.go +++ b/internal/dms/service/role.go @@ -139,7 +139,7 @@ func (d *DMSService) ListRoles(ctx context.Context, req *dmsV1.ListRoleReq) (rep } for _, op := range ops { // 不支持智能调优时,隐藏相关权限 - if !conf.IsOptimizationEnabled && + if !conf.IsOptimizationEnabled() && (op.UID == pkgConst.UIDOfOpPermissionCreateOptimization || op.UID == pkgConst.UIDOfOpPermissionViewOthersOptimization) { continue }