Search in sources :

Example 6 with StringLimitationResult

use of com.evolveum.midpoint.model.api.validator.StringLimitationResult 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 7 with StringLimitationResult

use of com.evolveum.midpoint.model.api.validator.StringLimitationResult in project midpoint by Evolveum.

the class PasswordPolicyValidationPanel method initLayout.

private void initLayout() {
    IModel<DisplayType> displayModel = (IModel) () -> {
        if (!Boolean.TRUE.equals(isAfterInitialization.getObject())) {
            return null;
        }
        String status = "fa-fw " + GuiStyleConstants.CLASS_OP_RESULT_STATUS_ICON_SUCCESS_COLORED + " fa-lg";
        String titleKey = "PasswordPolicyValidationPanel.valid";
        for (StringLimitationResult limitation : getModelObject()) {
            if (!limitation.isSuccess()) {
                status = "fa-fw " + GuiStyleConstants.CLASS_OP_RESULT_STATUS_ICON_FATAL_ERROR_COLORED + " fa-lg";
                titleKey = "PasswordPolicyValidationPanel.invalid";
            }
        }
        return GuiDisplayTypeUtil.createDisplayType(status, "", getPageBase().createStringResource(titleKey).getString());
    };
    ImagePanel resultIcon = new ImagePanel(ID_RESULT_ICON, displayModel);
    resultIcon.setOutputMarkupId(true);
    add(resultIcon);
    ImagePanel infoPanel = new ImagePanel(ID_INFO_ICON, Model.of(GuiDisplayTypeUtil.createDisplayType("fa fa-info-circle")));
    add(infoPanel);
    PasswordLimitationsPanel validationPanel = new PasswordLimitationsPanel(ID_POLICY_VALIDATION_POPOVER, getModel());
    validationPanel.setOutputMarkupId(true);
    add(validationPanel);
}
Also used : IModel(org.apache.wicket.model.IModel) DisplayType(com.evolveum.midpoint.xml.ns._public.common.common_3.DisplayType) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) PasswordLimitationsPanel(com.evolveum.midpoint.gui.api.component.password.PasswordLimitationsPanel)

Example 8 with StringLimitationResult

use of com.evolveum.midpoint.model.api.validator.StringLimitationResult in project midpoint by Evolveum.

the class PasswordPanel method initLayout.

