Search in sources :

Example 1 with CorrelationRule

use of org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule in project syncope by apache.

the class PolicyDataBinderImpl method getPolicy.

@SuppressWarnings("unchecked")
private <T extends Policy> T getPolicy(final T policy, final PolicyTO policyTO) {
    T result = policy;
    if (policyTO instanceof PasswordPolicyTO) {
        if (result == null) {
            result = (T) entityFactory.newEntity(PasswordPolicy.class);
        }
        PasswordPolicy passwordPolicy = PasswordPolicy.class.cast(result);
        PasswordPolicyTO passwordPolicyTO = PasswordPolicyTO.class.cast(policyTO);
        passwordPolicy.setAllowNullPassword(passwordPolicyTO.isAllowNullPassword());
        passwordPolicy.setHistoryLength(passwordPolicyTO.getHistoryLength());
        passwordPolicyTO.getRules().forEach(ruleKey -> {
            Implementation rule = implementationDAO.find(ruleKey);
            if (rule == null) {
                LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", ruleKey);
            } else {
                passwordPolicy.add(rule);
            }
        });
        // remove all implementations not contained in the TO
        passwordPolicy.getRules().removeIf(implementation -> !passwordPolicyTO.getRules().contains(implementation.getKey()));
    } else if (policyTO instanceof AccountPolicyTO) {
        if (result == null) {
            result = (T) entityFactory.newEntity(AccountPolicy.class);
        }
        AccountPolicy accountPolicy = AccountPolicy.class.cast(result);
        AccountPolicyTO accountPolicyTO = AccountPolicyTO.class.cast(policyTO);
        accountPolicy.setMaxAuthenticationAttempts(accountPolicyTO.getMaxAuthenticationAttempts());
        accountPolicy.setPropagateSuspension(accountPolicyTO.isPropagateSuspension());
        accountPolicyTO.getRules().forEach(ruleKey -> {
            Implementation rule = implementationDAO.find(ruleKey);
            if (rule == null) {
                LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", ruleKey);
            } else {
                accountPolicy.add(rule);
            }
        });
        // remove all implementations not contained in the TO
        accountPolicy.getRules().removeIf(implementation -> !accountPolicyTO.getRules().contains(implementation.getKey()));
        accountPolicy.getResources().clear();
        accountPolicyTO.getPassthroughResources().forEach(resourceName -> {
            ExternalResource resource = resourceDAO.find(resourceName);
            if (resource == null) {
                LOG.debug("Ignoring invalid resource {} ", resourceName);
            } else {
                accountPolicy.add(resource);
            }
        });
    } else if (policyTO instanceof PullPolicyTO) {
        if (result == null) {
            result = (T) entityFactory.newEntity(PullPolicy.class);
        }
        PullPolicy pullPolicy = PullPolicy.class.cast(result);
        PullPolicyTO pullPolicyTO = PullPolicyTO.class.cast(policyTO);
        pullPolicy.setConflictResolutionAction(pullPolicyTO.getConflictResolutionAction());
        pullPolicyTO.getCorrelationRules().forEach((type, impl) -> {
            AnyType anyType = anyTypeDAO.find(type);
            if (anyType == null) {
                LOG.debug("Invalid AnyType {} specified, ignoring...", type);
            } else {
                CorrelationRule correlationRule = pullPolicy.getCorrelationRule(anyType).orElse(null);
                if (correlationRule == null) {
                    correlationRule = entityFactory.newEntity(CorrelationRule.class);
                    correlationRule.setAnyType(anyType);
                    correlationRule.setPullPolicy(pullPolicy);
                    pullPolicy.add(correlationRule);
                }
                Implementation rule = implementationDAO.find(impl);
                if (rule == null) {
                    throw new NotFoundException("Implementation " + type);
                }
                correlationRule.setImplementation(rule);
            }
        });
        // remove all rules not contained in the TO
        pullPolicy.getCorrelationRules().removeIf(anyFilter -> !pullPolicyTO.getCorrelationRules().containsKey(anyFilter.getAnyType().getKey()));
    }
    if (result != null) {
        result.setDescription(policyTO.getDescription());
    }
    return result;
}
Also used : PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) Realm(org.apache.syncope.core.persistence.api.entity.Realm) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) Entity(org.apache.syncope.core.persistence.api.entity.Entity) PolicyDataBinder(org.apache.syncope.core.provisioning.api.data.PolicyDataBinder) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) PolicyTO(org.apache.syncope.common.lib.policy.PolicyTO) PasswordPolicyTO(org.apache.syncope.common.lib.policy.PasswordPolicyTO) Logger(org.slf4j.Logger) Policy(org.apache.syncope.core.persistence.api.entity.policy.Policy) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) Component(org.springframework.stereotype.Component) CorrelationRule(org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule) AccountPolicyTO(org.apache.syncope.common.lib.policy.AccountPolicyTO) PullPolicyTO(org.apache.syncope.common.lib.policy.PullPolicyTO) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) PullPolicyTO(org.apache.syncope.common.lib.policy.PullPolicyTO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) AccountPolicyTO(org.apache.syncope.common.lib.policy.AccountPolicyTO) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) CorrelationRule(org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) PasswordPolicyTO(org.apache.syncope.common.lib.policy.PasswordPolicyTO)

