Search in sources :

Example 1 with AttributeValidation

use of org.gluu.model.attribute.AttributeValidation in project oxCore by GluuFederation.

the class AttributeValidator method validate.

@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
    GluuAttribute attribute = (GluuAttribute) comp.getAttributes().get("attribute");
    if (attribute == null) {
        ((UIInput) comp).setValid(true);
        return;
    }
    AttributeValidation attributeValidation = attribute.getAttributeValidation();
    Integer minvalue = attributeValidation != null ? attributeValidation.getMinLength() : null;
    Integer maxValue = attributeValidation != null ? attributeValidation.getMaxLength() : null;
    String regexpValue = attributeValidation != null ? attributeValidation.getRegexp() : null;
    String attributeValue = (String) value;
    // Minimum length validation
    if (minvalue != null) {
        int min = attributeValidation.getMinLength();
        if ((attributeValue != null) && (attributeValue.length() < min)) {
            ((UIInput) comp).setValid(false);
            FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should be at least " + min + " symbols. ");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(comp.getClientId(context), message);
        }
    }
    // default maxlength
    int max = 400;
    if (maxValue != null) {
        max = attributeValidation.getMaxLength();
    }
    // Maximum Length validation
    if ((attributeValue != null) && (attributeValue.length() > max)) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should not exceed " + max + " symbols. ");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(comp.getClientId(context), message);
    }
    if ((attribute.getName().equalsIgnoreCase("mail") && ((regexpValue == null) || (StringHelper.isEmpty(regexpValue))))) {
        regexpValue = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" + "[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
    }
    if ((regexpValue != null) && StringHelper.isNotEmpty(regexpValue)) {
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regexpValue);
        if ((attributeValue != null) && !(attributeValue.trim().equals(""))) {
            java.util.regex.Matcher matcher = pattern.matcher(attributeValue);
            boolean flag = matcher.matches();
            if (!flag) {
                ((UIInput) comp).setValid(false);
                FacesMessage message = new FacesMessage(attribute.getDisplayName() + " Format is invalid. ");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                context.addMessage(comp.getClientId(context), message);
            }
        }
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) UIInput(javax.faces.component.UIInput) FacesMessage(javax.faces.application.FacesMessage) GluuAttribute(org.gluu.model.GluuAttribute)

Example 2 with AttributeValidation

use of org.gluu.model.attribute.AttributeValidation in project oxTrust by GluuFederation.

the class UpdatePersonAction method validateConfirmPassword.

public void validateConfirmPassword(FacesContext context, UIComponent comp, Object value) {
    Pattern pattern = null;
    String attributeValue = (String) value;
    if (StringHelper.isEmpty(attributeValue)) {
        FacesMessage message = new FacesMessage("Value is required");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
    AttributeValidation validation = attributeService.getAttributeByName("userPassword").getAttributeValidation();
    boolean canValidate = validation != null && validation.getRegexp() != null && !validation.getRegexp().isEmpty();
    if (comp.getClientId().endsWith("custpasswordId")) {
        this.password = (String) value;
    } else if (comp.getClientId().endsWith("custconfirmpasswordId")) {
        this.confirmPassword = (String) value;
    }
    if (canValidate) {
        pattern = Pattern.compile(validation.getRegexp());
    }
    if (!StringHelper.equalsIgnoreCase(password, confirmPassword) && this.confirmPassword != null) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage("Both passwords should be the same!");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
    if (canValidate && (!pattern.matcher(this.password).matches() || !pattern.matcher(this.confirmPassword).matches())) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"), facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"));
        context.addMessage(comp.getClientId(context), message);
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) Pattern(java.util.regex.Pattern) ValidatorException(javax.faces.validator.ValidatorException) FacesMessage(javax.faces.application.FacesMessage) UIInput(javax.faces.component.UIInput)

Example 3 with AttributeValidation

use of org.gluu.model.attribute.AttributeValidation in project oxTrust by GluuFederation.

the class UpdateAttributeAction method add.

public String add() {
    if (this.attribute != null) {
        return OxTrustConstants.RESULT_SUCCESS;
    }
    this.update = false;
    this.showAttributeDeleteConfirmation = false;
    this.attribute = new GluuAttribute();
    attribute.setAttributeValidation(new AttributeValidation());
    this.attribute.setStatus(GluuStatus.ACTIVE);
    this.attribute.setEditType(new GluuUserRole[] { GluuUserRole.ADMIN });
    this.canEdit = true;
    return OxTrustConstants.RESULT_SUCCESS;
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) GluuAttribute(org.gluu.model.GluuAttribute)

