Search in sources :

Example 6 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class ValuePolicyProcessor method validateValue.

public List<StringLimitationResult> validateValue(String newValue, ValuePolicyType pp, ObjectBasedValuePolicyOriginResolver<?> originResolver, List<LocalizableMessage> messages, String shortDesc, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    // TODO: do we want to throw exception when no value policy defined??
    Validate.notNull(pp, "Value policy must not be null.");
    OperationResult result = parentResult.createSubresult(OPERATION_STRING_POLICY_VALIDATION);
    result.addArbitraryObjectAsParam("policyName", pp.getName());
    List<StringLimitationResult> limitations = new ArrayList<>();
    try {
        normalize(pp);
        if (newValue == null) {
            newValue = "";
        }
        LimitationsType lims = pp.getStringPolicy().getLimitations();
        CollectionUtils.addIgnoreNull(limitations, testLength(newValue, lims, result, messages));
        CollectionUtils.addIgnoreNull(limitations, testMinimalUniqueCharacters(newValue, lims, result, messages));
        CollectionUtils.addIgnoreNull(limitations, testProhibitedValues(newValue, pp.getProhibitedValues(), originResolver, shortDesc, task, result, messages));
        // TODO: this needs to be determined from ValuePolicyType archetype
        ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
        limitations.addAll(testCheckExpression(newValue, lims, expressionProfile, originResolver, shortDesc, task, result, messages));
        if (!lims.getLimit().isEmpty()) {
            // check limitation
            HashSet<String> validChars;
            HashSet<String> allValidChars = new HashSet<>();
            List<String> characters = StringPolicyUtils.stringTokenizer(newValue);
            for (StringLimitType stringLimitationType : lims.getLimit()) {
                OperationResult limitResult = new OperationResult("Tested limitation: " + stringLimitationType.getDescription());
                validChars = getValidCharacters(stringLimitationType.getCharacterClass(), pp);
                int count = countValidCharacters(validChars, characters);
                allValidChars.addAll(validChars);
                StringLimitationResult limitation = null;
                limitation = testMinimalOccurrence(stringLimitationType, count, limitResult, messages, limitation);
                limitation = testMaximalOccurrence(stringLimitationType, count, limitResult, messages, limitation);
                limitation = testMustBeFirst(stringLimitationType, limitResult, messages, newValue, validChars, limitation);
                if (limitation != null) {
                    PolyStringType name = stringLimitationType.getName();
                    if (name == null) {
                        name = new PolyStringType(stringLimitationType.getDescription());
                        PolyStringTranslationType translation = new PolyStringTranslationType();
                        translation.setKey(stringLimitationType.getDescription());
                        name.setTranslation(translation);
                    }
                    PolyStringType help = new PolyStringType(getCharsetAsString(validChars));
                    limitation.setHelp(help);
                    limitation.setName(name);
                    limitations.add(limitation);
                }
                limitResult.computeStatus();
                result.addSubresult(limitResult);
            }
            CollectionUtils.addIgnoreNull(limitations, testInvalidCharacters(characters, allValidChars, result, messages));
        }
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
    if (!result.isSuccess() && !messages.isEmpty()) {
        result.setUserFriendlyMessage(new LocalizableMessageListBuilder().messages(messages).separator(LocalizableMessageList.SPACE).buildOptimized());
    }
    return limitations;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) LocalizableMessageListBuilder(com.evolveum.midpoint.util.LocalizableMessageListBuilder) StringLimitType(com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType) PolyStringTranslationType(com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)

Example 7 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class TestExpression method compileExpressionProfile.

protected ExpressionProfile compileExpressionProfile(String profileName) throws SchemaException, IOException {
    if (profileName == null) {
        return null;
    }
    PrismObject<SystemConfigurationType> systemConfig = PrismTestUtil.parseObject(getSystemConfigurationFile());
    SystemConfigurationExpressionsType expressions = systemConfig.asObjectable().getExpressions();
    if (expressions == null) {
        throw new SchemaException("No expressions in system config");
    }
    ExpressionProfileCompiler compiler = new ExpressionProfileCompiler();
    ExpressionProfiles profiles = compiler.compile(expressions);
    ExpressionProfile profile = profiles.getProfile(profileName);
    if (profile == null) {
        throw new SchemaException("Profile '" + profileName + "' not found in system config");
    }
    return profile;
}
Also used : ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ExpressionProfiles(com.evolveum.midpoint.schema.expression.ExpressionProfiles)

Example 8 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class ExpressionProfileCompiler method compileExpressionProfile.

private ExpressionProfile compileExpressionProfile(ExpressionProfileType expressionProfileType, List<ExpressionPermissionProfile> permissionProfiles) throws SchemaException {
    ExpressionProfile profile = new ExpressionProfile(expressionProfileType.getIdentifier());
    profile.setDecision(AccessDecision.translate(expressionProfileType.getDecision()));
    for (ExpressionEvaluatorProfileType evaluatorType : expressionProfileType.getEvaluator()) {
        profile.add(compileEvaluatorProfile(evaluatorType, permissionProfiles));
    }
    return profile;
}
Also used : ExpressionEvaluatorProfileType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionEvaluatorProfileType) ScriptExpressionProfile(com.evolveum.midpoint.schema.expression.ScriptExpressionProfile) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 9 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class SynchronizationExpressionsEvaluator method findFocusesByCorrelationRule.

