Browse Source

Routing: Add regexp syntax support to UserMatcher (#3799)

pull/3809/head
vnxme 2 months ago committed by GitHub
parent
commit
3a8c5f38e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 19
      app/router/condition.go

19
app/router/condition.go

@ -192,18 +192,28 @@ func (v NetworkMatcher) Apply(ctx routing.Context) bool {
}
type UserMatcher struct {
user []string
user []string
pattern []*regexp.Regexp
}
func NewUserMatcher(users []string) *UserMatcher {
usersCopy := make([]string, 0, len(users))
patternsCopy := make([]*regexp.Regexp, 0, len(users))
for _, user := range users {
if len(user) > 0 {
if len(user) > 7 && strings.HasPrefix(user, "regexp:") {
if re, err := regexp.Compile(user[7:]); err != nil {
patternsCopy = append(patternsCopy, re)
}
// Items of users slice with an invalid regexp syntax are ignored.
continue
}
usersCopy = append(usersCopy, user)
}
}
return &UserMatcher{
user: usersCopy,
user: usersCopy,
pattern: patternsCopy,
}
}
@ -218,6 +228,11 @@ func (v *UserMatcher) Apply(ctx routing.Context) bool {
return true
}
}
for _, re := range v.pattern {
if re.MatchString(user) {
return true
}
}
return false
}

Loading…
Cancel
Save