Skip to content

Commit 3518e5d

Browse files
committed
Add some helper functions to the SpecExpressionContext
1 parent d4171fd commit 3518e5d

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/spec/expression/SpecExpressionContext.java

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import eu.openanalytics.containerproxy.service.UserService;
2828
import org.keycloak.KeycloakPrincipal;
2929
import org.springframework.security.core.Authentication;
30-
import org.springframework.security.core.userdetails.User;
3130
import org.springframework.security.ldap.userdetails.LdapUserDetails;
3231
import org.springframework.security.saml.SAMLCredential;
3332

3433
import java.util.Arrays;
34+
import java.util.Collections;
3535
import java.util.List;
36+
import java.util.stream.Collectors;
3637

3738
public class SpecExpressionContext {
3839

@@ -77,6 +78,63 @@ public List<String> getGroups() {
7778
return groups;
7879
}
7980

81+
/**
82+
* Convert a {@see String} to a list of strings, by splitting according to the provided regex and trimming each result
83+
*/
84+
public List<String> toList(String attribute, String regex) {
85+
if (attribute == null) {
86+
return Collections.emptyList();
87+
}
88+
return Arrays.stream(attribute.split(regex)).map(String::trim).collect(Collectors.toList());
89+
}
90+
91+
/**
92+
* Convert a {@see String} to a list of strings, by splitting on `,` and trimming each result
93+
*/
94+
public List<String> toList(String attribute) {
95+
return toList(attribute, ",");
96+
}
97+
98+
/**
99+
* Convert a {@see String} to a list of strings, by splitting on according to the provided regex.
100+
* Each result is trimmed and converted to lowercase.
101+
*/
102+
public List<String> toLowerCaseList(String attribute, String regex) {
103+
if (attribute == null) {
104+
return Collections.emptyList();
105+
}
106+
return Arrays.stream(attribute.split(regex)).map(it -> it.trim().toLowerCase()).collect(Collectors.toList());
107+
}
108+
109+
/**
110+
* Convert a {@see String} to a list of strings, by splitting on `,`. Each result is trimmed and converted to lowercase.
111+
*/
112+
public List<String> toLowerCaseList(String attribute) {
113+
return toLowerCaseList(attribute, ",");
114+
}
115+
116+
/**
117+
* Returns true when the provided value is in the list of allowed values.
118+
* Both the attribute and allowed values are trimmed.
119+
*/
120+
public boolean isOneOf(String attribute, String... allowedValues) {
121+
if (attribute == null) {
122+
return false;
123+
}
124+
return Arrays.stream(allowedValues).anyMatch(it -> it.trim().equals(attribute.trim()));
125+
}
126+
127+
/**
128+
* Returns true when the provided value is in the list of allowed values.
129+
* Both the attribute and allowed values are trimmed and the comparison ignores casing of the values.
130+
*/
131+
public boolean isOneOfIgnoreCase(String attribute, String... allowedValues) {
132+
if (attribute == null) {
133+
return false;
134+
}
135+
return Arrays.stream(allowedValues).anyMatch(it -> it.trim().equalsIgnoreCase(attribute.trim()));
136+
}
137+
80138
public static SpecExpressionContext create(Object... objects) {
81139
SpecExpressionContext ctx = new SpecExpressionContext();
82140
for (Object o : objects) {
@@ -96,7 +154,7 @@ public static SpecExpressionContext create(Object... objects) {
96154
ctx.ldapUser = (LdapUserDetails) o;
97155
}
98156
if (o instanceof Authentication) {
99-
ctx.groups = Arrays.asList(UserService.getGroups((Authentication) o));
157+
ctx.groups = Arrays.asList(UserService.getGroups((Authentication) o));
100158
}
101159
}
102160
return ctx;

0 commit comments

Comments
 (0)