Search in sources :

Example 1 with Property

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;
}
Also used : TemplateEngine(io.gravitee.el.TemplateEngine) User(io.gravitee.am.model.User) Property(io.gravitee.am.policy.enrich.profile.configuration.Property)

Example 2 with Property

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"))));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) UserRepository(io.gravitee.am.repository.management.api.UserRepository) Mock(org.mockito.Mock) MultiValueMap(io.gravitee.common.util.MultiValueMap) RunWith(org.junit.runner.RunWith) RequestWrapper(io.gravitee.gateway.api.RequestWrapper) HashMap(java.util.HashMap) Single(io.reactivex.Single) EnrichProfilePolicyConfiguration(io.gravitee.am.policy.enrich.profile.configuration.EnrichProfilePolicyConfiguration) TemplateEngine(io.gravitee.el.TemplateEngine) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Map(java.util.Map) User(io.gravitee.am.model.User) EvaluableRequest(io.gravitee.am.gateway.handler.context.EvaluableRequest) Before(org.junit.Before) SpelTemplateEngineFactory(io.gravitee.el.spel.SpelTemplateEngineFactory) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Test(org.junit.Test) LinkedMultiValueMap(io.gravitee.common.util.LinkedMultiValueMap) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) PolicyChain(io.gravitee.policy.api.PolicyChain) PolicyResult(io.gravitee.policy.api.PolicyResult) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Property(io.gravitee.am.policy.enrich.profile.configuration.Property) User(io.gravitee.am.model.User) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Property(io.gravitee.am.policy.enrich.profile.configuration.Property) Test(org.junit.Test)

Aggregations

User (io.gravitee.am.model.User)2 Property (io.gravitee.am.policy.enrich.profile.configuration.Property)2 TemplateEngine (io.gravitee.el.TemplateEngine)2 EvaluableRequest (io.gravitee.am.gateway.handler.context.EvaluableRequest)1 EnrichProfilePolicyConfiguration (io.gravitee.am.policy.enrich.profile.configuration.EnrichProfilePolicyConfiguration)1 UserRepository (io.gravitee.am.repository.management.api.UserRepository)1 LinkedMultiValueMap (io.gravitee.common.util.LinkedMultiValueMap)1 MultiValueMap (io.gravitee.common.util.MultiValueMap)1 SpelTemplateEngineFactory (io.gravitee.el.spel.SpelTemplateEngineFactory)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Request (io.gravitee.gateway.api.Request)1 RequestWrapper (io.gravitee.gateway.api.RequestWrapper)1 Response (io.gravitee.gateway.api.Response)1 PolicyChain (io.gravitee.policy.api.PolicyChain)1 PolicyResult (io.gravitee.policy.api.PolicyResult)1 Single (io.reactivex.Single)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1