use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.
the class CreateGroup method createGroup.
private InternalGroup createGroup(CreateGroupArgs createGroupArgs) throws ResourceConflictException, IOException, ConfigInvalidException {
String nameLower = createGroupArgs.getGroupName().toLowerCase(Locale.US);
for (String name : systemGroupBackend.getNames()) {
if (name.toLowerCase(Locale.US).equals(nameLower)) {
throw new ResourceConflictException("group '" + name + "' already exists");
}
}
for (String name : systemGroupBackend.getReservedNames()) {
if (name.toLowerCase(Locale.US).equals(nameLower)) {
throw new ResourceConflictException("group name '" + name + "' is reserved");
}
}
AccountGroup.Id groupId = AccountGroup.id(sequences.nextGroupId());
AccountGroup.UUID uuid = MoreObjects.firstNonNull(createGroupArgs.uuid, GroupUuid.make(createGroupArgs.getGroupName(), self.get().newCommitterIdent(TimeUtil.now(), serverTimeZone)));
InternalGroupCreation groupCreation = InternalGroupCreation.builder().setGroupUUID(uuid).setNameKey(createGroupArgs.getGroup()).setId(groupId).build();
GroupDelta.Builder groupDeltaBuilder = GroupDelta.builder().setVisibleToAll(createGroupArgs.visibleToAll);
if (createGroupArgs.ownerGroupUuid != null) {
Optional<InternalGroup> ownerGroup = groupCache.get(createGroupArgs.ownerGroupUuid);
ownerGroup.map(InternalGroup::getGroupUUID).ifPresent(groupDeltaBuilder::setOwnerGroupUUID);
}
if (createGroupArgs.groupDescription != null) {
groupDeltaBuilder.setDescription(createGroupArgs.groupDescription);
}
groupDeltaBuilder.setMemberModification(members -> ImmutableSet.copyOf(createGroupArgs.initialMembers));
try {
return groupsUpdateProvider.get().createGroup(groupCreation, groupDeltaBuilder.build());
} catch (DuplicateKeyException e) {
throw new ResourceConflictException("group '" + createGroupArgs.getGroupName() + "' already exists", e);
}
}
use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.
the class JdbcAccountPatchReviewStore method markReviewed.
@Override
public boolean markReviewed(PatchSet.Id psId, Account.Id accountId, String path) {
try (TraceTimer ignored = TraceContext.newTimer("Mark file as reviewed", Metadata.builder().patchSetId(psId.get()).accountId(accountId.get()).filePath(path).build());
Connection con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement("INSERT INTO account_patch_reviews " + "(account_id, change_id, patch_set_id, file_name) VALUES " + "(?, ?, ?, ?)")) {
stmt.setInt(1, accountId.get());
stmt.setInt(2, psId.changeId().get());
stmt.setInt(3, psId.get());
stmt.setString(4, path);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
StorageException ormException = convertError("insert", e);
if (ormException instanceof DuplicateKeyException) {
return false;
}
throw ormException;
}
}
use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.
the class PutUsername method apply.
@Override
public Response<String> apply(AccountResource rsrc, UsernameInput input) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
}
Account.Id accountId = rsrc.getUser().getAccountId();
if (!externalIds.byAccount(accountId, SCHEME_USERNAME).isEmpty()) {
throw new MethodNotAllowedException("Username cannot be changed.");
}
if (realm.accountBelongsToRealm(externalIds.byAccount(accountId)) && !realm.allowsEdit(AccountFieldName.USER_NAME)) {
throw new MethodNotAllowedException("realm does not allow editing username");
}
if (input == null || Strings.isNullOrEmpty(input.username)) {
throw new BadRequestException("input required");
}
if (!ExternalId.isValidUsername(input.username)) {
throw new UnprocessableEntityException("invalid username");
}
ExternalId.Key key = externalIdKeyFactory.create(SCHEME_USERNAME, input.username);
try {
accountsUpdateProvider.get().update("Set Username via API", accountId, u -> u.addExternalId(externalIdFactory.create(key, accountId, null, null)));
} catch (DuplicateKeyException dupeErr) {
// If we are using this identity, don't report the exception.
Optional<ExternalId> other = externalIds.get(key);
if (other.isPresent() && other.get().accountId().equals(accountId)) {
return Response.ok(input.username);
}
// Otherwise, someone else has this identity.
throw new ResourceConflictException("username already used", dupeErr);
}
sshKeyCache.evict(input.username);
return Response.ok(input.username);
}
Aggregations