Skip to content

Commit

Permalink
refactor: ConditionalOnRole (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Mar 20, 2024
1 parent 9ec77ab commit 106d483
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
* @return
*/
String[] value();

boolean any() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnRole.class.getName(), true);
String[] anyRole = (String[]) attributes.get("value");

String[] expectedRoles = (String[]) attributes.get("value");
boolean any = (Boolean) attributes.get("any");
String roles = context.getEnvironment().getProperty("holoinsight.roles.active", "");
Iterable<String> iter = Splitter.on(',').trimResults().omitEmptyStrings().split(roles);

for (String role : anyRole) {
if (Iterables.contains(iter, role)) {
return ConditionOutcome.match("match '" + role + "' role");
if (any) {
for (String role : expectedRoles) {
if (Iterables.contains(iter, role)) {
return ConditionOutcome.match("match '" + role + "' role");
}
}
} else {
for (String role : expectedRoles) {
if (!Iterables.contains(iter, role)) {
return ConditionOutcome.match("no match '" + role + "' role");
}
}
}

return ConditionOutcome.noMatch("no any roles: " + Arrays.toString(anyRole));
return ConditionOutcome.noMatch("no match roles: " + Arrays.toString(expectedRoles));
}
}

0 comments on commit 106d483

Please sign in to comment.