Search in sources :

Example 1 with INullAcceptingValidator

use of org.apache.wicket.validation.INullAcceptingValidator in project projectforge by micromata.

the class UserEditForm method addPasswordFields.

@SuppressWarnings("serial")
private void addPasswordFields() {
    // Password
    final FieldsetPanel fs = gridBuilder.newFieldset(getString("password"), getString("passwordRepeat"));
    final PasswordTextField passwordField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "password")) {

        @Override
        protected void onComponentTag(final ComponentTag tag) {
            super.onComponentTag(tag);
            if (passwordUser == null) {
                tag.put("value", "");
            }
        }
    };
    passwordField.setMarkupId("password").setOutputMarkupId(true);
    passwordField.setResetPassword(false).setRequired(isNew());
    // Password repeat
    final PasswordTextField passwordRepeatField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "passwordRepeat")) {

        @Override
        protected void onComponentTag(final ComponentTag tag) {
            super.onComponentTag(tag);
            if (passwordUser == null) {
                tag.put("value", "");
            }
        }
    };
    passwordRepeatField.setMarkupId("passwordRepeat").setOutputMarkupId(true);
    passwordRepeatField.setResetPassword(false).setRequired(false);
    // validation
    passwordRepeatField.add((INullAcceptingValidator<String>) validatable -> {
        final String passwordRepeatInput = validatable.getValue();
        passwordField.validate();
        final String passwordInput = passwordField.getConvertedInput();
        if (StringUtils.isEmpty(passwordInput) == true && StringUtils.isEmpty(passwordRepeatInput) == true) {
            passwordUser = null;
            return;
        }
        if (StringUtils.equals(passwordInput, passwordRepeatInput) == false) {
            passwordUser = null;
            validatable.error(new ValidationError().addKey("user.error.passwordAndRepeatDoesNotMatch"));
            return;
        }
        if (passwordUser == null) {
            final List<I18nKeyAndParams> errorMsgKeys = passwordQualityService.checkPasswordQuality(passwordInput.toCharArray());
            if (errorMsgKeys.isEmpty() == false) {
                for (I18nKeyAndParams errorMsgKey : errorMsgKeys) {
                    final String localizedMessage = I18nHelper.getLocalizedMessage(errorMsgKey);
                    validatable.error(new ValidationError().setMessage(localizedMessage));
                }
            } else {
                passwordUser = new PFUserDO();
                char[] pw = passwordInput.toCharArray();
                userService.createEncryptedPassword(passwordUser, pw);
                LoginHandler.clearPassword(pw);
            }
        }
    });
    WicketUtils.setPercentSize(passwordField, 50);
    WicketUtils.setPercentSize(passwordRepeatField, 50);
    fs.add(passwordField);
    fs.add(passwordRepeatField);
    final I18nKeyAndParams passwordQualityI18nKeyAndParams = passwordQualityService.getPasswordQualityI18nKeyAndParams();
    fs.addHelpIcon(I18nHelper.getLocalizedMessage(passwordQualityI18nKeyAndParams));
}
Also used : java.util(java.util) SpringBean(org.apache.wicket.spring.injection.annot.SpringBean) TimeNotation(org.projectforge.framework.time.TimeNotation) DateTimeFormatter(org.projectforge.framework.time.DateTimeFormatter) SimpleDateFormat(java.text.SimpleDateFormat) GroupDO(org.projectforge.framework.persistence.user.entities.GroupDO) StringUtils(org.apache.commons.lang3.StringUtils) Login(org.projectforge.business.login.Login) Gender(org.projectforge.framework.persistence.user.entities.Gender) GridBuilder(org.projectforge.web.wicket.bootstrap.GridBuilder) GroupService(org.projectforge.business.group.service.GroupService) LoginHandler(org.projectforge.business.login.LoginHandler) ThreadLocalUserContext(org.projectforge.framework.persistence.user.api.ThreadLocalUserContext) WicketUtils(org.projectforge.web.wicket.WicketUtils) ApplicationContextProvider(org.projectforge.framework.configuration.ApplicationContextProvider) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ComponentTag(org.apache.wicket.markup.ComponentTag) AbstractEditForm(org.projectforge.web.wicket.AbstractEditForm) Const(org.projectforge.Const) I18nKeyAndParams(org.projectforge.framework.i18n.I18nKeyAndParams) Logger(org.slf4j.Logger) org.projectforge.web.wicket.components(org.projectforge.web.wicket.components) StringHelper(org.projectforge.common.StringHelper) GridSize(org.projectforge.web.wicket.bootstrap.GridSize) Model(org.apache.wicket.model.Model) AccessChecker(org.projectforge.framework.access.AccessChecker) WebConstants(org.projectforge.web.wicket.WebConstants) org.projectforge.business.user(org.projectforge.business.user) ValidationError(org.apache.wicket.validation.ValidationError) AttributeModifier(org.apache.wicket.AttributeModifier) ConfigurationService(org.projectforge.business.configuration.ConfigurationService) DivTextPanel(org.projectforge.web.wicket.flowlayout.DivTextPanel) IValidator(org.apache.wicket.validation.IValidator) Select2MultiChoice(org.wicketstuff.select2.Select2MultiChoice) PasswordQualityService(org.projectforge.business.password.PasswordQualityService) FieldsetPanel(org.projectforge.web.wicket.flowlayout.FieldsetPanel) PropertyModel(org.apache.wicket.model.PropertyModel) MultiChoiceListHelper(org.projectforge.web.common.MultiChoiceListHelper) PFUserDO(org.projectforge.framework.persistence.user.entities.PFUserDO) org.apache.wicket.markup.html.form(org.apache.wicket.markup.html.form) UserService(org.projectforge.business.user.service.UserService) INullAcceptingValidator(org.apache.wicket.validation.INullAcceptingValidator) org.projectforge.business.ldap(org.projectforge.business.ldap) Configuration(org.projectforge.framework.configuration.Configuration) I18nHelper(org.projectforge.framework.i18n.I18nHelper) IFormValidator(org.apache.wicket.markup.html.form.validation.IFormValidator) AjaxButton(org.apache.wicket.ajax.markup.html.form.AjaxButton) ResourceModel(org.apache.wicket.model.ResourceModel) ComponentTag(org.apache.wicket.markup.ComponentTag) I18nKeyAndParams(org.projectforge.framework.i18n.I18nKeyAndParams) FieldsetPanel(org.projectforge.web.wicket.flowlayout.FieldsetPanel) ValidationError(org.apache.wicket.validation.ValidationError) PFUserDO(org.projectforge.framework.persistence.user.entities.PFUserDO)

