Search in sources :

Example 1 with PolicyTO

use of org.apache.syncope.common.lib.policy.PolicyTO in project syncope by apache.

the class PolicyServiceImpl method create.

@Override
public Response create(final PolicyType type, final PolicyTO policyTO) {
    PolicyTO policy = logic.create(type, policyTO);
    URI location = uriInfo.getAbsolutePathBuilder().path(policy.getKey()).build();
    return Response.created(location).header(RESTHeaders.RESOURCE_KEY, policy.getKey()).build();
}
Also used : URI(java.net.URI) PolicyTO(org.apache.syncope.common.lib.policy.PolicyTO)

Example 2 with PolicyTO

use of org.apache.syncope.common.lib.policy.PolicyTO 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 3 with PolicyTO

use of org.apache.syncope.common.lib.policy.PolicyTO in project syncope by apache.

the class PolicyRuleWizardBuilder method onApplyInternal.

@Override
protected Serializable onApplyInternal(final PolicyRuleWrapper modelObject) {
    PolicyTO policyTO = restClient.getPolicy(type, policy);
    ComposablePolicy composable;
    if (policyTO instanceof ComposablePolicy) {
        composable = (ComposablePolicy) policyTO;
    } else {
        throw new IllegalStateException("Non composable policy");
    }
    if (modelObject.getImplementationEngine() == ImplementationEngine.JAVA) {
        ImplementationTO rule = implementationClient.read(implementationType, modelObject.getImplementationKey());
        try {
            rule.setBody(MAPPER.writeValueAsString(modelObject.getConf()));
            implementationClient.update(rule);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    if (modelObject.isNew()) {
        composable.getRules().add(modelObject.getImplementationKey());
    }
    restClient.updatePolicy(type, policyTO);
    return modelObject;
}
Also used : ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) ComposablePolicy(org.apache.syncope.common.lib.policy.ComposablePolicy) PolicyTO(org.apache.syncope.common.lib.policy.PolicyTO)

Aggregations

PolicyTO (org.apache.syncope.common.lib.policy.PolicyTO)3 URI (java.net.URI)1 Collectors (java.util.stream.Collectors)1 AccountPolicyTO (org.apache.syncope.common.lib.policy.AccountPolicyTO)1 ComposablePolicy (org.apache.syncope.common.lib.policy.ComposablePolicy)1 PasswordPolicyTO (org.apache.syncope.common.lib.policy.PasswordPolicyTO)1 PullPolicyTO (org.apache.syncope.common.lib.policy.PullPolicyTO)1 ImplementationTO (org.apache.syncope.common.lib.to.ImplementationTO)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 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)1 Realm (org.apache.syncope.core.persistence.api.entity.Realm)1 AccountPolicy (org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy)1 CorrelationRule (org.apache.syncope.core.persistence.api.entity.policy.CorrelationRule)1