Search in sources :

Example 1 with LimitationsType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType in project midpoint by Evolveum.

the class ValuePolicyProcessor method validateValue.

public <O extends ObjectType> boolean validateValue(String newValue, ValuePolicyType pp, PrismObject<O> object, StringBuilder message, String shortDesc, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    Validate.notNull(pp, "Value policy must not be null.");
    OperationResult result = parentResult.createSubresult(OPERATION_STRING_POLICY_VALIDATION);
    result.addParam("policyName", pp.getName());
    normalize(pp);
    if (newValue == null && (pp.getMinOccurs() == null || XsdTypeMapper.multiplicityToInteger(pp.getMinOccurs()) == 0)) {
        // No password is allowed
        result.recordSuccess();
        return true;
    }
    if (newValue == null) {
        newValue = "";
    }
    LimitationsType lims = pp.getStringPolicy().getLimitations();
    testMinimalLength(newValue, lims, result, message);
    testMaximalLength(newValue, lims, result, message);
    testMinimalUniqueCharacters(newValue, lims, result, message);
    if (lims.getLimit() == null || lims.getLimit().isEmpty()) {
        if (message.toString() == null || message.toString().isEmpty()) {
            result.computeStatus();
        } else {
            result.computeStatus(message.toString());
        }
        return result.isAcceptable();
    }
    // check limitation
    HashSet<String> validChars = null;
    HashSet<String> allValidChars = new HashSet<>();
    List<String> passwd = 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, passwd);
        allValidChars.addAll(validChars);
        testMinimalOccurence(stringLimitationType, count, limitResult, message);
        testMaximalOccurence(stringLimitationType, count, limitResult, message);
        testMustBeFirst(stringLimitationType, count, limitResult, message, newValue, validChars);
        limitResult.computeStatus();
        result.addSubresult(limitResult);
    }
    testInvalidCharacters(passwd, allValidChars, result, message);
    testCheckExpression(newValue, lims, object, shortDesc, task, result, message);
    if (message.toString() == null || message.toString().isEmpty()) {
        result.computeStatus();
    } else {
        result.computeStatus(message.toString());
    }
    return result.isAcceptable();
}
Also used : StringLimitType(com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType) HashSet(java.util.HashSet)

Example 2 with LimitationsType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType in project midpoint by Evolveum.

the class StringPolicyUtils method normalize.

public static StringPolicyType normalize(StringPolicyType sp) {
    if (null == sp) {
        throw new IllegalArgumentException("Providide string policy cannot be null");
    }
    if (null == sp.getLimitations()) {
        LimitationsType sl = new LimitationsType();
        sl.setCheckAgainstDictionary(false);
        sl.setCheckPattern("");
        sl.setMaxLength(Integer.MAX_VALUE);
        sl.setMinLength(0);
        sl.setMinUniqueChars(0);
        sp.setLimitations(sl);
    }
    // Add default char class
    if (null == sp.getCharacterClass()) {
        CharacterClassType cct = new CharacterClassType();
        cct.setValue(ASCII7_CHARS);
        sp.setCharacterClass(cct);
    }
    return sp;
}
Also used : CharacterClassType(com.evolveum.midpoint.xml.ns._public.common.common_3.CharacterClassType) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType)

Example 3 with LimitationsType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType 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 4 with LimitationsType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType in project midpoint by Evolveum.

the class ValuePolicyProcessor method testCheckExpression.

private <O extends ObjectType> void testCheckExpression(String newPassword, LimitationsType lims, PrismObject<O> object, String shortDesc, Task task, OperationResult result, StringBuilder message) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    List<CheckExpressionType> checkExpressions = lims.getCheckExpression();
    if (checkExpressions.isEmpty()) {
        return;
    }
    for (CheckExpressionType checkExpression : checkExpressions) {
        ExpressionType expressionType = checkExpression.getExpression();
        if (expressionType == null) {
            return;
        }
        if (!checkExpression(newPassword, expressionType, object, shortDesc, task, result)) {
            String msg = checkExpression.getFailureMessage();
            if (msg == null) {
                msg = "Check expression failed";
            }
            result.addSubresult(new OperationResult("Check expression", OperationResultStatus.FATAL_ERROR, msg));
            message.append(msg);
            message.append("\n");
        }
    }
}
Also used : CheckExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) CheckExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 5 with LimitationsType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType in project midpoint by Evolveum.

the class ValuePolicyProcessor method checkAttempt.

private <O extends ObjectType> boolean checkAttempt(String generatedValue, StringPolicyType policy, PrismObject<O> object, String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    LimitationsType limitationsType = policy.getLimitations();
    if (limitationsType == null) {
        return true;
    }
    List<CheckExpressionType> checkExpressionTypes = limitationsType.getCheckExpression();
    if (!checkExpressions(generatedValue, checkExpressionTypes, object, shortDesc, task, result)) {
        LOGGER.trace("Check expression returned false for generated value in {}", shortDesc);
        return false;
    }
    // TODO Check pattern
    return true;
}
Also used : CheckExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)5 LimitationsType (com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType)5 CheckExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType)3 StringLimitationResult (com.evolveum.midpoint.model.api.validator.StringLimitationResult)2 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)2 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)2 PropertyLimitationsType (com.evolveum.midpoint.xml.ns._public.common.common_3.PropertyLimitationsType)2 StringLimitType (com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType)2 PolyStringTranslationType (com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)2 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 LocalizableMessage (com.evolveum.midpoint.util.LocalizableMessage)1 LocalizableMessageListBuilder (com.evolveum.midpoint.util.LocalizableMessageListBuilder)1 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 CharacterClassType (com.evolveum.midpoint.xml.ns._public.common.common_3.CharacterClassType)1 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)1 PropertyAccessType (com.evolveum.midpoint.xml.ns._public.common.common_3.PropertyAccessType)1 StringPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.StringPolicyType)1 ValuePolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType)1