Search in sources :

Example 1 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class ApprovalsUtil method checkApprovals.

private static void checkApprovals(Map<String, Short> approvals, ChangeControl changeCtl) throws AuthException {
    for (Map.Entry<String, Short> vote : approvals.entrySet()) {
        String name = vote.getKey();
        Short value = vote.getValue();
        PermissionRange range = changeCtl.getRange(Permission.forLabel(name));
        if (range == null || !range.contains(value)) {
            throw new AuthException(String.format("applying label \"%s\": %d is restricted", name, value));
        }
    }
}
Also used : PermissionRange(com.google.gerrit.common.data.PermissionRange) AuthException(com.google.gerrit.extensions.restapi.AuthException) Map(java.util.Map)

Example 2 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class PostPrivate method applyImpl.

@Override
public Response<String> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, SetPrivateOp.Input input) throws RestApiException, UpdateException {
    if (!canSetPrivate(rsrc)) {
        throw new AuthException("not allowed to mark private");
    }
    if (rsrc.getChange().isPrivate()) {
        return Response.ok("");
    }
    ChangeControl control = rsrc.getControl();
    SetPrivateOp op = new SetPrivateOp(cmUtil, true, input);
    try (BatchUpdate u = updateFactory.create(dbProvider.get(), control.getProject().getNameKey(), control.getUser(), TimeUtil.nowTs())) {
        u.addOp(control.getId(), op).execute();
    }
    return Response.created("");
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) AuthException(com.google.gerrit.extensions.restapi.AuthException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 3 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class DeletePrivate method applyImpl.

@Override
protected Response<String> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, SetPrivateOp.Input input) throws RestApiException, UpdateException {
    if (!canDeletePrivate(rsrc)) {
        throw new AuthException("not allowed to unmark private");
    }
    if (!rsrc.getChange().isPrivate()) {
        throw new ResourceConflictException("change is not private");
    }
    ChangeControl control = rsrc.getControl();
    SetPrivateOp op = new SetPrivateOp(cmUtil, false, input);
    try (BatchUpdate u = updateFactory.create(dbProvider.get(), control.getProject().getNameKey(), control.getUser(), TimeUtil.nowTs())) {
        u.addOp(control.getId(), op).execute();
    }
    return Response.none();
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ChangeControl(com.google.gerrit.server.project.ChangeControl) AuthException(com.google.gerrit.extensions.restapi.AuthException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 4 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class ListChangeDrafts method apply.

@Override
public Map<String, List<CommentInfo>> apply(ChangeResource rsrc) throws AuthException, OrmException {
    if (!rsrc.getControl().getUser().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    ChangeData cd = changeDataFactory.create(db.get(), rsrc.getControl());
    List<Comment> drafts = commentsUtil.draftByChangeAuthor(db.get(), cd.notes(), rsrc.getControl().getUser().getAccountId());
    return commentJson.get().setFillAccounts(false).setFillPatchSet(true).newCommentFormatter().format(drafts);
}
Also used : Comment(com.google.gerrit.reviewdb.client.Comment) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 5 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class PutDescription method apply.

@Override
public Response<String> apply(GroupResource resource, Input input) throws AuthException, MethodNotAllowedException, ResourceNotFoundException, OrmException, IOException {
    if (input == null) {
        // Delete would set description to null.
        input = new Input();
    }
    if (resource.toAccountGroup() == null) {
        throw new MethodNotAllowedException();
    } else if (!resource.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    AccountGroup group = db.get().accountGroups().get(resource.toAccountGroup().getId());
    if (group == null) {
        throw new ResourceNotFoundException();
    }
    group.setDescription(Strings.emptyToNull(input.description));
    db.get().accountGroups().update(Collections.singleton(group));
    groupCache.evict(group);
    return Strings.isNullOrEmpty(input.description) ? Response.<String>none() : Response.ok(input.description);
}
Also used : DefaultInput(com.google.gerrit.extensions.restapi.DefaultInput) Input(com.google.gerrit.server.group.PutDescription.Input) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

AuthException (com.google.gerrit.extensions.restapi.AuthException)68 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)22 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)20 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)15 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)14 Change (com.google.gerrit.reviewdb.client.Change)13 IOException (java.io.IOException)12 Account (com.google.gerrit.reviewdb.client.Account)11 Project (com.google.gerrit.reviewdb.client.Project)11 CurrentUser (com.google.gerrit.server.CurrentUser)11 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)11 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)11 ArrayList (java.util.ArrayList)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)8 ChangeControl (com.google.gerrit.server.project.ChangeControl)7 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)6 OrmException (com.google.gwtorm.server.OrmException)6 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)6