Example 2 with INullAcceptingValidator

use of org.apache.wicket.validation.INullAcceptingValidator in project projectforge by micromata.

the class UserEditForm method addWlanPasswordFields.

private void addWlanPasswordFields() {
    // wlan password
    final FieldsetPanel fs = gridBuilder.newFieldset(getString("ldap.wlanSambaPassword"), getString("passwordRepeat"));
    final PasswordTextField passwordField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "wlanPassword")) {

        @Override
        protected void onComponentTag(final ComponentTag tag) {
            super.onComponentTag(tag);
            if (wlanPasswordValid == false) {
                tag.put("value", "");
            }
        }
    };
    passwordField.setMarkupId("wlanPassword").setOutputMarkupId(true);
    passwordField.setResetPassword(false).setRequired(isNew());
    // wlan password repeat
    final PasswordTextField passwordRepeatField = new PasswordTextField(fs.getTextFieldId(), new PropertyModel<>(this, "wlanPasswordRepeat")) {

        @Override
        protected void onComponentTag(final ComponentTag tag) {
            super.onComponentTag(tag);
            if (wlanPasswordValid == false) {
                tag.put("value", "");
            }
        }
    };
    passwordRepeatField.setMarkupId("wlanPasswordRepeat").setOutputMarkupId(true);
    passwordRepeatField.setResetPassword(false).setRequired(false);
    // validation
    passwordRepeatField.add((INullAcceptingValidator<String>) validatable -> {
        wlanPasswordValid = false;
        final String passwordRepeatInput = validatable.getValue();
        passwordField.validate();
        final String passwordInput = passwordField.getConvertedInput();
        if (StringUtils.isEmpty(passwordInput) && StringUtils.isEmpty(passwordRepeatInput)) {
            return;
        }
        if (StringUtils.equals(passwordInput, passwordRepeatInput) == false) {
            validatable.error(new ValidationError().addKey("user.error.passwordAndRepeatDoesNotMatch"));
            return;
        }
        final List<I18nKeyAndParams> errorMsgKeys = passwordQualityService.checkPasswordQuality(passwordInput.toCharArray());
        if (errorMsgKeys.isEmpty() == false) {
            for (I18nKeyAndParams errorMsgKey : errorMsgKeys) {
                final String localizedMessage = I18nHelper.getLocalizedMessage(errorMsgKey);
                validatable.error(new ValidationError().setMessage(localizedMessage));
            }
        } else {
            wlanPasswordValid = true;
        }
    });
    WicketUtils.setPercentSize(passwordField, 50);
    WicketUtils.setPercentSize(passwordRepeatField, 50);
    fs.add(passwordField);
    fs.add(passwordRepeatField);
    final I18nKeyAndParams passwordQualityI18nKeyAndParams = passwordQualityService.getPasswordQualityI18nKeyAndParams();
    fs.addHelpIcon(I18nHelper.getLocalizedMessage(passwordQualityI18nKeyAndParams));
}
Also used : java.util(java.util) SpringBean(org.apache.wicket.spring.injection.annot.SpringBean) TimeNotation(org.projectforge.framework.time.TimeNotation) DateTimeFormatter(org.projectforge.framework.time.DateTimeFormatter) SimpleDateFormat(java.text.SimpleDateFormat) GroupDO(org.projectforge.framework.persistence.user.entities.GroupDO) StringUtils(org.apache.commons.lang3.StringUtils) Login(org.projectforge.business.login.Login) Gender(org.projectforge.framework.persistence.user.entities.Gender) GridBuilder(org.projectforge.web.wicket.bootstrap.GridBuilder) GroupService(org.projectforge.business.group.service.GroupService) LoginHandler(org.projectforge.business.login.LoginHandler) ThreadLocalUserContext(org.projectforge.framework.persistence.user.api.ThreadLocalUserContext) WicketUtils(org.projectforge.web.wicket.WicketUtils) ApplicationContextProvider(org.projectforge.framework.configuration.ApplicationContextProvider) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ComponentTag(org.apache.wicket.markup.ComponentTag) AbstractEditForm(org.projectforge.web.wicket.AbstractEditForm) Const(org.projectforge.Const) I18nKeyAndParams(org.projectforge.framework.i18n.I18nKeyAndParams) Logger(org.slf4j.Logger) org.projectforge.web.wicket.components(org.projectforge.web.wicket.components) StringHelper(org.projectforge.common.StringHelper) GridSize(org.projectforge.web.wicket.bootstrap.GridSize) Model(org.apache.wicket.model.Model) AccessChecker(org.projectforge.framework.access.AccessChecker) WebConstants(org.projectforge.web.wicket.WebConstants) org.projectforge.business.user(org.projectforge.business.user) ValidationError(org.apache.wicket.validation.ValidationError) AttributeModifier(org.apache.wicket.AttributeModifier) ConfigurationService(org.projectforge.business.configuration.ConfigurationService) DivTextPanel(org.projectforge.web.wicket.flowlayout.DivTextPanel) IValidator(org.apache.wicket.validation.IValidator) Select2MultiChoice(org.wicketstuff.select2.Select2MultiChoice) PasswordQualityService(org.projectforge.business.password.PasswordQualityService) FieldsetPanel(org.projectforge.web.wicket.flowlayout.FieldsetPanel) PropertyModel(org.apache.wicket.model.PropertyModel) MultiChoiceListHelper(org.projectforge.web.common.MultiChoiceListHelper) PFUserDO(org.projectforge.framework.persistence.user.entities.PFUserDO) org.apache.wicket.markup.html.form(org.apache.wicket.markup.html.form) UserService(org.projectforge.business.user.service.UserService) INullAcceptingValidator(org.apache.wicket.validation.INullAcceptingValidator) org.projectforge.business.ldap(org.projectforge.business.ldap) Configuration(org.projectforge.framework.configuration.Configuration) I18nHelper(org.projectforge.framework.i18n.I18nHelper) IFormValidator(org.apache.wicket.markup.html.form.validation.IFormValidator) AjaxButton(org.apache.wicket.ajax.markup.html.form.AjaxButton) ResourceModel(org.apache.wicket.model.ResourceModel) ComponentTag(org.apache.wicket.markup.ComponentTag) I18nKeyAndParams(org.projectforge.framework.i18n.I18nKeyAndParams) FieldsetPanel(org.projectforge.web.wicket.flowlayout.FieldsetPanel) ValidationError(org.apache.wicket.validation.ValidationError)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 StringUtils (org.apache.commons.lang3.StringUtils)2 AttributeModifier (org.apache.wicket.AttributeModifier)2 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)2 AjaxButton (org.apache.wicket.ajax.markup.html.form.AjaxButton)2 ComponentTag (org.apache.wicket.markup.ComponentTag)2 org.apache.wicket.markup.html.form (org.apache.wicket.markup.html.form)2 IFormValidator (org.apache.wicket.markup.html.form.validation.IFormValidator)2 Model (org.apache.wicket.model.Model)2 PropertyModel (org.apache.wicket.model.PropertyModel)2 ResourceModel (org.apache.wicket.model.ResourceModel)2 SpringBean (org.apache.wicket.spring.injection.annot.SpringBean)2 INullAcceptingValidator (org.apache.wicket.validation.INullAcceptingValidator)2 IValidator (org.apache.wicket.validation.IValidator)2 ValidationError (org.apache.wicket.validation.ValidationError)2 Const (org.projectforge.Const)2 ConfigurationService (org.projectforge.business.configuration.ConfigurationService)2 GroupService (org.projectforge.business.group.service.GroupService)2 org.projectforge.business.ldap (org.projectforge.business.ldap)2