ExternalIdCache: Add method to get external IDs by account ID + scheme

Change-Id: I800c41923facbfe9194ce47716ed2d23d0ffd70a
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-01-31 15:12:29 +01:00
parent 6d2fedd8e6
commit 20e5507d46
3 changed files with 13 additions and 15 deletions

View File

@@ -210,8 +210,8 @@ public class GpgKeys implements
@VisibleForTesting @VisibleForTesting
public static FluentIterable<AccountExternalId> getGpgExtIds( public static FluentIterable<AccountExternalId> getGpgExtIds(
ExternalIdCache externalIdCache, Account.Id accountId) { ExternalIdCache externalIdCache, Account.Id accountId) {
return FluentIterable.from(externalIdCache.byAccount(accountId)) return FluentIterable.from(
.filter(in -> in.isScheme(SCHEME_GPGKEY)); externalIdCache.byAccount(accountId, SCHEME_GPGKEY));
} }
private Iterable<AccountExternalId> getGpgExtIds(AccountResource rsrc) { private Iterable<AccountExternalId> getGpgExtIds(AccountResource rsrc) {

View File

@@ -30,7 +30,6 @@ import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@@ -76,7 +75,8 @@ public class ChangeUserName implements Callable<VoidResult> {
@Override @Override
public VoidResult call() throws OrmException, NameAlreadyUsedException, public VoidResult call() throws OrmException, NameAlreadyUsedException,
InvalidUserNameException, IOException { InvalidUserNameException, IOException {
final Collection<AccountExternalId> old = old(); Collection<AccountExternalId> old =
externalIdCache.byAccount(user.getAccountId(), SCHEME_USERNAME);
if (!old.isEmpty()) { if (!old.isEmpty()) {
throw new IllegalStateException(USERNAME_CANNOT_BE_CHANGED); throw new IllegalStateException(USERNAME_CANNOT_BE_CHANGED);
} }
@@ -128,15 +128,4 @@ public class ChangeUserName implements Callable<VoidResult> {
sshKeyCache.evict(newUsername); sshKeyCache.evict(newUsername);
return VoidResult.INSTANCE; return VoidResult.INSTANCE;
} }
private Collection<AccountExternalId> old() {
final Collection<AccountExternalId> r = new ArrayList<>(1);
for (AccountExternalId i : externalIdCache.byAccount(
user.getAccountId())) {
if (i.isScheme(SCHEME_USERNAME)) {
r.add(i);
}
}
return r;
}
} }

View File

@@ -14,11 +14,14 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import static java.util.stream.Collectors.toSet;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountExternalId; import com.google.gerrit.reviewdb.client.AccountExternalId;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set;
/** Caches external ids of all accounts */ /** Caches external ids of all accounts */
public interface ExternalIdCache { public interface ExternalIdCache {
@@ -40,4 +43,10 @@ public interface ExternalIdCache {
default void onRemove(Account.Id accountId, AccountExternalId.Key extIdKey) { default void onRemove(Account.Id accountId, AccountExternalId.Key extIdKey) {
onRemove(accountId, Collections.singleton(extIdKey)); onRemove(accountId, Collections.singleton(extIdKey));
} }
default Set<AccountExternalId> byAccount(Account.Id accountId,
String scheme) {
return byAccount(accountId).stream().filter(e -> e.isScheme(scheme))
.collect(toSet());
}
} }