Skip to content

Commit

Permalink
Process interceptor excludes with && instead of ||. Fixes #9853
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Apr 13, 2016
1 parent 2064310 commit ff45b56
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
Expand Up @@ -59,10 +59,10 @@ class UrlMappingMatcher implements Matcher {
boolean doesMatch(String uri, UrlMappingInfo info) {
boolean hasUriPatterns = !uriPatterns.isEmpty()

boolean isNotExcluded = !isExcluded(uri, info)
if(matchAll && isNotExcluded) return true
boolean isExcluded = this.isExcluded(uri, info)
if(matchAll && !isExcluded) return true

if(isNotExcluded) {
if(!isExcluded) {
if (hasUriPatterns) {
uri = uri.replace(';', '')
for (pattern in uriPatterns) {
Expand Down Expand Up @@ -200,10 +200,15 @@ class UrlMappingMatcher implements Matcher {
Pattern methodExcludesRegex
@Override
boolean isExcluded(UrlMappingInfo info) {
(controllerExcludesRegex != null && ((info.controllerName ?: '') ==~ controllerExcludesRegex)) ||
(actionExcludesRegex != null && ((info.actionName ?: '') ==~ actionExcludesRegex)) ||
(namespaceExcludesRegex != null && ((info.namespace ?: '') ==~ namespaceExcludesRegex)) ||
(methodExcludesRegex != null && ((info.httpMethod ?: '') ==~ methodExcludesRegex))
boolean controllerExclude = controllerExcludesRegex == null || ((info.controllerName ?: '') ==~ controllerExcludesRegex)
boolean actionExclude = actionExcludesRegex == null || ((info.actionName ?: '') ==~ actionExcludesRegex)
boolean namespaceExclude = namespaceExcludesRegex == null || ((info.namespace ?: '') ==~ namespaceExcludesRegex)
boolean methodExclude = methodExcludesRegex == null || ((info.httpMethod ?: '') ==~ methodExcludesRegex)

controllerExclude &&
actionExclude &&
namespaceExclude &&
methodExclude
}
}

Expand Down
Expand Up @@ -121,6 +121,41 @@ class InterceptorSpec extends Specification {

}

void "Test the match all interceptor mappings exception an exact controller action pair"() {
given:"A test interceptor"
def i = new Test4Interceptor()
def webRequest = GrailsWebMockUtil.bindMockWebRequest()
def request = webRequest.request

when:"The current request is for a controller called test"
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test"))
then:"We match"
i.doesMatch()

when:"The current request is for a controller called test and action called bar"
clearMatch(i,request)
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", actionName: "bar"))
then:"We match"
i.doesMatch()

when:"The current request is for a controller called test and action called bar"
clearMatch(i,request)
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "foo", actionName: "bar"))
then:"We match"
!i.doesMatch()

when:"The current request is for another controller"
clearMatch(i,request)
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "other"))
then:"We match"
i.doesMatch()

when:"The current request is for an excluded controller controller"
clearMatch(i,request)
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "foo"))
then:"We don't match"
i.doesMatch()
}
void clearMatch(i, HttpServletRequest request) {
request.removeAttribute(i.getClass().name + InterceptorArtefactHandler.MATCH_SUFFIX)
}
Expand Down Expand Up @@ -155,4 +190,11 @@ class Test3Interceptor implements Interceptor {
matchAll()
.excludes(controller:"foo")
}
}

class Test4Interceptor implements Interceptor {
Test4Interceptor() {
matchAll()
.excludes(controller:"foo", action:"bar")
}
}

0 comments on commit ff45b56

Please sign in to comment.