com.google.gerrit.server.account.AuthRequest#getUserName: Return Optional

This makes it more explicit that callers must handle the case where the
returned user name is absent.

Change-Id: Ia1fcdb3c9b5be82c08c024b0e3fb3720762bd885
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-01-19 14:31:23 +01:00
parent ff3823e4eb
commit e72b87fb1e
3 changed files with 21 additions and 14 deletions

View File

@@ -185,7 +185,12 @@ public class AccountManager {
}
setInactiveFlag.deactivate(id.accountId());
} catch (Exception e) {
log.error("Unable to deactivate account " + authRequest.getUserName(), e);
log.error(
"Unable to deactivate account "
+ authRequest
.getUserName()
.orElse(" for external ID key " + authRequest.getExternalIdKey().get()),
e);
}
}
@@ -244,15 +249,15 @@ public class AccountManager {
}
if (!realm.allowsEdit(AccountFieldName.USER_NAME)
&& !Strings.isNullOrEmpty(who.getUserName())
&& !who.getUserName().equals(user.getUserName().orElse(null))) {
&& who.getUserName().isPresent()
&& !who.getUserName().equals(user.getUserName())) {
if (user.getUserName().isPresent()) {
log.warn(
"Not changing already set username {} to {}",
user.getUserName().get(),
who.getUserName());
who.getUserName().get());
} else {
log.warn("Not setting username to {}", who.getUserName());
log.warn("Not setting username to {}", who.getUserName().get());
}
}
@@ -275,7 +280,7 @@ public class AccountManager {
ExternalId extId =
ExternalId.createWithEmail(who.getExternalIdKey(), newId, who.getEmailAddress());
ExternalId userNameExtId =
!Strings.isNullOrEmpty(who.getUserName()) ? createUsername(newId, who.getUserName()) : null;
who.getUserName().isPresent() ? createUsername(newId, who.getUserName().get()) : null;
boolean isFirstAccount = awaitsFirstAccountCheck.getAndSet(false) && !accounts.hasAnyAccount();
@@ -310,7 +315,7 @@ public class AccountManager {
}
if (userNameExtId != null) {
sshKeyCache.evict(who.getUserName());
who.getUserName().ifPresent(sshKeyCache::evict);
}
IdentifiedUser user = userFactory.create(newId);

View File

@@ -18,7 +18,10 @@ import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_EXT
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GERRIT;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_MAILTO;
import com.google.common.base.Strings;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.server.account.externalids.ExternalId;
import java.util.Optional;
/**
* Information for {@link AccountManager#authenticate(AuthRequest)}.
@@ -59,7 +62,7 @@ public class AuthRequest {
private String password;
private String displayName;
private String emailAddress;
private String userName;
private Optional<String> userName;
private boolean skipAuthentication;
private String authPlugin;
private String authProvider;
@@ -111,12 +114,12 @@ public class AuthRequest {
emailAddress = email != null && email.length() > 0 ? email : null;
}
public String getUserName() {
public Optional<String> getUserName() {
return userName;
}
public void setUserName(String user) {
userName = user;
public void setUserName(@Nullable String user) {
userName = Optional.ofNullable(Strings.emptyToNull(user));
}
public boolean isSkipAuthentication() {

View File

@@ -92,7 +92,7 @@ public class OAuthRealm extends AbstractRealm {
OAuthUserInfo userInfo;
try {
userInfo = loginProvider.login(who.getUserName(), who.getPassword());
userInfo = loginProvider.login(who.getUserName().orElse(null), who.getPassword());
} catch (IOException e) {
throw new AccountException("Cannot authenticate", e);
}
@@ -100,8 +100,7 @@ public class OAuthRealm extends AbstractRealm {
throw new AccountException("Cannot authenticate");
}
if (!Strings.isNullOrEmpty(userInfo.getEmailAddress())
&& (Strings.isNullOrEmpty(who.getUserName())
|| !allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL))) {
&& (!who.getUserName().isPresent() || !allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL))) {
who.setEmailAddress(userInfo.getEmailAddress());
}
if (!Strings.isNullOrEmpty(userInfo.getDisplayName())