@NotNull
public <F extends FocusType> List<PrismObject<F>> findFocusesByCorrelationRule(Class<F> focusType, ShadowType currentShadow, List<ConditionalSearchFilterType> conditionalFilters, ResourceType resourceType, SystemConfigurationType configurationType, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    if (conditionalFilters == null || conditionalFilters.isEmpty()) {
        LOGGER.warn("Correlation rule for resource '{}' doesn't contain query, " + "returning empty list of users.", resourceType);
        return emptyList();
    }
    // TODO: determine from the resource
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    List<PrismObject<F>> allUsers = new ArrayList<>();
    for (ConditionalSearchFilterType conditionalFilter : conditionalFilters) {
        // TODO: better description
        if (satisfyCondition(currentShadow, conditionalFilter, expressionProfile, resourceType, configurationType, "Condition expression", task, result)) {
            LOGGER.trace("Condition {} in correlation expression evaluated to true", conditionalFilter.getCondition());
            List<PrismObject<F>> usersFound = findFocusesByCorrelationRule(focusType, currentShadow, conditionalFilter, expressionProfile, resourceType, configurationType, task, result);
            for (PrismObject<F> userFound : usersFound) {
                if (!contains(allUsers, userFound)) {
                    allUsers.add(userFound);
                }
            }
        }
    }
    LOGGER.debug("SYNCHRONIZATION: CORRELATION: expression for {} returned {} users: {}", currentShadow, allUsers.size(), PrettyPrinter.prettyPrint(allUsers, 3));
    return allUsers;
}
Also used : ArrayList(java.util.ArrayList) ConditionalSearchFilterType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConditionalSearchFilterType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class ScriptingExpressionEvaluator method evaluateExpression.

private ExecutionContext evaluateExpression(@NotNull ExecuteScriptType executeScript, VariablesMap initialVariables, boolean privileged, boolean recordProgressAndIterationStatistics, Task task, OperationResult result) throws ScriptExecutionException {
    Validate.notNull(executeScript.getScriptingExpression(), "Scripting expression must be present");
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    try {
        VariablesMap frozenVariables = VariablesUtil.initialPreparation(initialVariables, executeScript.getVariables(), expressionFactory, modelObjectResolver, prismContext, expressionProfile, task, result);
        PipelineData pipelineData = PipelineData.parseFrom(executeScript.getInput(), frozenVariables, prismContext);
        ExecutionContext context = new ExecutionContext(executeScript.getOptions(), task, this, privileged, recordProgressAndIterationStatistics, frozenVariables);
        PipelineData output = evaluateExpression(executeScript.getScriptingExpression().getValue(), pipelineData, context, result);
        context.setFinalOutput(output);
        result.computeStatusIfUnknown();
        context.computeResults();
        return context;
    } catch (ExpressionEvaluationException | SchemaException | ObjectNotFoundException | RuntimeException | CommunicationException | ConfigurationException | SecurityViolationException e) {
        result.recordFatalError("Couldn't execute script", e);
        throw new ScriptExecutionException("Couldn't execute script: " + e.getMessage(), e);
    } catch (Throwable t) {
        result.recordFatalError("Couldn't execute script", t);
        throw t;
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)16 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)7 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)3 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)3 ScriptExpressionProfile (com.evolveum.midpoint.schema.expression.ScriptExpressionProfile)3 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 Trace (com.evolveum.midpoint.util.logging.Trace)3 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)3 NotNull (org.jetbrains.annotations.NotNull)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)2 ExpressionUtil (com.evolveum.midpoint.repo.common.expression.ExpressionUtil)2 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)2 ExpressionEvaluatorProfile (com.evolveum.midpoint.schema.expression.ExpressionEvaluatorProfile)2 MiscSchemaUtil (com.evolveum.midpoint.schema.util.MiscSchemaUtil)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)2 Collection (java.util.Collection)2 List (java.util.List)2