Merge "Unset preferred email when corresponding external ID is deleted"

This commit is contained in:
ekempin
2017-04-05 12:15:46 +00:00
committed by Gerrit Code Review
2 changed files with 24 additions and 17 deletions

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.acceptance.rest.account;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.GitUtil.fetch;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_MAILTO;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static org.junit.Assert.fail;
@@ -24,6 +25,7 @@ import com.github.rholder.retry.BlockStrategy;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.GerritConfig;
import com.google.gerrit.acceptance.PushOneCommit;
@@ -127,6 +129,18 @@ public class ExternalIdIT extends AbstractDaemonTest {
assertThat(results).containsExactlyElementsIn(expectedIds);
}
@Test
public void deleteExternalIdOfPreferredEmail() throws Exception {
String preferredEmail = gApi.accounts().self().get().email;
assertThat(preferredEmail).isNotNull();
gApi.accounts()
.self()
.deleteExternalIds(
ImmutableList.of(ExternalId.Key.create(SCHEME_MAILTO, preferredEmail).get()));
assertThat(gApi.accounts().self().get().email).isNull();
}
@Test
public void deleteExternalIds_Conflict() throws Exception {
List<String> toDelete = new ArrayList<>();

View File

@@ -24,12 +24,10 @@ import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIds;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -40,25 +38,19 @@ import java.util.Map;
import org.eclipse.jgit.errors.ConfigInvalidException;
public class DeleteExternalIds implements RestModifyView<AccountResource, List<String>> {
private final AccountByEmailCache accountByEmailCache;
private final AccountCache accountCache;
private final AccountManager accountManager;
private final ExternalIds externalIds;
private final ExternalIdsUpdate.User externalIdsUpdateFactory;
private final Provider<CurrentUser> self;
private final Provider<ReviewDb> dbProvider;
@Inject
DeleteExternalIds(
AccountByEmailCache accountByEmailCache,
AccountCache accountCache,
AccountManager accountManager,
ExternalIds externalIds,
ExternalIdsUpdate.User externalIdsUpdateFactory,
Provider<CurrentUser> self,
Provider<ReviewDb> dbProvider) {
this.accountByEmailCache = accountByEmailCache;
this.accountCache = accountCache;
this.accountManager = accountManager;
this.externalIds = externalIds;
this.externalIdsUpdateFactory = externalIdsUpdateFactory;
this.self = self;
this.dbProvider = dbProvider;
}
@@ -74,7 +66,6 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
throw new BadRequestException("external IDs are required");
}
Account.Id accountId = resource.getUser().getAccountId();
Map<ExternalId.Key, ExternalId> externalIdMap =
externalIds
.byAccount(dbProvider.get(), resource.getUser().getAccountId())
@@ -100,12 +91,14 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
}
}
if (!toDelete.isEmpty()) {
externalIdsUpdateFactory.create().delete(dbProvider.get(), toDelete);
accountCache.evict(accountId);
for (ExternalId e : toDelete) {
accountByEmailCache.evict(e.email());
try {
for (ExternalId extId : toDelete) {
AuthRequest authRequest = new AuthRequest(extId.key());
authRequest.setEmailAddress(extId.email());
accountManager.unlink(extId.accountId(), authRequest);
}
} catch (AccountException e) {
throw new ResourceConflictException(e.getMessage());
}
return Response.none();