private <F extends FocusType> void initLayout(final boolean isReadOnly, PrismObject<F> object) {
    setOutputMarkupId(true);
    final WebMarkupContainer inputContainer = new WebMarkupContainer(ID_INPUT_CONTAINER) {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return passwordInputVisible;
        }
    };
    inputContainer.setOutputMarkupId(true);
    add(inputContainer);
    ValuePolicyType valuePolicy = getValuePolicy(object);
    LoadableModel<List<StringLimitationResult>> limitationsModel = new LoadableModel<>() {

        @Override
        protected List<StringLimitationResult> load() {
            return getLimitationsForActualPassword(valuePolicy, object);
        }
    };
    final PasswordLimitationsPanel validationPanel = new PasswordLimitationsPanel(ID_VALIDATION_PANEL, limitationsModel);
    validationPanel.setOutputMarkupId(true);
    inputContainer.add(validationPanel);
    final PasswordTextField password1 = new SecureModelPasswordTextField(ID_PASSWORD_ONE, new PasswordModel(model)) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            if (clearPasswordInput) {
                tag.remove("value");
            }
        }
    };
    password1.add(AttributeAppender.append("onfocus", "initPasswordValidation({\n" + "container: $('#progress-bar-container'),\n" + "hierarchy: {\n" + "    '0': ['progress-bar-danger', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.veryWeak").getString() + "'],\n" + "    '25': ['progress-bar-danger', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.weak").getString() + "'],\n" + "    '50': ['progress-bar-warning', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.good").getString() + "'],\n" + "    '75': ['progress-bar-success', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.strong").getString() + "'],\n" + "    '100': ['progress-bar-success', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.veryStrong").getString() + "']\n" + "}\n" + "})"));
    password1.setRequired(false);
    password1.setOutputMarkupId(true);
    password1.add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
    inputContainer.add(password1);
    final PasswordTextField password2 = new SecureModelPasswordTextField(ID_PASSWORD_TWO, new PasswordModel(Model.of(new ProtectedStringType())));
    password2.setRequired(false);
    password2.setOutputMarkupId(true);
    inputContainer.add(password2);
    password1.add(new AjaxFormComponentUpdatingBehavior("change") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            boolean required = !StringUtils.isEmpty(password1.getModelObject());
            password2.setRequired(required);
            changePasswordPerformed();
        }
    });
    IModel<String> password2ValidationModel = (IModel<String>) () -> {
        String s1 = password1.getModelObject();
        String s2 = password2.getValue();
        if (StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2)) {
            return "";
        }
        if (!Objects.equals(s1, s2)) {
            return PageBase.createStringResourceStatic(null, "passwordPanel.error").getString();
        }
        return "";
    };
    Label password2ValidationMessage = new Label(ID_PASSWORD_TWO_VALIDATION_MESSAGE, password2ValidationModel);
    password2ValidationMessage.setOutputMarkupId(true);
    inputContainer.add(password2ValidationMessage);
    password1.add(new AjaxFormComponentUpdatingBehavior("keyup input") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            limitationsModel.reset();
            validationPanel.refreshItems(target);
            updatePasswordValidation(target);
            target.add(password2ValidationMessage);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(Duration.ofMillis(500), true));
            attributes.setChannel(new AjaxChannel("Drop", AjaxChannel.Type.DROP));
        }
    });
    PasswordValidator pass2Validator = new PasswordValidator(password1);
    password2.add(pass2Validator);
    password2.add(new AjaxFormComponentUpdatingBehavior("keyup input") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.add(password2ValidationMessage);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(Duration.ofMillis(500), true));
            attributes.setChannel(new AjaxChannel("Drop", AjaxChannel.Type.DROP));
        }
    });
    final WebMarkupContainer linkContainer = new WebMarkupContainer(ID_LINK_CONTAINER) {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return !passwordInputVisible;
        }
    };
    inputContainer.setOutputMarkupId(true);
    linkContainer.setOutputMarkupId(true);
    add(linkContainer);
    final Label passwordSetLabel = new Label(ID_PASSWORD_SET, new ResourceModel("passwordPanel.passwordSet"));
    linkContainer.add(passwordSetLabel);
    final Label passwordRemoveLabel = new Label(ID_PASSWORD_REMOVE, new ResourceModel("passwordPanel.passwordRemoveLabel"));
    passwordRemoveLabel.setVisible(false);
    linkContainer.add(passwordRemoveLabel);
    AjaxLink<Void> link = new AjaxLink<Void>(ID_CHANGE_PASSWORD_LINK) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            clearPasswordInput = true;
            setPasswordInput = false;
            onLinkClick(target);
        }

        @Override
        public boolean isVisible() {
            return !passwordInputVisible && model != null && model.getObject() != null;
        }
    };
    link.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return !isReadOnly;
        }
    });
    link.setBody(new ResourceModel("passwordPanel.passwordChange"));
    link.setOutputMarkupId(true);
    linkContainer.add(link);
    final WebMarkupContainer removeButtonContainer = new WebMarkupContainer(ID_REMOVE_BUTTON_CONTAINER);
    AjaxLink<Void> removePassword = new AjaxLink<Void>(ID_REMOVE_PASSWORD_LINK) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            onRemovePassword(model, target);
        }
    };
    removePassword.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            PageBase pageBase = getPageBase();
            if (pageBase == null) {
                return false;
            }
            if (pageBase instanceof PageUserSelfProfile || pageBase instanceof PageOrgSelfProfile || pageBase instanceof PageRoleSelfProfile || pageBase instanceof PageServiceSelfProfile) {
                return false;
            }
            if (pageBase instanceof PageAdminFocus && !((PageAdminFocus) pageBase).isLoggedInFocusPage() && model.getObject() != null) {
                return true;
            }
            return false;
        }
    });
    removePassword.setBody(new ResourceModel("passwordPanel.passwordRemove"));
    removePassword.setOutputMarkupId(true);
    removeButtonContainer.add(removePassword);
    add(removeButtonContainer);
}
Also used : ThrottlingSettings(org.apache.wicket.ajax.attributes.ThrottlingSettings) ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) PageAdminFocus(com.evolveum.midpoint.web.page.admin.PageAdminFocus) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) Label(org.apache.wicket.markup.html.basic.Label) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) PageRoleSelfProfile(com.evolveum.midpoint.web.page.self.PageRoleSelfProfile) ResourceModel(org.apache.wicket.model.ResourceModel) List(java.util.List) ArrayList(java.util.ArrayList) AjaxChannel(org.apache.wicket.ajax.AjaxChannel) VisibleEnableBehaviour(com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) AjaxFormComponentUpdatingBehavior(org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior) IModel(org.apache.wicket.model.IModel) PageServiceSelfProfile(com.evolveum.midpoint.web.page.self.PageServiceSelfProfile) PasswordTextField(org.apache.wicket.markup.html.form.PasswordTextField) PageBase(com.evolveum.midpoint.gui.api.page.PageBase) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) AjaxRequestAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes) PageUserSelfProfile(com.evolveum.midpoint.web.page.self.PageUserSelfProfile) ComponentTag(org.apache.wicket.markup.ComponentTag) LoadableModel(com.evolveum.midpoint.gui.api.model.LoadableModel) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) PageOrgSelfProfile(com.evolveum.midpoint.web.page.self.PageOrgSelfProfile)

