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