Treat empty username as non-existing username

Some callers of AccountState#getUserName and
IdentifiedUser#getUserName() explicitly check that the user name is
non-empty to then treat it the same way as a non-existing user name.
Save the callers from this effort and move this check to a generic
place.

Change-Id: I65cc3403405ec702a0c30ca1f4b17555d97c1315
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2018-01-18 19:21:50 +01:00
parent 9cb0b0cb88
commit 881431d714
9 changed files with 15 additions and 8 deletions

View File

@ -66,7 +66,7 @@ public class GetUserFilter implements Filter {
CurrentUser user = userProvider.get();
if (user != null && user.isIdentifiedUser()) {
IdentifiedUser who = user.asIdentifiedUser();
if (who.getUserName() != null && !who.getUserName().isEmpty()) {
if (who.getUserName() != null) {
req.setAttribute(REQ_ATTR_KEY, who.getUserName());
} else {
req.setAttribute(REQ_ATTR_KEY, "a/" + who.getAccountId());

View File

@ -575,7 +575,7 @@ class GitwebServlet extends HttpServlet {
IdentifiedUser u = userProvider.get().asIdentifiedUser();
String user = u.getUserName();
env.set("GERRIT_USER_NAME", user);
if (user != null && !user.isEmpty()) {
if (user != null) {
remoteUser = user;
} else {
remoteUser = "account-" + u.getAccountId();

View File

@ -226,7 +226,7 @@ public class ProjectQoSFilter implements Filter {
CurrentUser who = user.get();
if (who.isIdentifiedUser()) {
String name = who.asIdentifiedUser().getUserName();
if (name != null && !name.isEmpty()) {
if (name != null) {
userName = " (" + name + ")";
}
}

View File

@ -291,7 +291,10 @@ public class IdentifiedUser extends CurrentUser {
return accountId;
}
/** @return the user's user name; null if one has not been selected/assigned. */
/**
* @return the user's user name; null if one has not been selected/assigned or if the user name is
* empty.
*/
@Override
@Nullable
public String getUserName() {
@ -380,7 +383,7 @@ public class IdentifiedUser extends CurrentUser {
// necessarily want to publish through Git records.
//
String user = getUserName();
if (user == null || user.isEmpty()) {
if (user == null) {
user = "account-" + ua.getId().toString();
}

View File

@ -232,7 +232,7 @@ public class AccountManager {
if (!realm.allowsEdit(AccountFieldName.USER_NAME)
&& who.getUserName() != null
&& !Objects.equals(user.getUserName(), who.getUserName())) {
&& !Objects.equals(user.getUserName(), Strings.emptyToNull(who.getUserName()))) {
log.warn(
String.format(
"Not changing already set username %s to %s", user.getUserName(), who.getUserName()));

View File

@ -195,6 +195,9 @@ public class AccountState {
* Get the username, if one has been declared for this user.
*
* <p>The username is the {@link ExternalId} using the scheme {@link ExternalId#SCHEME_USERNAME}.
*
* @return the username, {@link Optional#empty()} if the user has no username, or if the username
* is empty
*/
public Optional<String> getUserName() {
return userName;

View File

@ -62,6 +62,7 @@ public abstract class ExternalId implements Serializable {
.stream()
.filter(e -> e.isScheme(SCHEME_USERNAME))
.map(e -> e.key().id())
.filter(u -> !Strings.isNullOrEmpty(u))
.findFirst();
}

View File

@ -204,7 +204,7 @@ final class ShowConnections extends SshCommand {
IdentifiedUser u = user.asIdentifiedUser();
if (!numeric) {
Optional<String> name = u.state().getUserName().filter(n -> !n.isEmpty());
Optional<String> name = u.state().getUserName();
if (name.isPresent()) {
return name.get();
}

@ -1 +1 @@
Subproject commit ddbd12781fd4b3156625608ee608950ab370b9a3
Subproject commit 771695a0903463aa91337323db136b68535e6746