Example 9 with StringLimitationResult

use of com.evolveum.midpoint.model.api.validator.StringLimitationResult in project midpoint by Evolveum.

the class ValuePolicyProcessor method testLength.

private StringLimitationResult testLength(String value, LimitationsType limitations, OperationResult result, List<LocalizableMessage> messages) {
    if (limitations.getMinLength() == null && limitations.getMaxLength() == null) {
        return null;
    }
    StringLimitationResult limitation = new StringLimitationResult();
    limitation.setMinOccurs(limitations.getMinLength());
    limitation.setMaxOccurs(limitations.getMaxLength());
    PolyStringType name = new PolyStringType("characters");
    PolyStringTranslationType translation = new PolyStringTranslationType();
    translation.setKey("ValuePolicy.characters");
    name.setTranslation(translation);
    limitation.setName(name);
    limitation.setSuccess(true);
    if (limitations.getMinLength() != null && value.length() < limitations.getMinLength()) {
        LocalizableMessage msg = new LocalizableMessageBuilder().key("ValuePolicy.minimalSizeNotMet").arg(limitations.getMinLength()).arg(value.length()).build();
        result.addSubresult(new OperationResult("Check global minimal length", OperationResultStatus.FATAL_ERROR, msg));
        messages.add(msg);
        limitation.setSuccess(false);
    }
    if (limitations.getMaxLength() != null && value.length() > limitations.getMaxLength()) {
        LocalizableMessage msg = new LocalizableMessageBuilder().key("ValuePolicy.maximalSizeExceeded").arg(limitations.getMaxLength()).arg(value.length()).build();
        result.addSubresult(new OperationResult("Check global maximal length", OperationResultStatus.FATAL_ERROR, msg));
        messages.add(msg);
        limitation.setSuccess(false);
    }
    return limitation;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder) PolyStringTranslationType(com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)

Example 10 with StringLimitationResult

use of com.evolveum.midpoint.model.api.validator.StringLimitationResult in project midpoint by Evolveum.

the class ValuePolicyProcessor method testMaximalOccurrence.

private StringLimitationResult testMaximalOccurrence(StringLimitType stringLimitation, int count, OperationResult result, List<LocalizableMessage> messages, StringLimitationResult limitation) {
    if (stringLimitation.getMaxOccurs() == null) {
        return limitation;
    }
    if (limitation == null) {
        limitation = new StringLimitationResult();
        limitation.setMaxOccurs(stringLimitation.getMaxOccurs());
        limitation.setSuccess(true);
    } else {
        limitation.setMaxOccurs(stringLimitation.getMaxOccurs());
    }
    if (count > stringLimitation.getMaxOccurs()) {
        LocalizableMessage msg = new LocalizableMessageBuilder().key("ValuePolicy.maximalOccurrenceExceeded").arg(stringLimitation.getMaxOccurs()).arg(stringLimitation.getDescription()).arg(count).build();
        result.addSubresult(new OperationResult("Check maximal occurrence of characters", OperationResultStatus.FATAL_ERROR, msg));
        messages.add(msg);
        limitation.setSuccess(false);
    }
    return limitation;
}
Also used : StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder)

Aggregations

StringLimitationResult (com.evolveum.midpoint.model.api.validator.StringLimitationResult)13 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)10 LocalizableMessage (com.evolveum.midpoint.util.LocalizableMessage)8 LocalizableMessageBuilder (com.evolveum.midpoint.util.LocalizableMessageBuilder)7 PolyStringTranslationType (com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)6 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)6 IModel (org.apache.wicket.model.IModel)4 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)3 LoadableModel (com.evolveum.midpoint.gui.api.model.LoadableModel)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)2 Task (com.evolveum.midpoint.task.api.Task)2 LocalizableMessageListBuilder (com.evolveum.midpoint.util.LocalizableMessageListBuilder)2 Trace (com.evolveum.midpoint.util.logging.Trace)2 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)2 LimitationsType (com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType)2 StringLimitType (com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType)2 java.util (java.util)2 CollectionUtils (org.apache.commons.collections4.CollectionUtils)2