use of io.gravitee.am.policy.enrich.profile.configuration.Property in project gravitee-access-management by gravitee-io.
the class EnrichProfilePolicy method prepareUserProfile.
/**
* Complete the user profile based on the Policy configuration
*
* @param context
* @return true if the profile must be updated, false otherwise
*/
protected boolean prepareUserProfile(ExecutionContext context) {
boolean needUpdate = false;
if (!(configuration.getProperties() == null || configuration.getProperties().isEmpty())) {
User user = (User) context.getAttribute("user");
if (user != null) {
LOGGER.debug("Enrich profile for user '{}'", user.getId());
Map<String, Object> additionalInformation = user.getAdditionalInformation();
if (additionalInformation == null) {
additionalInformation = new HashMap<>();
user.setAdditionalInformation(additionalInformation);
}
TemplateEngine tplEngine = context.getTemplateEngine();
for (Property property : configuration.getProperties()) {
String additionalInfo = tplEngine.getValue(property.getClaimValue(), String.class);
additionalInformation.put(property.getClaim(), additionalInfo);
}
needUpdate = true;
} else {
LOGGER.debug("User is missing from the execution context, ignore this policy");
}
} else {
LOGGER.debug("No properties found in policy configuration, ignore this policy");
}
return needUpdate;
}
use of io.gravitee.am.policy.enrich.profile.configuration.Property in project gravitee-access-management by gravitee-io.
the class EnrichProfilePolicyTest method shouldUpdateUser.
@Test
public void shouldUpdateUser() throws Exception {
final CountDownLatch lock = new CountDownLatch(1);
this.policyChain = spy(new CountDownPolicyChain(lock));
when(configuration.getProperties()).thenReturn(Arrays.asList(new Property("myclaim", "myclaimValue"), new Property("myclaim-tpl", "{#request.params['" + REQUEST_PARAM + "']}")));
User user = mock(User.class);
Map<String, Object> additionalInformation = new HashMap<>();
when(user.getAdditionalInformation()).thenReturn(additionalInformation);
when(executionContext.getAttribute("user")).thenReturn(user);
when(userRepository.update(any())).thenReturn(Single.just(user));
EnrichProfilePolicy enrichProfilePolicy = new EnrichProfilePolicy(configuration);
enrichProfilePolicy.onRequest(request, response, executionContext, policyChain);
lock.await(1, TimeUnit.SECONDS);
verify(policyChain, never()).failWith(any());
verify(policyChain).doNext(any(), any());
verify(userRepository).update(argThat(u -> u.getAdditionalInformation() != null && u.getAdditionalInformation().containsKey("myclaim") && "myclaimValue".equals(u.getAdditionalInformation().get("myclaim")) && u.getAdditionalInformation().containsKey("myclaim-tpl") && PARAM_VALUE.equals(u.getAdditionalInformation().get("myclaim-tpl"))));
}
Aggregations