<commit_before>package com.evolveum.midpoint.model.lens.projector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.common.crypto.EncryptionException;
import com.evolveum.midpoint.common.crypto.Protector;
import com.evolveum.midpoint.common.policy.PasswordPolicyUtils;
import com.evolveum.midpoint.model.api.PolicyViolationException;
import com.evolveum.midpoint.model.lens.LensContext;
import com.evolveum.midpoint.model.lens.LensFocusContext;
import com.evolveum.midpoint.model.lens.LensProjectionContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.AccountShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.PasswordType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ProtectedStringType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.UserType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ValuePolicyType;


@Component
public class PasswordPolicyProcessor {
	
	private static final Trace LOGGER = TraceManager.getTrace(UserPolicyProcessor.class);
	
	@Autowired(required = true)
	Protector protector;

	
	void processPasswordPolicy(ValuePolicyType passwordPolicy, PrismProperty password, OperationResult result)
			throws PolicyViolationException, SchemaException {

		String passwordValue = determinePasswordValue(password);
		if (passwordValue == null || passwordPolicy == null) {
			LOGGER.trace("Skipping processing password policies. Password value or password policies not specified.");
			return;
		}

		boolean isValid = PasswordPolicyUtils.validatePassword(passwordValue, passwordPolicy, result);

		if (!isValid) {
//			result.computeStatus();
			throw new PolicyViolationException("Provided password does not satisfy password policies. " + result.getMessage());

		}

	}
	
	<F extends ObjectType, P extends ObjectType> void processPasswordPolicy(LensFocusContext<UserType> focusContext, LensContext<F,P> context, OperationResult result)
			throws PolicyViolationException, SchemaException {
		
		PrismProperty<PasswordType> password = getPassword(focusContext);
		
		ValuePolicyType passwordPolicy = context.getGlobalPasswordPolicy();
		
		processPasswordPolicy(passwordPolicy, password, result);

	}<commit_msg>fix for: MID-1018 (error when the provided password is empty and the policy is set..)<commit_after>package com.evolveum.midpoint.model.lens.projector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.common.crypto.EncryptionException;
import com.evolveum.midpoint.common.crypto.Protector;
import com.evolveum.midpoint.common.policy.PasswordPolicyUtils;
import com.evolveum.midpoint.model.api.PolicyViolationException;
import com.evolveum.midpoint.model.lens.LensContext;
import com.evolveum.midpoint.model.lens.LensFocusContext;
import com.evolveum.midpoint.model.lens.LensProjectionContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.AccountShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.PasswordType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ProtectedStringType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.UserType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ValuePolicyType;


@Component
public class PasswordPolicyProcessor {
	
	private static final Trace LOGGER = TraceManager.getTrace(UserPolicyProcessor.class);
	
	@Autowired(required = true)
	Protector protector;

	
	void processPasswordPolicy(ValuePolicyType passwordPolicy, PrismProperty password, OperationResult result)
			throws PolicyViolationException, SchemaException {

		String passwordValue = determinePasswordValue(password);
		if (passwordPolicy == null) {
			LOGGER.trace("Skipping processing password policies. Password value or password policies not specified.");
			return;
		}
		
		if (password == null || password.isEmpty()){
			throw new PolicyViolationException("Provided password is empty.");
		}

		boolean isValid = PasswordPolicyUtils.validatePassword(passwordValue, passwordPolicy, result);

		if (!isValid) {
//			String message = result.getMessage() !=null ? result.getMessage() : result.computeStatus();
			throw new PolicyViolationException("Provided password does not satisfy password policies. " + result.getMessage());

		}

	}
	
	<F extends ObjectType, P extends ObjectType> void processPasswordPolicy(LensFocusContext<UserType> focusContext, LensContext<F,P> context, OperationResult result)
			throws PolicyViolationException, SchemaException {
		
		PrismProperty<PasswordType> password = getPassword(focusContext);
		
		ValuePolicyType passwordPolicy = context.getGlobalPasswordPolicy();
		
		processPasswordPolicy(passwordPolicy, password, result);

	}
{'fraction_non_alphanumeric': 0.09525341914722446, 'fraction_numerical': 0.002574416733708769, 'mean_word_length': 6.46218487394958, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896667', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1809, 'n_words': 309}