Skip to content
Permalink
Browse files Browse the repository at this point in the history
added nickname validator; html escape existing values when displaying;
refs #24571
  • Loading branch information
Andrey Kozhushkov committed Mar 23, 2023
1 parent 1376ecf commit 8eadb32
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
@@ -0,0 +1,75 @@
/*
* This file is part of the Goobi viewer - a content presentation and management
* application for digitized objects.
*
* Visit these websites for more information.
* - http://www.intranda.com
* - http://digiverso.com
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobi.viewer.faces.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import io.goobi.viewer.messages.ViewerResourceBundle;

/**
* Syntax validator for names (e.g. nickname).
*/
@FacesValidator("nameValidator")
public class NameValidator implements Validator<String> {

private static final String REGEX = "^[\\w ]+$"; //NOSONAR input size is limited
private static final Pattern PATTERN = Pattern.compile(REGEX);

/* (non-Javadoc)
* @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
/** {@inheritDoc} */
@Override
public void validate(FacesContext context, UIComponent component, String value) throws ValidatorException {
if (!validate(value)) {
FacesMessage msg = new FacesMessage(ViewerResourceBundle.getTranslation("pi_errInvalid", null), "");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}

/**
* <p>
* validateEmailAddress.
* </p>
*
* @param email a {@link java.lang.String} object.
* @should match correct name
* @should not match invalid name
* @return a boolean.
*/
public static boolean validate(String name) {
if (name == null || name.length() > 10_000) {
return false;
}
Matcher m = PATTERN.matcher(name.toLowerCase());
return m.find();
}
}
Expand Up @@ -34,7 +34,7 @@
<div class="admin__boxed-entry -no-actions -w33">
<div class="admin__dashboard-user-welcome">
<div class="admin__dashboard-user-welcome-left">
<h2>#{msg.admin__dashboard_hello} #{userBean.user.nickName}</h2>
<h2>#{msg.admin__dashboard_hello} <h:outputText value="#{userBean.user.nickName}" escape="true" /></h2>
<p>#{msg.admin__dashboard_documentation}:<br/>
<a class="-bluelink" href="https://docs.goobi.io" target="_blank" rel="noopener">https://docs.goobi.io</a>
</p>
Expand Down
Expand Up @@ -70,7 +70,7 @@
</div>
<div class="admin__table-data" role="gridcell">
<!-- USER NAME -->
<span class="admin__table-name">#{user.nickName}</span>
<span class="admin__table-name"><h:outputText value="#{user.nickName}" escape="true" /></span>
<!-- USER EMAIL -->
<a href="mailto:#{user.email}" class="admin__table-email -textlink">#{user.email}</a>
<!-- EDIT -->
Expand Down
Expand Up @@ -24,7 +24,8 @@
<div class="col-11 col-lg-9 admin__form-input">
<h:inputText value="#{user.nickName}" id="displayName"
label="displayName" styleClass="form-control" required="false"
requiredMessage="#{msg.loginEnterDisplayName}" />
requiredMessage="#{msg.loginEnterDisplayName}"
validator="nameValidator" />
<span class="admin__form-help-text">#{msg.admin__user_display_name__help}</span>
</div>
<div class="col-1 admin__form-help-icon">
Expand Down
@@ -0,0 +1,46 @@
/*
* This file is part of the Goobi viewer - a content presentation and management
* application for digitized objects.
*
* Visit these websites for more information.
* - http://www.intranda.com
* - http://digiverso.com
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobi.viewer.faces.validators;

import org.junit.Assert;
import org.junit.Test;

public class NameValidatorTest {

/**
* @see NameValidator#validate(String)
* @verifies match correct name
*/
@Test
public void validate_shouldMatchCorrectName() throws Exception {
Assert.assertTrue(NameValidator.validate("John Doe"));
}

/**
* @see NameValidator#validate(String)
* @verifies not match invalid name
*/
@Test
public void validate_shouldNotMatchInvalidName() throws Exception {
Assert.assertTrue(NameValidator.validate("John Doe<script />"));
}
}

0 comments on commit 8eadb32

Please sign in to comment.