Skip to content
Permalink
Browse files
allow mapping of complex user properties that implement the Supplier …
…interface
  • Loading branch information
iancu committed Apr 6, 2022
1 parent a7d9bc5 commit c7d16e8dfa6660a6a9677edbdd1e54c97903c1af
Showing 3 changed files with 52 additions and 0 deletions.
@@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import javax.jcr.Binary;
import javax.jcr.RepositoryException;
@@ -624,6 +625,9 @@ protected void syncProperties(@NotNull ExternalIdentity ext, @NotNull Authorizab
String relPath = entry.getKey();
String name = entry.getValue();
Object obj = properties.get(name);
if (obj instanceof Supplier) {
obj = ((Supplier) obj).get();
}
if (obj == null) {
int nameLen = name.length();
if (nameLen > 1 && name.charAt(0) == '"' && name.charAt(nameLen-1) == '"') {
@@ -16,12 +16,14 @@
*/
package org.apache.jackrabbit.oak.spi.security.authentication.external;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import javax.jcr.Credentials;
import javax.jcr.SimpleCredentials;
@@ -283,4 +285,20 @@ public Iterable<ExternalIdentityRef> getDeclaredMembers() {
return ImmutableList.of();
}
}
public static class UserWithSupplierProperties extends TestUser {

public final String stringValue = "customValue";
public final BigDecimal bigDecimal = new BigDecimal("99.99");
public final Boolean boolValue = Boolean.TRUE;

public UserWithSupplierProperties(String userId, @NotNull String idpName) {
super(userId, idpName);
withProperty("profile/id", "zw2")
.withProperty("profile/name", "Supplied properties user")
.withProperty("stringSuppliedValue", (Supplier) () -> stringValue)
.withProperty("bigDecimalSuppliedValue", (Supplier) () -> bigDecimal)
.withProperty("nullSupplier", (Supplier) () -> null)
.withProperty("booleanSuppliedValue", (Supplier) () -> boolValue);
}
}
}
@@ -1201,6 +1201,36 @@ public void testSyncPropertiesRemapped() throws Exception {
}
}

@Test
public void testSyncPropertiesSupplier() throws Exception {
//given
TestIdentityProvider.UserWithSupplierProperties externalUser = new TestIdentityProvider.UserWithSupplierProperties("supplierPropsUser", "idp");
Authorizable a = syncCtx.createUser(externalUser);

// create exact mapping
Map<String, String> mapping = new HashMap<>();
Map<String, ?> extProps = externalUser.getProperties();
for (String propName : extProps.keySet()) {
mapping.put("a/"+propName, propName);
}
//when
syncCtx.syncProperties(externalUser, a, mapping);

//then
assertFalse(a.hasProperty("a/nullSupplier"));
assertTrue(a.hasProperty("a/profile/id"));
assertTrue(a.hasProperty("a/profile/name"));
assertTrue(a.hasProperty("a/stringSuppliedValue"));
assertTrue(a.hasProperty("a/bigDecimalSuppliedValue"));
assertTrue(a.hasProperty("a/booleanSuppliedValue"));

assertEquals(externalUser.getProperties().get("profile/id"), a.getProperty("a/profile/id")[0].getString());
assertEquals(externalUser.getProperties().get("profile/name"), a.getProperty("a/profile/name")[0].getString());
assertEquals(externalUser.stringValue, a.getProperty("a/stringSuppliedValue")[0].getString());
assertEquals(externalUser.bigDecimal, a.getProperty("a/bigDecimalSuppliedValue")[0].getDecimal());
assertEquals(externalUser.boolValue, a.getProperty("a/booleanSuppliedValue")[0].getBoolean());
}

@Test
public void testIsExpiredLocalGroup() throws Exception {
Group gr = createTestGroup();

0 comments on commit c7d16e8

Please sign in to comment.