Search in sources :

Example 16 with VariablesMap

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

the class VariablesUtil method createVariables.

private static VariablesMap createVariables(VariablesMap variableMap) {
    VariablesMap rv = new VariablesMap();
    VariablesMap clonedVariableMap = cloneIfNecessary(variableMap);
    clonedVariableMap.forEach((name, value) -> rv.put(name, value));
    return rv;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 17 with VariablesMap

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

the class VariablesUtil method initialPreparation.

// We create immutable versions of prism variables to avoid unnecessary downstream cloning
@NotNull
static VariablesMap initialPreparation(VariablesMap initialVariables, ScriptingVariablesDefinitionType derivedVariables, ExpressionFactory expressionFactory, ObjectResolver objectResolver, PrismContext prismContext, ExpressionProfile expressionProfile, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    VariablesMap rv = new VariablesMap();
    addProvidedVariables(rv, initialVariables, task);
    addDerivedVariables(rv, derivedVariables, new VariableResolutionContext(expressionFactory, objectResolver, prismContext, expressionProfile, task), result);
    return rv;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with VariablesMap

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

the class VariablesUtil method cloneIfNecessary.

@NotNull
public static VariablesMap cloneIfNecessary(@NotNull VariablesMap variables) {
    VariablesMap rv = new VariablesMap();
    variables.forEach((key, value) -> rv.put(key, cloneIfNecessary(key, value)));
    return rv;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with VariablesMap

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

the class ConstraintEvaluatorHelper method createVariablesMap.

// corresponds with PolicyRuleBasedAspect.processNameFromApprovalActions
public <AH extends AssignmentHolderType> VariablesMap createVariablesMap(PolicyRuleEvaluationContext<AH> rctx, JAXBElement<? extends AbstractPolicyConstraintType> constraintElement) {
    VariablesMap var = new VariablesMap();
    PrismObject<AH> object = rctx.getObject();
    var.put(ExpressionConstants.VAR_USER, object, object.getDefinition());
    var.put(ExpressionConstants.VAR_FOCUS, object, object.getDefinition());
    var.put(ExpressionConstants.VAR_OBJECT, object, object.getDefinition());
    var.put(ExpressionConstants.VAR_OBJECT_DISPLAY_INFORMATION, LocalizationUtil.createLocalizableMessageType(createDisplayInformation(object, false)), LocalizableMessageType.class);
    if (rctx instanceof AssignmentPolicyRuleEvaluationContext) {
        AssignmentPolicyRuleEvaluationContext actx = (AssignmentPolicyRuleEvaluationContext<AH>) rctx;
        PrismObject target = actx.evaluatedAssignment.getTarget();
        var.put(ExpressionConstants.VAR_TARGET, target, target.getDefinition());
        var.put(ExpressionConstants.VAR_TARGET_DISPLAY_INFORMATION, LocalizationUtil.createLocalizableMessageType(createDisplayInformation(target, false)), LocalizableMessageType.class);
        var.put(ExpressionConstants.VAR_EVALUATED_ASSIGNMENT, actx.evaluatedAssignment, EvaluatedAssignment.class);
        AssignmentType assignment = actx.evaluatedAssignment.getAssignment(actx.state == ObjectState.BEFORE);
        var.put(ExpressionConstants.VAR_ASSIGNMENT, assignment, AssignmentType.class);
    } else {
        SchemaRegistry schemaRegistry = PrismContext.get().getSchemaRegistry();
        PrismObjectDefinition<ObjectType> targetDef = schemaRegistry.findObjectDefinitionByCompileTimeClass(ObjectType.class);
        var.put(ExpressionConstants.VAR_TARGET, null, targetDef);
        var.put(ExpressionConstants.VAR_TARGET_DISPLAY_INFORMATION, null, LocalizableMessageType.class);
        var.put(ExpressionConstants.VAR_EVALUATED_ASSIGNMENT, null, EvaluatedAssignment.class);
        PrismContainerDefinition<AssignmentType> assignmentDef = schemaRegistry.findObjectDefinitionByCompileTimeClass(AssignmentHolderType.class).findContainerDefinition(AssignmentHolderType.F_ASSIGNMENT);
        var.put(ExpressionConstants.VAR_ASSIGNMENT, null, assignmentDef);
    }
    var.put(VAR_RULE_EVALUATION_CONTEXT, rctx, PolicyRuleEvaluationContext.class);
    var.put(VAR_EVALUATOR_HELPER, this, ConstraintEvaluatorHelper.class);
    var.put(VAR_CONSTRAINT, constraintElement != null ? constraintElement.getValue() : null, AbstractPolicyConstraintType.class);
    var.put(VAR_CONSTRAINT_ELEMENT, constraintElement, JAXBElement.class);
    return var;
}
Also used : AssignmentPolicyRuleEvaluationContext(com.evolveum.midpoint.model.impl.lens.projector.policy.AssignmentPolicyRuleEvaluationContext) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) SchemaRegistry(com.evolveum.midpoint.prism.schema.SchemaRegistry)

Example 20 with VariablesMap

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

the class StateConstraintEvaluator method evaluateForObject.

private <AH extends AssignmentHolderType> EvaluatedStateTrigger evaluateForObject(JAXBElement<StatePolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    StatePolicyConstraintType constraint = constraintElement.getValue();
    int count = (constraint.getFilter() != null ? 1 : 0) + (constraint.getExpression() != null ? 1 : 0) + (constraint.getMessageExpression() != null ? 1 : 0) + (constraint.getExecuteScript() != null ? 1 : 0);
    if (count != 1) {
        throw new SchemaException("Exactly one of filter, expression, messageExpression, executeScript element must be present.");
    }
    PrismObject<AH> object = ctx.getObject();
    if (object == null) {
        return null;
    }
    if (constraint.getFilter() != null) {
        ObjectFilter filter = prismContext.getQueryConverter().parseFilter(constraint.getFilter(), object.asObjectable().getClass());
        if (!filter.match(object.getValue(), matchingRuleRegistry)) {
            return null;
        }
    }
    if (constraint.getExecuteScript() != null) {
        VariablesMap variables = new VariablesMap();
        variables.put(VAR_OBJECT, object, object.getDefinition());
        variables.put(VAR_RULE_EVALUATION_CONTEXT, ctx, PolicyRuleEvaluationContext.class);
        ExecutionContext resultingContext;
        try {
            resultingContext = scriptingExpressionEvaluator.evaluateExpressionPrivileged(constraint.getExecuteScript(), variables, ctx.task, result);
        } catch (ScriptExecutionException e) {
            // TODO
            throw new SystemException(e);
        }
        PipelineData output = resultingContext.getFinalOutput();
        LOGGER.trace("Scripting expression returned {} item(s); console output is:\n{}", output != null ? output.getData().size() : null, resultingContext.getConsoleOutput());
        List<PipelineItem> items = output != null ? output.getData() : emptyList();
        if (items.isEmpty()) {
            return null;
        }
    // TODO retrieve localization messages from output
    }
    if (constraint.getMessageExpression() != null) {
        LocalizableMessageType messageBean = evaluatorHelper.evaluateLocalizableMessageType(constraint.getMessageExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "message expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result);
        if (messageBean == null) {
            return null;
        } else {
            LocalizableMessage message = LocalizationUtil.toLocalizableMessage(messageBean);
            return new EvaluatedStateTrigger(OBJECT_STATE, constraint, message, message);
        }
    }
    if (constraint.getExpression() != null) {
        if (!evaluatorHelper.evaluateBoolean(constraint.getExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result)) {
            return null;
        }
    }
    return new EvaluatedStateTrigger(OBJECT_STATE, constraint, createMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result), createShortMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result));
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) LocalizableMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.LocalizableMessageType) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) StatePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.StatePolicyConstraintType) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) EvaluatedStateTrigger(com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7