Skip to content

Commit ff45b56

Browse files
committed
Process interceptor excludes with && instead of ||. Fixes #9853
1 parent 2064310 commit ff45b56

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

grails-plugin-interceptors/src/main/groovy/org/grails/plugins/web/interceptors/UrlMappingMatcher.groovy

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class UrlMappingMatcher implements Matcher {
5959
boolean doesMatch(String uri, UrlMappingInfo info) {
6060
boolean hasUriPatterns = !uriPatterns.isEmpty()
6161

62-
boolean isNotExcluded = !isExcluded(uri, info)
63-
if(matchAll && isNotExcluded) return true
62+
boolean isExcluded = this.isExcluded(uri, info)
63+
if(matchAll && !isExcluded) return true
6464

65-
if(isNotExcluded) {
65+
if(!isExcluded) {
6666
if (hasUriPatterns) {
6767
uri = uri.replace(';', '')
6868
for (pattern in uriPatterns) {
@@ -200,10 +200,15 @@ class UrlMappingMatcher implements Matcher {
200200
Pattern methodExcludesRegex
201201
@Override
202202
boolean isExcluded(UrlMappingInfo info) {
203-
(controllerExcludesRegex != null && ((info.controllerName ?: '') ==~ controllerExcludesRegex)) ||
204-
(actionExcludesRegex != null && ((info.actionName ?: '') ==~ actionExcludesRegex)) ||
205-
(namespaceExcludesRegex != null && ((info.namespace ?: '') ==~ namespaceExcludesRegex)) ||
206-
(methodExcludesRegex != null && ((info.httpMethod ?: '') ==~ methodExcludesRegex))
203+
boolean controllerExclude = controllerExcludesRegex == null || ((info.controllerName ?: '') ==~ controllerExcludesRegex)
204+
boolean actionExclude = actionExcludesRegex == null || ((info.actionName ?: '') ==~ actionExcludesRegex)
205+
boolean namespaceExclude = namespaceExcludesRegex == null || ((info.namespace ?: '') ==~ namespaceExcludesRegex)
206+
boolean methodExclude = methodExcludesRegex == null || ((info.httpMethod ?: '') ==~ methodExcludesRegex)
207+
208+
controllerExclude &&
209+
actionExclude &&
210+
namespaceExclude &&
211+
methodExclude
207212
}
208213
}
209214

grails-plugin-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ class InterceptorSpec extends Specification {
121121

122122
}
123123

124+
void "Test the match all interceptor mappings exception an exact controller action pair"() {
125+
given:"A test interceptor"
126+
def i = new Test4Interceptor()
127+
def webRequest = GrailsWebMockUtil.bindMockWebRequest()
128+
def request = webRequest.request
129+
130+
when:"The current request is for a controller called test"
131+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test"))
132+
then:"We match"
133+
i.doesMatch()
134+
135+
when:"The current request is for a controller called test and action called bar"
136+
clearMatch(i,request)
137+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", actionName: "bar"))
138+
then:"We match"
139+
i.doesMatch()
140+
141+
when:"The current request is for a controller called test and action called bar"
142+
clearMatch(i,request)
143+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "foo", actionName: "bar"))
144+
then:"We match"
145+
!i.doesMatch()
146+
147+
when:"The current request is for another controller"
148+
clearMatch(i,request)
149+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "other"))
150+
then:"We match"
151+
i.doesMatch()
152+
153+
when:"The current request is for an excluded controller controller"
154+
clearMatch(i,request)
155+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "foo"))
156+
then:"We don't match"
157+
i.doesMatch()
158+
}
124159
void clearMatch(i, HttpServletRequest request) {
125160
request.removeAttribute(i.getClass().name + InterceptorArtefactHandler.MATCH_SUFFIX)
126161
}
@@ -155,4 +190,11 @@ class Test3Interceptor implements Interceptor {
155190
matchAll()
156191
.excludes(controller:"foo")
157192
}
193+
}
194+
195+
class Test4Interceptor implements Interceptor {
196+
Test4Interceptor() {
197+
matchAll()
198+
.excludes(controller:"foo", action:"bar")
199+
}
158200
}

0 commit comments

Comments
 (0)