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;
}
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"));
}
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());
}
Aggregations