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