Unset preferred email when corresponding external ID is deleted
Change-Id: I4971bdda4fafa7d526fa564694afffcaac82014d Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -16,6 +16,7 @@ package com.google.gerrit.acceptance.rest.account;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static com.google.gerrit.acceptance.GitUtil.fetch;
|
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.account.externalids.ExternalId.SCHEME_USERNAME;
|
||||||
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
|
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
|
||||||
import static org.junit.Assert.fail;
|
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.Retryer;
|
||||||
import com.github.rholder.retry.RetryerBuilder;
|
import com.github.rholder.retry.RetryerBuilder;
|
||||||
import com.github.rholder.retry.StopStrategies;
|
import com.github.rholder.retry.StopStrategies;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||||
import com.google.gerrit.acceptance.GerritConfig;
|
import com.google.gerrit.acceptance.GerritConfig;
|
||||||
import com.google.gerrit.acceptance.PushOneCommit;
|
import com.google.gerrit.acceptance.PushOneCommit;
|
||||||
@@ -127,6 +129,18 @@ public class ExternalIdIT extends AbstractDaemonTest {
|
|||||||
assertThat(results).containsExactlyElementsIn(expectedIds);
|
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
|
@Test
|
||||||
public void deleteExternalIds_Conflict() throws Exception {
|
public void deleteExternalIds_Conflict() throws Exception {
|
||||||
List<String> toDelete = new ArrayList<>();
|
List<String> toDelete = new ArrayList<>();
|
||||||
|
|||||||
@@ -24,12 +24,10 @@ import com.google.gerrit.extensions.restapi.Response;
|
|||||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||||
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
|
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.reviewdb.server.ReviewDb;
|
||||||
import com.google.gerrit.server.CurrentUser;
|
import com.google.gerrit.server.CurrentUser;
|
||||||
import com.google.gerrit.server.account.externalids.ExternalId;
|
import com.google.gerrit.server.account.externalids.ExternalId;
|
||||||
import com.google.gerrit.server.account.externalids.ExternalIds;
|
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.gwtorm.server.OrmException;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
@@ -40,25 +38,19 @@ import java.util.Map;
|
|||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
|
|
||||||
public class DeleteExternalIds implements RestModifyView<AccountResource, List<String>> {
|
public class DeleteExternalIds implements RestModifyView<AccountResource, List<String>> {
|
||||||
private final AccountByEmailCache accountByEmailCache;
|
private final AccountManager accountManager;
|
||||||
private final AccountCache accountCache;
|
|
||||||
private final ExternalIds externalIds;
|
private final ExternalIds externalIds;
|
||||||
private final ExternalIdsUpdate.User externalIdsUpdateFactory;
|
|
||||||
private final Provider<CurrentUser> self;
|
private final Provider<CurrentUser> self;
|
||||||
private final Provider<ReviewDb> dbProvider;
|
private final Provider<ReviewDb> dbProvider;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
DeleteExternalIds(
|
DeleteExternalIds(
|
||||||
AccountByEmailCache accountByEmailCache,
|
AccountManager accountManager,
|
||||||
AccountCache accountCache,
|
|
||||||
ExternalIds externalIds,
|
ExternalIds externalIds,
|
||||||
ExternalIdsUpdate.User externalIdsUpdateFactory,
|
|
||||||
Provider<CurrentUser> self,
|
Provider<CurrentUser> self,
|
||||||
Provider<ReviewDb> dbProvider) {
|
Provider<ReviewDb> dbProvider) {
|
||||||
this.accountByEmailCache = accountByEmailCache;
|
this.accountManager = accountManager;
|
||||||
this.accountCache = accountCache;
|
|
||||||
this.externalIds = externalIds;
|
this.externalIds = externalIds;
|
||||||
this.externalIdsUpdateFactory = externalIdsUpdateFactory;
|
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.dbProvider = dbProvider;
|
this.dbProvider = dbProvider;
|
||||||
}
|
}
|
||||||
@@ -74,7 +66,6 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
|
|||||||
throw new BadRequestException("external IDs are required");
|
throw new BadRequestException("external IDs are required");
|
||||||
}
|
}
|
||||||
|
|
||||||
Account.Id accountId = resource.getUser().getAccountId();
|
|
||||||
Map<ExternalId.Key, ExternalId> externalIdMap =
|
Map<ExternalId.Key, ExternalId> externalIdMap =
|
||||||
externalIds
|
externalIds
|
||||||
.byAccount(dbProvider.get(), resource.getUser().getAccountId())
|
.byAccount(dbProvider.get(), resource.getUser().getAccountId())
|
||||||
@@ -100,12 +91,14 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!toDelete.isEmpty()) {
|
try {
|
||||||
externalIdsUpdateFactory.create().delete(dbProvider.get(), toDelete);
|
for (ExternalId extId : toDelete) {
|
||||||
accountCache.evict(accountId);
|
AuthRequest authRequest = new AuthRequest(extId.key());
|
||||||
for (ExternalId e : toDelete) {
|
authRequest.setEmailAddress(extId.email());
|
||||||
accountByEmailCache.evict(e.email());
|
accountManager.unlink(extId.accountId(), authRequest);
|
||||||
}
|
}
|
||||||
|
} catch (AccountException e) {
|
||||||
|
throw new ResourceConflictException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.none();
|
return Response.none();
|
||||||
|
|||||||
Reference in New Issue
Block a user