Optimize USER_NAME_PATTERN string and its usage

The following issues from [1] are addressed by this change:
1. common pattern part was extracted
2. USER_NAME_PATTERN string is used to indicate that account
couldn't have been created as it is not compliant with it
3. number of lines was reduced in USER_NAME_PATTERN by combining
relevant characters together

In addition:
USER_NAME_PATTERN_LAST is derived from USER_NAME_PATTERN_FIRST
as they are equal.

[1] https://gerrit-review.googlesource.com/c/gerrit/+/194650

Change-Id: I02ce9c6b6d3b121050178851e2121b3419e7b830
Signed-off-by: Jacek Centkowski <jcentkowski@collab.net>
This commit is contained in:
Jacek Centkowski
2018-09-03 16:10:23 +02:00
parent 87bb52bcae
commit cf97d694fe
2 changed files with 8 additions and 10 deletions

View File

@@ -44,15 +44,14 @@ import java.sql.Timestamp;
* </ul> * </ul>
*/ */
public final class Account { public final class Account {
public static final String USER_NAME_PATTERN_FIRST = "[a-zA-Z0-9]"; private static final String USER_NAME_COMMON_PATTERN = "a-zA-Z0-9";
public static final String USER_NAME_PATTERN_REST = "[a-zA-Z0-9._@-]"; public static final String USER_NAME_PATTERN_FIRST = "[" + USER_NAME_COMMON_PATTERN + "]";
public static final String USER_NAME_PATTERN_LAST = "[a-zA-Z0-9]"; public static final String USER_NAME_PATTERN_REST = "[" + USER_NAME_COMMON_PATTERN + "._@-]";
public static final String USER_NAME_PATTERN_LAST = USER_NAME_PATTERN_FIRST;
/** Regular expression that {@link #userName} must match. */ /** Regular expression that {@link #userName} must match. */
public static final String USER_NAME_PATTERN = public static final String USER_NAME_PATTERN =
"^" "^("
+ //
"("
+ // + //
USER_NAME_PATTERN_FIRST USER_NAME_PATTERN_FIRST
+ // + //
@@ -65,9 +64,7 @@ public final class Account {
+ // + //
USER_NAME_PATTERN_FIRST USER_NAME_PATTERN_FIRST
+ // + //
")" ")$";
+ //
"$";
/** Key local to Gerrit to identify a user. */ /** Key local to Gerrit to identify a user. */
public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> { public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> {

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN;
import static com.google.gerrit.server.account.ExternalId.SCHEME_MAILTO; import static com.google.gerrit.server.account.ExternalId.SCHEME_MAILTO;
import com.google.gerrit.audit.AuditService; import com.google.gerrit.audit.AuditService;
@@ -113,7 +114,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
if (!username.matches(Account.USER_NAME_PATTERN)) { if (!username.matches(Account.USER_NAME_PATTERN)) {
throw new BadRequestException( throw new BadRequestException(
"Username '" + username + "' must contain only letters, numbers, _, - or ."); "Username '" + username + "' must comply with [" + USER_NAME_PATTERN + "] pattern.");
} }
Set<AccountGroup.Id> groups = parseGroups(input.groups); Set<AccountGroup.Id> groups = parseGroups(input.groups);