Search in sources :

Example 6 with DuplicateKeyException

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);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) IdString(com.google.gerrit.extensions.restapi.IdString) InternalGroupCreation(com.google.gerrit.server.group.db.InternalGroupCreation) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException) InternalGroup(com.google.gerrit.entities.InternalGroup)

Example 7 with DuplicateKeyException

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;
    }
}
Also used : SQLException(java.sql.SQLException) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StorageException(com.google.gerrit.exceptions.StorageException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Example 8 with DuplicateKeyException

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);
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Aggregations

DuplicateKeyException (com.google.gerrit.exceptions.DuplicateKeyException)8 AccountGroup (com.google.gerrit.entities.AccountGroup)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 StorageException (com.google.gerrit.exceptions.StorageException)2 GroupDelta (com.google.gerrit.server.group.db.GroupDelta)2 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 Account (com.google.gerrit.entities.Account)1 InternalGroup (com.google.gerrit.entities.InternalGroup)1 NoSuchGroupException (com.google.gerrit.exceptions.NoSuchGroupException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 InternalGroupCreation (com.google.gerrit.server.group.db.InternalGroupCreation)1