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