From 1c2c0317db0a18c05597d04816a020f878be1d1f Mon Sep 17 00:00:00 2001 From: liaochuntao Date: Fri, 9 Sep 2022 16:54:26 +0800 Subject: [PATCH] fix:issue #644 (#645) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:issue #644 * fix:移除重复定义的函数 * refactor:checkMobile=>checkMobilePhone * refactor:调整checkEmail逻辑 * fix:调整错误信息 * refactor:调整错误信息描述 --- auth/defaultauth/user.go | 10 +++++++++- auth/defaultauth/utils.go | 16 +++++++++------- common/utils/common.go | 3 +++ polaris-server.yaml | 6 ------ service/instance.go | 15 --------------- store/boltdb/default.go | 2 -- store/boltdb/group.go | 4 ++++ store/boltdb/strategy.go | 6 ++++++ store/boltdb/user.go | 12 ++++++++++++ 9 files changed, 43 insertions(+), 31 deletions(-) diff --git a/auth/defaultauth/user.go b/auth/defaultauth/user.go index 307b63daa..fe8b3ffe1 100644 --- a/auth/defaultauth/user.go +++ b/auth/defaultauth/user.go @@ -559,7 +559,7 @@ func checkCreateUser(req *api.User) *api.Response { return api.NewUserResponse(api.InvalidUserOwners, req) } - if err := checkMobile(req.Mobile); err != nil { + if err := checkMobilePhone(req.Mobile); err != nil { return api.NewUserResponse(api.InvalidUserMobile, req) } @@ -587,6 +587,14 @@ func checkUpdateUser(req *api.User) *api.Response { return api.NewUserResponse(api.BadRequest, req) } + if err := checkMobilePhone(req.Mobile); err != nil { + return api.NewUserResponse(api.InvalidUserMobile, req) + } + + if err := checkEmail(req.Email); err != nil { + return api.NewUserResponse(api.InvalidUserEmail, req) + } + return nil } diff --git a/auth/defaultauth/utils.go b/auth/defaultauth/utils.go index 8a08f3019..ceaeaa9ff 100644 --- a/auth/defaultauth/utils.go +++ b/auth/defaultauth/utils.go @@ -20,6 +20,7 @@ package defaultauth import ( "context" "errors" + "fmt" "regexp" "unicode/utf8" @@ -59,7 +60,6 @@ var storeCodeAPICodeMap = map[store.StatusCode]uint32{ var ( regNameStr = regexp.MustCompile("^[\u4E00-\u9FA5A-Za-z0-9_\\-]+$") - regEmail = regexp.MustCompile(`^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$`) ) // StoreCode2APICode store code to api code @@ -130,8 +130,8 @@ func checkOwner(owner *wrappers.StringValue) error { return nil } -// checkMobile 检查用户的 mobile 信息 -func checkMobile(mobile *wrappers.StringValue) error { +// checkMobilePhone 检查用户的 mobile phone 信息 +func checkMobilePhone(mobile *wrappers.StringValue) error { if mobile == nil { return nil } @@ -140,8 +140,9 @@ func checkMobile(mobile *wrappers.StringValue) error { return nil } - if utf8.RuneCountInString(mobile.GetValue()) != 11 { - return errors.New("invalid mobile") + if utf8.RuneCountInString(mobile.GetValue()) > utils.MobilePhoneLength { + return fmt.Errorf("invalid mobile, current is %s, length must be less than %d", + mobile.GetValue(), utils.MobilePhoneLength) } return nil @@ -157,8 +158,9 @@ func checkEmail(email *wrappers.StringValue) error { return nil } - if ok := regEmail.MatchString(email.GetValue()); !ok { - return errors.New("invalid email") + if utf8.RuneCountInString(email.GetValue()) > utils.MaxEmailLength { + return fmt.Errorf("invalid email, current is %s, length must be less than %d", + email.GetValue(), utils.MaxEmailLength) } return nil diff --git a/common/utils/common.go b/common/utils/common.go index 76d449c81..805bc1af2 100644 --- a/common/utils/common.go +++ b/common/utils/common.go @@ -91,6 +91,9 @@ const ( MaxPlatformNameLength = 128 MaxPlatformDomainLength = 1024 MaxPlatformQPS = 65535 + + MaxEmailLength = 64 + MobilePhoneLength = 11 ) var resourceNameRE = regexp.MustCompile("^[0-9A-Za-z-./:_]+$") diff --git a/polaris-server.yaml b/polaris-server.yaml index fe26c3115..4ab09b1bc 100644 --- a/polaris-server.yaml +++ b/polaris-server.yaml @@ -78,12 +78,6 @@ bootstrap: enable_register: true isolated: false services: - - name: polaris.discover - protocols: - - service-grpc - - name: polaris.healthcheck - protocols: - - service-grpc - name: polaris.checker protocols: - service-grpc diff --git a/service/instance.go b/service/instance.go index 883f78456..ad17f85be 100644 --- a/service/instance.go +++ b/service/instance.go @@ -610,21 +610,6 @@ func (s *Server) updateInstanceAttribute(req *api.Instance, instance *model.Inst return needUpdate } -func instanceLocationNeedUpdate(req *api.Location, old *api.Location) bool { - - if req.GetRegion().GetValue() != old.GetRegion().GetValue() { - return true - } - if req.GetZone().GetValue() != old.GetZone().GetValue() { - return true - } - if req.GetCampus().GetValue() != old.GetCampus().GetValue() { - return true - } - - return false -} - // 健康检查的更新 func updateHealthCheck(req *api.Instance, instance *model.Instance) bool { needUpdate := false diff --git a/store/boltdb/default.go b/store/boltdb/default.go index 8b2959734..01c8d26f0 100644 --- a/store/boltdb/default.go +++ b/store/boltdb/default.go @@ -111,8 +111,6 @@ const ( var ( namespacesToInit = []string{"default", namespacePolaris} servicesToInit = map[string]string{ - "polaris.discover": "1866010b40be6542db1a2cc846c7f51f", - "polaris.healthcheck": "846c1866010b40b7f51fe6542db1a2cc", "polaris.checker": "fbca9bfa04ae4ead86e1ecf5811e32a9", "polaris.monitor": "bbfdda174ea64e11ac862adf14593c03", "polaris.config": "e6542db1a2cc846c1866010b40b7f51f", diff --git a/store/boltdb/group.go b/store/boltdb/group.go index 970364567..e58a5c1a6 100644 --- a/store/boltdb/group.go +++ b/store/boltdb/group.go @@ -460,6 +460,10 @@ func doGroupPage(ret map[string]interface{}, offset uint32, limit uint32) []*mod // GetGroupsForCache 查询用户分组数据,主要用于Cache更新 func (gs *groupStore) GetGroupsForCache(mtime time.Time, firstUpdate bool) ([]*model.UserGroupDetail, error) { + if firstUpdate { + mtime = time.Time{} + } + ret, err := gs.handler.LoadValuesByFilter(tblGroup, []string{GroupFieldModifyTime}, &groupForStore{}, func(m map[string]interface{}) bool { mt := m[GroupFieldModifyTime].(time.Time) diff --git a/store/boltdb/strategy.go b/store/boltdb/strategy.go index b473fc270..4f30b8f67 100644 --- a/store/boltdb/strategy.go +++ b/store/boltdb/strategy.go @@ -165,6 +165,7 @@ func (ss *strategyStore) updateStrategy(tx *bolt.Tx, modify *model.ModifyStrateg computeResources(false, modify.AddResources, saveVal) computeResources(true, modify.RemoveResources, saveVal) + saveVal.ModifyTime = time.Now() if err := saveValue(tx, tblStrategy, saveVal.ID, saveVal); err != nil { logger.StoreScope().Error("[Store][Strategy] update auth_strategy", zap.Error(err), zap.String("id", saveVal.ID)) @@ -279,6 +280,7 @@ func (ss *strategyStore) operateStrategyResources(remove bool, resources []model } computeResources(remove, ress, rule) + rule.ModifyTime = time.Now() if err := saveValue(tx, tblStrategy, rule.ID, rule); err != nil { logger.StoreScope().Error("[Store][Strategy] operate strategy resource", zap.Error(err), zap.Bool("remove", remove), zap.String("id", id)) @@ -669,6 +671,10 @@ func comparePrincipalExist(principalType, principalId string, m map[string]inter func (ss *strategyStore) GetStrategyDetailsForCache(mtime time.Time, firstUpdate bool) ([]*model.StrategyDetail, error) { + if firstUpdate { + mtime = time.Time{} + } + ret, err := ss.handler.LoadValuesByFilter(tblStrategy, []string{StrategyFieldModifyTime}, &strategyForStore{}, func(m map[string]interface{}) bool { mt := m[StrategyFieldModifyTime].(time.Time) diff --git a/store/boltdb/user.go b/store/boltdb/user.go index ef4fc0c52..2cb8b24e5 100644 --- a/store/boltdb/user.go +++ b/store/boltdb/user.go @@ -142,6 +142,8 @@ func (us *userStore) UpdateUser(user *model.User) error { properties[UserFieldToken] = user.Token properties[UserFieldTokenEnable] = user.TokenEnable properties[UserFieldPassword] = user.Password + properties[UserFieldEmail] = user.Email + properties[UserFieldMobile] = user.Mobile properties[UserFieldModifyTime] = time.Now() err := us.handler.UpdateValue(tblUser, user.ID, properties) @@ -396,6 +398,12 @@ func (us *userStore) getUsers(filters map[string]string, offset uint32, limit ui } } + if queryId, ok := filters["id"]; ok { + if queryId != saveId { + return false + } + } + return true }) @@ -490,6 +498,10 @@ func (us *userStore) getGroupUsers(filters map[string]string, offset uint32, lim // GetUsersForCache func (us *userStore) GetUsersForCache(mtime time.Time, firstUpdate bool) ([]*model.User, error) { + if firstUpdate { + mtime = time.Time{} + } + ret, err := us.handler.LoadValuesByFilter(tblUser, []string{UserFieldModifyTime}, &userForStore{}, func(m map[string]interface{}) bool { mt := m[UserFieldModifyTime].(time.Time)