Account.java: introduce compiled pattern and use where applicable
There is no point in compiling USER_NAME_PATTERN over and over again in all those places where it is used hence compiled version was added. Note that original version was kept so that there is no collateral damage in plugins that use it. Change-Id: I8082b2ad3e04a6407e9c41f969a0e274386b63ab Signed-off-by: Jacek Centkowski <jcentkowski@collab.net>
This commit is contained in:
@@ -16,6 +16,7 @@ gwt_module(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//gerrit-extension-api:client",
|
||||
"//lib:guava",
|
||||
"//lib:gwtorm-client",
|
||||
"//lib:gwtorm-client_src",
|
||||
],
|
||||
|
||||
@@ -16,11 +16,13 @@ package com.google.gerrit.reviewdb.client;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.RefNames.REFS_USERS;
|
||||
|
||||
import com.google.common.annotations.GwtIncompatible;
|
||||
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
||||
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
|
||||
import com.google.gwtorm.client.Column;
|
||||
import com.google.gwtorm.client.IntKey;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Information about a single user.
|
||||
@@ -66,6 +68,9 @@ public final class Account {
|
||||
+ //
|
||||
")$";
|
||||
|
||||
@GwtIncompatible("Unemulated class java.util.regex.Pattern")
|
||||
public static final Pattern USER_NAME_PATTERN_COMPILED = Pattern.compile(USER_NAME_PATTERN);
|
||||
|
||||
/** Key local to Gerrit to identify a user. */
|
||||
public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
@@ -105,7 +106,7 @@ public class AccountResolver {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
if (nameOrEmail.matches(Account.USER_NAME_PATTERN)) {
|
||||
if (USER_NAME_PATTERN_COMPILED.matcher(nameOrEmail).matches()) {
|
||||
AccountState who = byId.getByUsername(nameOrEmail);
|
||||
if (who != null) {
|
||||
return Collections.singleton(who.getAccount().getId());
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
|
||||
import static com.google.gerrit.server.account.ExternalId.SCHEME_USERNAME;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.common.errors.NameAlreadyUsedException;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.ssh.SshKeyCache;
|
||||
@@ -31,7 +31,6 @@ import com.google.inject.assistedinject.Assisted;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.regex.Pattern;
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -39,7 +38,6 @@ import org.slf4j.LoggerFactory;
|
||||
/** Operation to change the username of an account. */
|
||||
public class ChangeUserName implements Callable<VoidResult> {
|
||||
private static final Logger log = LoggerFactory.getLogger(ChangeUserName.class);
|
||||
private static final Pattern USER_NAME_PATTERN = Pattern.compile(Account.USER_NAME_PATTERN);
|
||||
|
||||
public static final String USERNAME_CANNOT_BE_CHANGED = "Username cannot be changed.";
|
||||
|
||||
@@ -90,7 +88,7 @@ public class ChangeUserName implements Callable<VoidResult> {
|
||||
|
||||
ExternalIdsUpdate externalIdsUpdate = externalIdsUpdateFactory.create();
|
||||
if (newUsername != null && !newUsername.isEmpty()) {
|
||||
if (!USER_NAME_PATTERN.matcher(newUsername).matches()) {
|
||||
if (!USER_NAME_PATTERN_COMPILED.matcher(newUsername).matches()) {
|
||||
throw new InvalidUserNameException();
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN;
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
|
||||
import static com.google.gerrit.server.account.ExternalId.SCHEME_MAILTO;
|
||||
|
||||
import com.google.gerrit.audit.AuditService;
|
||||
@@ -112,7 +113,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
|
||||
throw new BadRequestException("username must match URL");
|
||||
}
|
||||
|
||||
if (!username.matches(Account.USER_NAME_PATTERN)) {
|
||||
if (!USER_NAME_PATTERN_COMPILED.matcher(username).matches()) {
|
||||
throw new BadRequestException(
|
||||
"Username '" + username + "' must comply with [" + USER_NAME_PATTERN + "] pattern.");
|
||||
}
|
||||
|
||||
@@ -16,7 +16,10 @@ package com.google.gerrit.server.account;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
|
||||
/** Error indicating the SSH user name does not match {@link Account#USER_NAME_PATTERN} pattern. */
|
||||
/**
|
||||
* Error indicating the SSH user name does not match {@link Account#USER_NAME_PATTERN_COMPILED}
|
||||
* pattern.
|
||||
*/
|
||||
public class InvalidUserNameException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.server.args4j;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
|
||||
|
||||
import com.google.gerrit.extensions.client.AuthType;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
@@ -90,7 +92,7 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
|
||||
}
|
||||
|
||||
private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException {
|
||||
if (!user.matches(Account.USER_NAME_PATTERN)) {
|
||||
if (!USER_NAME_PATTERN_COMPILED.matcher(user).matches()) {
|
||||
throw new CmdLineException(owner, "user \"" + user + "\" not found");
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.server.group;
|
||||
|
||||
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.audit.AuditService;
|
||||
@@ -198,7 +200,7 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
|
||||
}
|
||||
|
||||
private Account createAccountByLdap(String user) throws IOException {
|
||||
if (!user.matches(Account.USER_NAME_PATTERN)) {
|
||||
if (!USER_NAME_PATTERN_COMPILED.matcher(user).matches()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user