Example 4 with AttributeValidation

use of org.gluu.model.attribute.AttributeValidation in project oxTrust by GluuFederation.

the class OxTrustAttributeValidator method validate.

@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
    GluuAttribute attribute = (GluuAttribute) comp.getAttributes().get("attribute");
    if (attribute == null) {
        ((UIInput) comp).setValid(true);
        return;
    }
    AttributeValidation attributeValidation = attribute.getAttributeValidation();
    Integer minvalue = attributeValidation != null ? attributeValidation.getMinLength() : null;
    Integer maxValue = attributeValidation != null ? attributeValidation.getMaxLength() : null;
    String regexpValue = attributeValidation != null ? attributeValidation.getRegexp() : null;
    String attributeValue = (String) value;
    // Minimum length validation
    if (minvalue != null) {
        int min = attributeValidation.getMinLength();
        if ((attributeValue != null) && (attributeValue.length() < min)) {
            ((UIInput) comp).setValid(false);
            FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should be at least " + min + " symbols. ");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(comp.getClientId(context), message);
        }
    }
    // default maxlength
    int max = 400;
    if (maxValue != null) {
        max = attributeValidation.getMaxLength();
    }
    // Maximum Length validation
    if ((attributeValue != null) && (attributeValue.length() > max)) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should not exceed " + max + " symbols. ");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(comp.getClientId(context), message);
    }
    if ((attribute.getName().equalsIgnoreCase("mail") && ((regexpValue == null) || (StringHelper.isEmpty(regexpValue))))) {
        regexpValue = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" + "[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
    }
    if ((regexpValue != null) && StringHelper.isNotEmpty(regexpValue)) {
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regexpValue);
        if ((attributeValue != null) && !(attributeValue.trim().equals(""))) {
            java.util.regex.Matcher matcher = pattern.matcher(attributeValue);
            boolean flag = matcher.matches();
            if (!flag) {
                ((UIInput) comp).setValid(false);
                FacesMessage message = new FacesMessage(attribute.getDisplayName() + " Format is invalid. ");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                context.addMessage(comp.getClientId(context), message);
            }
        }
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) UIInput(javax.faces.component.UIInput) FacesMessage(javax.faces.application.FacesMessage) GluuAttribute(org.gluu.model.GluuAttribute)

Example 5 with AttributeValidation

use of org.gluu.model.attribute.AttributeValidation in project oxTrust by GluuFederation.

the class PasswordValidator method validate.

@Override
public void validate(FacesContext arg0, UIComponent arg1, Object value) throws ValidatorException {
    if (attributeService == null) {
        attributeService = CdiUtil.bean(AttributeService.class);
    }
    GluuAttribute attributeByName = attributeService.getAttributeByName(USER_PASSWORD);
    AttributeValidation validation = attributeByName.getAttributeValidation();
    if (validation != null && validation.getRegexp() != null && !validation.getRegexp().isEmpty()) {
        pattern = Pattern.compile(validation.getRegexp());
        hasValidation = true;
    }
    if (hasValidation) {
        matcher = pattern.matcher(value.toString());
    }
    if (hasValidation && !matcher.matches()) {
        FacesMessage msg = new FacesMessage(facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"));
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(msg);
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) ValidatorException(javax.faces.validator.ValidatorException) AttributeService(org.gluu.oxtrust.service.AttributeService) FacesMessage(javax.faces.application.FacesMessage) GluuAttribute(org.gluu.model.GluuAttribute)

Aggregations

AttributeValidation (org.gluu.model.attribute.AttributeValidation)7 FacesMessage (javax.faces.application.FacesMessage)5 UIInput (javax.faces.component.UIInput)4 GluuAttribute (org.gluu.model.GluuAttribute)4 ValidatorException (javax.faces.validator.ValidatorException)2 Pattern (java.util.regex.Pattern)1 LanguageBean (org.gluu.oxauth.i18n.LanguageBean)1 AttributeService (org.gluu.oxtrust.service.AttributeService)1 AttributeService (org.gluu.service.AttributeService)1