2727import eu .openanalytics .containerproxy .service .UserService ;
2828import org .keycloak .KeycloakPrincipal ;
2929import org .springframework .security .core .Authentication ;
30- import org .springframework .security .core .userdetails .User ;
3130import org .springframework .security .ldap .userdetails .LdapUserDetails ;
3231import org .springframework .security .saml .SAMLCredential ;
3332
3433import java .util .Arrays ;
34+ import java .util .Collections ;
3535import java .util .List ;
36+ import java .util .stream .Collectors ;
3637
3738public 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