Skip to content

Commit

Permalink
fix:issue #644 (#645)
Browse files Browse the repository at this point in the history
* fix:issue #644

* fix:移除重复定义的函数

* refactor:checkMobile=>checkMobilePhone

* refactor:调整checkEmail逻辑

* fix:调整错误信息

* refactor:调整错误信息描述
  • Loading branch information
chuntaojun authored Sep 9, 2022
1 parent c3cb151 commit 1c2c031
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 31 deletions.
10 changes: 9 additions & 1 deletion auth/defaultauth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 9 additions & 7 deletions auth/defaultauth/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package defaultauth
import (
"context"
"errors"
"fmt"
"regexp"
"unicode/utf8"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions common/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const (
MaxPlatformNameLength = 128
MaxPlatformDomainLength = 1024
MaxPlatformQPS = 65535

MaxEmailLength = 64
MobilePhoneLength = 11
)

var resourceNameRE = regexp.MustCompile("^[0-9A-Za-z-./:_]+$")
Expand Down
6 changes: 0 additions & 6 deletions polaris-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions store/boltdb/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions store/boltdb/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions store/boltdb/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions store/boltdb/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c2c031

Please sign in to comment.