Simplify MatchUserWithAllowedRegexes
This used to return an error back when it was dealing with wildcards (which may or may not have compiled to a valid regex). But it now deals with pre-compiled regexes and has no chance of failing, so we need no `error` returns.
This commit is contained in:
@@ -23,19 +23,19 @@ func WildcardUserPatternsToRegexPatterns(wildCardPatterns []string) (*[]*regexp.
|
|||||||
|
|
||||||
// MatchUserWithAllowedRegexes tells if the given user id is allowed to use the bot, according to the given whitelist
|
// MatchUserWithAllowedRegexes tells if the given user id is allowed to use the bot, according to the given whitelist
|
||||||
// An empty whitelist means "everyone is allowed"
|
// An empty whitelist means "everyone is allowed"
|
||||||
func MatchUserWithAllowedRegexes(userID string, allowed []*regexp.Regexp) (bool, error) {
|
func MatchUserWithAllowedRegexes(userID string, allowed []*regexp.Regexp) bool {
|
||||||
// No whitelisted users means everyone is whitelisted
|
// No whitelisted users means everyone is whitelisted
|
||||||
if len(allowed) == 0 {
|
if len(allowed) == 0 {
|
||||||
return true, nil
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, regex := range allowed {
|
for _, regex := range allowed {
|
||||||
if regex.MatchString(userID) {
|
if regex.MatchString(userID) {
|
||||||
return true, nil
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseAllowedUserRule parses a user whitelisting rule and returns a regular expression which corresponds to it
|
// parseAllowedUserRule parses a user whitelisting rule and returns a regular expression which corresponds to it
|
||||||
|
|||||||
@@ -202,10 +202,7 @@ func TestMatch(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualResult, err := MatchUserWithAllowedRegexes(testData.checkedValue, *allowedUserRegexes)
|
actualResult := MatchUserWithAllowedRegexes(testData.checkedValue, *allowedUserRegexes)
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if actualResult == testData.expectedResult {
|
if actualResult == testData.expectedResult {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user