Example 2 with CorrelationRule

use of org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule in project syncope by apache.

the class PolicyTest method findByKey.

@Test
public void findByKey() {
    PullPolicy policy = policyDAO.find("880f8553-069b-4aed-9930-2cd53873f544");
    assertNotNull(policy);
    CorrelationRule rule = policy.getCorrelationRule(anyTypeDAO.findUser()).orElse(null);
    assertNotNull(rule);
    DefaultPullCorrelationRuleConf ruleConf = POJOHelper.deserialize(rule.getImplementation().getBody(), DefaultPullCorrelationRuleConf.class);
    assertNotNull(ruleConf);
    assertEquals(2, ruleConf.getSchemas().size());
    assertTrue(ruleConf.getSchemas().contains("username"));
    assertTrue(ruleConf.getSchemas().contains("firstname"));
}
Also used : PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) PullCorrelationRule(org.apache.syncope.core.persistence.api.dao.PullCorrelationRule) CorrelationRule(org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule) DefaultPullCorrelationRuleConf(org.apache.syncope.common.lib.policy.DefaultPullCorrelationRuleConf) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 3 with CorrelationRule

use of org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule in project syncope by apache.

the class PolicyTest method create.

@Test
public void create() {
    PullPolicy policy = entityFactory.newEntity(PullPolicy.class);
    policy.setConflictResolutionAction(ConflictResolutionAction.IGNORE);
    policy.setDescription("Pull policy");
    final String pullURuleName = "net.tirasa.pull.correlation.TirasaURule";
    final String pullGRuleName = "net.tirasa.pull.correlation.TirasaGRule";
    Implementation impl1 = entityFactory.newEntity(Implementation.class);
    impl1.setKey(pullURuleName);
    impl1.setEngine(ImplementationEngine.JAVA);
    impl1.setType(ImplementationType.PULL_CORRELATION_RULE);
    impl1.setBody(PullCorrelationRule.class.getName());
    impl1 = implementationDAO.save(impl1);
    CorrelationRule rule1 = entityFactory.newEntity(CorrelationRule.class);
    rule1.setAnyType(anyTypeDAO.findUser());
    rule1.setPullPolicy(policy);
    rule1.setImplementation(impl1);
    policy.add(rule1);
    Implementation impl2 = entityFactory.newEntity(Implementation.class);
    impl2.setKey(pullGRuleName);
    impl2.setEngine(ImplementationEngine.JAVA);
    impl2.setType(ImplementationType.PULL_CORRELATION_RULE);
    impl2.setBody(PullCorrelationRule.class.getName());
    impl2 = implementationDAO.save(impl2);
    CorrelationRule rule2 = entityFactory.newEntity(CorrelationRule.class);
    rule2.setAnyType(anyTypeDAO.findGroup());
    rule2.setPullPolicy(policy);
    rule2.setImplementation(impl2);
    policy.add(rule2);
    policy = policyDAO.save(policy);
    assertNotNull(policy);
    assertEquals(pullURuleName, policy.getCorrelationRule(anyTypeDAO.findUser()).get().getImplementation().getKey());
    assertEquals(pullGRuleName, policy.getCorrelationRule(anyTypeDAO.findGroup()).get().getImplementation().getKey());
}
Also used : PullCorrelationRule(org.apache.syncope.core.persistence.api.dao.PullCorrelationRule) PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) PullCorrelationRule(org.apache.syncope.core.persistence.api.dao.PullCorrelationRule) CorrelationRule(org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

CorrelationRule (org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule)3 PullPolicy (org.apache.syncope.core.persistence.api.entity.policy.PullPolicy)3 PullCorrelationRule (org.apache.syncope.core.persistence.api.dao.PullCorrelationRule)2 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)2 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)2 Test (org.junit.jupiter.api.Test)2 Collectors (java.util.stream.Collectors)1 AccountPolicyTO (org.apache.syncope.common.lib.policy.AccountPolicyTO)1 DefaultPullCorrelationRuleConf (org.apache.syncope.common.lib.policy.DefaultPullCorrelationRuleConf)1 PasswordPolicyTO (org.apache.syncope.common.lib.policy.PasswordPolicyTO)1 PolicyTO (org.apache.syncope.common.lib.policy.PolicyTO)1 PullPolicyTO (org.apache.syncope.common.lib.policy.PullPolicyTO)1 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)1 ExternalResourceDAO (org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO)1 ImplementationDAO (org.apache.syncope.core.persistence.api.dao.ImplementationDAO)1 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)1 RealmDAO (org.apache.syncope.core.persistence.api.dao.RealmDAO)1 AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)1 Entity (org.apache.syncope.core.persistence.api.entity.Entity)1 EntityFactory (org.apache.syncope.core.persistence.api.entity.EntityFactory)1