Use ExternalId.isValidUsername instead of ExternalId.USER_NAME_PATTERN_REGEX

Since ExternalId.USER_NAME_PATTERN_REGEX is now no longer used by
external callers it is made private.

Change-Id: Id7a108bf39baffc52273879bbf8973dbbea0ae0c
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-02-09 14:48:19 +01:00
committed by David Pursehouse
parent 45bf80651a
commit 7a5364662f
8 changed files with 8 additions and 11 deletions

View File

@@ -105,7 +105,7 @@ public class AccountResolver {
return Collections.emptySet(); return Collections.emptySet();
} }
if (nameOrEmail.matches(ExternalId.USER_NAME_PATTERN_REGEX)) { if (ExternalId.isValidUsername(nameOrEmail)) {
AccountState who = byId.getByUsername(nameOrEmail); AccountState who = byId.getByUsername(nameOrEmail);
if (who != null) { if (who != null) {
return Collections.singleton(who.getAccount().getId()); return Collections.singleton(who.getAccount().getId());

View File

@@ -87,7 +87,7 @@ public class ChangeUserName implements Callable<VoidResult> {
ExternalIdsUpdate externalIdsUpdate = externalIdsUpdateFactory.create(); ExternalIdsUpdate externalIdsUpdate = externalIdsUpdateFactory.create();
if (newUsername != null && !newUsername.isEmpty()) { if (newUsername != null && !newUsername.isEmpty()) {
if (!newUsername.matches(ExternalId.USER_NAME_PATTERN_REGEX)) { if (!ExternalId.isValidUsername(newUsername)) {
throw new InvalidUserNameException(); throw new InvalidUserNameException();
} }

View File

@@ -111,7 +111,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
throw new BadRequestException("username must match URL"); throw new BadRequestException("username must match URL");
} }
if (!username.matches(ExternalId.USER_NAME_PATTERN_REGEX)) { if (!ExternalId.isValidUsername(username)) {
throw new BadRequestException( throw new BadRequestException(
"Username '" + username + "' must contain only letters, numbers, _, - or ."); "Username '" + username + "' must contain only letters, numbers, _, - or .");
} }

View File

@@ -46,7 +46,7 @@ public abstract class ExternalId implements Serializable {
private static final String USER_NAME_PATTERN_LAST_REGEX = "[a-zA-Z0-9]"; private static final String USER_NAME_PATTERN_LAST_REGEX = "[a-zA-Z0-9]";
/** Regular expression that a username must match. */ /** Regular expression that a username must match. */
public static final String USER_NAME_PATTERN_REGEX = private static final String USER_NAME_PATTERN_REGEX =
"^(" "^("
+ // + //
USER_NAME_PATTERN_FIRST_REGEX USER_NAME_PATTERN_FIRST_REGEX

View File

@@ -14,10 +14,7 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
/** /** Error indicating the SSH user name does not match the expected pattern. */
* Error indicating the SSH user name does not match {@link
* com.google.gerrit.server.account.ExternalId#USER_NAME_PATTERN_REGEX} pattern.
*/
public class InvalidUserNameException extends Exception { public class InvalidUserNameException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@@ -91,7 +91,7 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
} }
private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException { private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException {
if (!user.matches(ExternalId.USER_NAME_PATTERN_REGEX)) { if (!ExternalId.isValidUsername(user)) {
throw new CmdLineException(owner, "user \"" + user + "\" not found"); throw new CmdLineException(owner, "user \"" + user + "\" not found");
} }

View File

@@ -199,7 +199,7 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
} }
private Account createAccountByLdap(String user) throws IOException { private Account createAccountByLdap(String user) throws IOException {
if (!user.matches(ExternalId.USER_NAME_PATTERN_REGEX)) { if (!ExternalId.isValidUsername(user)) {
return null; return null;
} }