Skip to content

Commit

Permalink
fix: validate project name to be alphanumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jul 20, 2017
1 parent 8ea283a commit 0a0feca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
32 changes: 32 additions & 0 deletions server/zanata-war/src/main/java/org/zanata/action/ProjectHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,28 @@ public void initialize() {
}
}

public void onProjectNameChange(ValueChangeEvent e) {
if (!isValidName((String) e.getNewValue())) {
String componentId = e.getComponent().getId();
facesMessages.addToControl(componentId,
msgs.get("jsf.project.name.validation.alphanumeric"));
}
}

/**
* Check the name by removing any whitespaces in the string and
* make sure it contains at least an alphanumeric char
*/
public boolean isValidName(String name) {
String trimmedName = StringUtils.deleteWhitespace(name);
for (char c : trimmedName.toCharArray()) {
if (Character.isDigit(c) || Character.isLetter(c)) {
return true;
}
}
return false;
}

public void verifySlugAvailable(ValueChangeEvent e) {
String slug = (String) e.getNewValue();
validateSlug(slug, e.getComponent().getId());
Expand Down Expand Up @@ -740,6 +762,11 @@ public String update() {
&& !getSlug().equals(getInputSlugValue())) {
getInstance().setSlug(getInputSlugValue());
}
if (!isValidName(getInstance().getName())) {
facesMessages.addGlobal(SEVERITY_ERROR,
msgs.get("jsf.project.name.validation.alphanumeric"));
return null;
}
boolean softDeleted = false;
if (getInstance().getStatus() == EntityStatus.OBSOLETE) {
softDeleted = true;
Expand Down Expand Up @@ -768,6 +795,11 @@ public String persist() {
return null;
}
getInstance().setSlug(getInputSlugValue());
if (!isValidName(getInstance().getName())) {
facesMessages.addGlobal(SEVERITY_ERROR,
msgs.get("jsf.project.name.validation.alphanumeric"));
return null;
}
if (StringUtils.isEmpty(selectedProjectType)
|| selectedProjectType.equals("null")) {
facesMessages.addGlobal(SEVERITY_ERROR,
Expand Down
1 change: 1 addition & 0 deletions server/zanata-war/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ jsf.project.WebhookName=Webhook name
jsf.project.WebhookType.label=Type
jsf.project.InvalidUrl=Invalid URL: {0}
jsf.project.DuplicateUrl=Same URL is already in the list. {0}
jsf.project.name.validation.alphanumeric=Project name must contain at least one alphanumeric character
jsf.webhook.response.state={0}% {1}
jsf.webhook.test.label=Test webhook
jsf.webhook.test.tooltip=Fire a test event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
<h:outputLabel for="name">#{msgs['jsf.ProjectName']}</h:outputLabel>
<zanata:decorate id="name" componentId="nameField">
<h:inputText id="name" required="true" maxlength="80"
value="#{projectHome.instance.name}">
value="#{projectHome.instance.name}"
valueChangeListener="#{projectHome.onProjectNameChange}">
<f:validateLength minimum="1"/>
<a4j:ajax event="blur" render="nameField" execute="@this"/>
</h:inputText>
</zanata:decorate>
</div>
Expand Down

0 comments on commit 0a0feca

Please sign in to comment.