use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class DeleteEmail method apply.
public Response<?> apply(IdentifiedUser user, String email) throws ResourceNotFoundException, ResourceConflictException, MethodNotAllowedException, IOException, ConfigInvalidException {
Account.Id accountId = user.getAccountId();
if (realm.accountBelongsToRealm(externalIds.byAccount(accountId)) && !realm.allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL)) {
throw new MethodNotAllowedException("realm does not allow deleting emails");
}
Set<ExternalId> extIds = externalIds.byAccount(accountId).stream().filter(e -> email.equals(e.email())).collect(toSet());
if (extIds.isEmpty()) {
throw new ResourceNotFoundException(email);
}
if (realm.accountBelongsToRealm(extIds)) {
String errorMsg = String.format("Cannot remove e-mail '%s' which is directly associated with %s authentication", email, authType);
throw new ResourceConflictException(errorMsg);
}
try {
accountManager.unlink(user.getAccountId(), extIds.stream().map(ExternalId::key).collect(toSet()));
} catch (AccountException e) {
throw new ResourceConflictException(e.getMessage());
}
return Response.none();
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class PutPreferred method apply.
public Response<String> apply(IdentifiedUser user, String preferredEmail) throws RestApiException, IOException, ConfigInvalidException {
AtomicReference<Optional<RestApiException>> exception = new AtomicReference<>(Optional.empty());
AtomicBoolean alreadyPreferred = new AtomicBoolean(false);
Optional<AccountState> updatedAccount = accountsUpdateProvider.get().update("Set Preferred Email via API", user.getAccountId(), (a, u) -> {
if (preferredEmail.equals(a.account().preferredEmail())) {
alreadyPreferred.set(true);
} else {
// check if the user has a matching email
String matchingEmail = null;
for (String email : a.externalIds().stream().map(ExternalId::email).filter(Objects::nonNull).collect(toSet())) {
if (email.equals(preferredEmail)) {
// we have an email that matches exactly, prefer this one
matchingEmail = email;
break;
} else if (matchingEmail == null && email.equalsIgnoreCase(preferredEmail)) {
// we found an email that matches but has a different case
matchingEmail = email;
}
}
if (matchingEmail == null) {
// user doesn't have an external ID for this email
if (user.hasEmailAddress(preferredEmail)) {
// but Realm says the user is allowed to use this email
Set<ExternalId> existingExtIdsWithThisEmail = externalIds.byEmail(preferredEmail);
if (!existingExtIdsWithThisEmail.isEmpty()) {
// but the email is already assigned to another account
logger.atWarning().log("Cannot set preferred email %s for account %s because it is owned" + " by the following account(s): %s", preferredEmail, user.getAccountId(), existingExtIdsWithThisEmail.stream().map(ExternalId::accountId).collect(toList()));
exception.set(Optional.of(new ResourceConflictException("email in use by another account")));
return;
}
// claim the email now
u.addExternalId(externalIdFactory.createEmail(a.account().id(), preferredEmail));
matchingEmail = preferredEmail;
} else {
// Realm says that the email doesn't belong to the user. This can only
// happen as
// a race condition because EmailsCollection would have thrown
// ResourceNotFoundException already before invoking this REST endpoint.
exception.set(Optional.of(new ResourceNotFoundException(preferredEmail)));
return;
}
}
u.setPreferredEmail(matchingEmail);
}
});
if (!updatedAccount.isPresent()) {
throw new ResourceNotFoundException("account not found");
}
if (exception.get().isPresent()) {
throw exception.get().get();
}
return alreadyPreferred.get() ? Response.ok() : Response.created();
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class AccountIT method cannotAddEmailAddressUsedByAnotherAccount.
@Test
public void cannotAddEmailAddressUsedByAnotherAccount() throws Exception {
String email = "new.email@example.com";
EmailInput input = newEmailInput(email);
gApi.accounts().self().addEmail(input);
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(user.username()).addEmail(input));
assertThat(thrown).hasMessageThat().contains("Identity 'mailto:" + email + "' in use by another account");
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class AccountIT method addOtherUsersGpgKey_Conflict.
@Test
public void addOtherUsersGpgKey_Conflict() throws Exception {
AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
// Both users have a matching external ID for this key.
addExternalIdEmail(admin, "test5@example.com");
accountIndexedCounter.clear();
accountsUpdateProvider.get().update("Add External ID", user.id(), u -> u.addExternalId(externalIdFactory.create("foo", "myId", user.id())));
accountIndexedCounter.assertReindexOf(user);
TestKey key = validKeyWithSecondUserId();
addGpgKey(key.getPublicKeyArmored());
requestScopeOperations.setApiUser(user.id());
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> addGpgKey(user, key.getPublicKeyArmored()));
assertThat(thrown).hasMessageThat().contains("GPG key already associated with another account");
}
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class AccountIT method deactivateNotActive.
@Test
public void deactivateNotActive() throws Exception {
int id = gApi.accounts().id("user").get()._accountId;
assertThat(gApi.accounts().id("user").getActive()).isTrue();
gApi.accounts().id("user").setActive(false);
assertThat(gApi.accounts().id(id).getActive()).isFalse();
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(id).setActive(false));
assertThat(thrown).hasMessageThat().isEqualTo("account not active");
gApi.accounts().id(id).setActive(true);
}
Aggregations