Skip to content

Commit 13048b5

Browse files
author
Vladimir Kotal
committed
use stream() for the final OPTIONAL check
1 parent 98df528 commit 13048b5

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/authorization/AuthorizationStack.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Locale;
2929
import java.util.Map;
3030
import java.util.TreeMap;
31+
import java.util.function.Predicate;
3132
import java.util.logging.Level;
3233
import java.util.logging.Logger;
3334

@@ -219,7 +220,7 @@ protected boolean processStack(Nameable entity,
219220
PluginDecisionPredicate pluginPredicate,
220221
PluginSkippingPredicate skippingPredicate) {
221222

222-
Boolean overallDecision = null;
223+
boolean overallDecision = true;
223224
boolean optionalFailure = false;
224225

225226
if (getStack().isEmpty()) {
@@ -253,14 +254,9 @@ protected boolean processStack(Nameable entity,
253254
break;
254255
} else if (!entityDecision && authEntity.isOptional()) {
255256
optionalFailure = true;
256-
} else if (entityDecision && authEntity.isSufficient()) {
257+
} else if (overallDecision && entityDecision && authEntity.isSufficient()) {
257258
// sufficient immediately returns the success
258-
if ((overallDecision == null) || overallDecision) {
259-
overallDecision = true;
260-
break;
261-
}
262-
} else if (overallDecision == null && entityDecision) {
263-
overallDecision = true;
259+
break;
264260
}
265261
} catch (AuthorizationException ex) {
266262
// Propagate up so that proper HTTP error can be given.
@@ -292,14 +288,12 @@ protected boolean processStack(Nameable entity,
292288
}
293289
}
294290

295-
if (overallDecision == null && optionalFailure) {
291+
if (optionalFailure &&
292+
getStack().stream().filter(AuthorizationEntity::isOptional).count() == 1 &&
293+
getStack().stream().filter(Predicate.not(AuthorizationEntity::isOptional)).findAny().isEmpty()) {
296294
return false;
297295
}
298296

299-
if (overallDecision == null) {
300-
return true;
301-
}
302-
303297
return overallDecision;
304298
}
305299

opengrok-indexer/src/test/java/org/opengrok/indexer/authorization/AuthorizationFrameworkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public static StackSetup[][] params() {
716716
NewTest(true, createUnallowedProject()),
717717
NewTest(true, createUnallowedGroup()),
718718
// optional plugin1 returns true
719-
// optional plugin2 returns false
719+
// optional plugin2 returns false => true
720720
NewTest(true, createAllowedProject()),
721721
NewTest(true, createAllowedGroup()))
722722
},

0 commit comments

Comments
 (0)