Skip to content

Commit f1a4e05

Browse files
author
ghost
committed
Improved code flow
1 parent d6e40d7 commit f1a4e05

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

core/src/main/java/org/springframework/security/access/expression/method/ExpressionBasedPreInvocationAdvice.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,42 @@ public boolean before(Authentication authentication, MethodInvocation mi, PreInv
5656
}
5757

5858
private Object findFilterTarget(String filterTargetName, EvaluationContext ctx, MethodInvocation invocation) {
59-
Object filterTarget = null;
60-
61-
Object[] arguments = invocation.getArguments();
62-
63-
StringBuilder assertionMessageBuilder = new StringBuilder();
59+
Object filterTarget;
6460

6561
if (filterTargetName.length() > 0) {
6662
filterTarget = ctx.lookupVariable(filterTargetName);
67-
assertionMessageBuilder.append("Filter target was null, or no argument with name ").append(filterTargetName).append(" found in method");
63+
Assert.notNull(filterTarget,
64+
() -> new StringBuilder("Filter target was null, or no argument with name ").append(filterTargetName).append(" found in method").toString());
65+
assertIsNotOfTypeArray(filterTarget);
66+
67+
return filterTarget;
6868
}
6969

70-
if (filterTargetName.length() == 0 && arguments.length > 1) {
70+
Object[] args = invocation.getArguments();
71+
72+
if (args.length > 1) {
7173
throw new IllegalArgumentException(
7274
"Unable to determine the method argument for filtering. Specify the filter target.");
7375
}
7476

75-
if (filterTargetName.length() == 0 &&
76-
arguments.length == 1 &&
77-
argumentsIsArrayOrCollection(arguments[0])
78-
) {
79-
filterTarget = arguments[0];
80-
assertionMessageBuilder.append("A PreFilter expression was set but the method argument type")
81-
.append(
82-
filterTarget.getClass()).append(" is not filterable");
77+
if (args.length == 1 && firstArgIsArrayOrCollection(args[0])) {
78+
filterTarget = args[0];
79+
Assert.notNull(filterTarget, () -> new StringBuilder("A PreFilter expression was set but the method argument type")
80+
.append(args[0].getClass()).append(" is not filterable").toString());
81+
assertIsNotOfTypeArray(filterTarget);
82+
return filterTarget;
8383
}
8484

85-
Assert.notNull(filterTarget, assertionMessageBuilder.toString());
86-
87-
assertionMessageBuilder.setLength(0);
88-
assertionMessageBuilder.append("Pre-filtering on array types is not supported. Using a Collection will solve this problem");
89-
90-
Assert.isTrue(!filterTarget.getClass().isArray(), assertionMessageBuilder.toString());
91-
92-
93-
return filterTarget;
85+
throw new IllegalStateException("Unexpected either filterTargetName or invocation arguments provided. Provide either the target name or invocation argument with one argument");
9486
}
9587

96-
private boolean argumentsIsArrayOrCollection(Object argument) {
97-
if (argument.getClass().isArray()) {
98-
return true;
99-
}
100-
101-
return argument instanceof Collection<?>;
88+
private boolean firstArgIsArrayOrCollection(Object arg) {
89+
return arg.getClass().isArray() || arg instanceof Collection<?>;
90+
}
10291

92+
private void assertIsNotOfTypeArray(Object filterTarget) {
93+
Assert.isTrue(!filterTarget.getClass().isArray(),
94+
"Pre-filtering on array types is not supported. Using a Collection will solve this problem");
10395
}
10496

10597
public void setExpressionHandler(MethodSecurityExpressionHandler expressionHandler) {

0 commit comments

Comments
 (0)