Search in sources :

Example 36 with UnprocessableEntityException

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

the class PostReviewers method addByAccountId.

@Nullable
private Addition addByAccountId(String reviewer, ChangeResource rsrc, ReviewerState state, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify, boolean allowGroup, boolean allowByEmail) throws OrmException, PermissionBackendException {
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(reviewer).getAccountId();
    } catch (UnprocessableEntityException | AuthException e) {
        // AuthException won't occur since the user is authenticated at this point.
        if (!allowGroup && !allowByEmail) {
            // Only return failure if we aren't going to try other interpretations.
            return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUser, reviewer));
        }
        return null;
    }
    ReviewerResource rrsrc = reviewerFactory.create(rsrc, accountId);
    Account member = rrsrc.getReviewerUser().getAccount();
    PermissionBackend.ForRef perm = permissionBackend.user(rrsrc.getReviewerUser()).ref(rrsrc.getChange().getDest());
    if (isValidReviewer(member, perm)) {
        return new Addition(reviewer, rsrc, ImmutableSet.of(member.getId()), null, state, notify, accountsToNotify);
    }
    if (!member.isActive()) {
        if (allowByEmail && state == CC) {
            return null;
        }
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerInactive, reviewer));
    }
    return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerCantSeeChange, reviewer));
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) Nullable(com.google.gerrit.common.Nullable)

Example 37 with UnprocessableEntityException

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

the class PutAssignee method applyImpl.

@Override
protected AccountInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, AssigneeInput input) throws RestApiException, UpdateException, OrmException, IOException, PermissionBackendException {
    rsrc.permissions().check(ChangePermission.EDIT_ASSIGNEE);
    input.assignee = Strings.nullToEmpty(input.assignee).trim();
    if (input.assignee.isEmpty()) {
        throw new BadRequestException("missing assignee field");
    }
    IdentifiedUser assignee = accounts.parse(input.assignee);
    if (!assignee.getAccount().isActive()) {
        throw new UnprocessableEntityException(input.assignee + " is not active");
    }
    try {
        rsrc.permissions().database(db).user(assignee).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new AuthException("read not permitted for " + input.assignee);
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getControl().getUser(), TimeUtil.nowTs())) {
        SetAssigneeOp op = assigneeFactory.create(assignee);
        bu.addOp(rsrc.getId(), op);
        PostReviewers.Addition reviewersAddition = addAssigneeAsCC(rsrc, input.assignee);
        bu.addOp(rsrc.getId(), reviewersAddition.op);
        bu.execute();
        return accountLoaderFactory.create(true).fillOne(assignee.getAccountId());
    }
}
Also used : Addition(com.google.gerrit.server.change.PostReviewers.Addition) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 38 with UnprocessableEntityException

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

the class BanCommit method apply.

@Override
public BanResultInfo apply(ProjectResource rsrc, Input input) throws UnprocessableEntityException, AuthException, ResourceConflictException, IOException {
    BanResultInfo r = new BanResultInfo();
    if (input != null && input.commits != null && !input.commits.isEmpty()) {
        List<ObjectId> commitsToBan = new ArrayList<>(input.commits.size());
        for (String c : input.commits) {
            try {
                commitsToBan.add(ObjectId.fromString(c));
            } catch (IllegalArgumentException e) {
                throw new UnprocessableEntityException(e.getMessage());
            }
        }
        try {
            BanCommitResult result = banCommit.ban(rsrc.getControl(), commitsToBan, input.reason);
            r.newlyBanned = transformCommits(result.getNewlyBannedCommits());
            r.alreadyBanned = transformCommits(result.getAlreadyBannedCommits());
            r.ignored = transformCommits(result.getIgnoredObjectIds());
        } catch (PermissionDeniedException e) {
            throw new AuthException(e.getMessage());
        } catch (ConcurrentRefUpdateException e) {
            throw new ResourceConflictException(e.getMessage(), e);
        }
    }
    return r;
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionDeniedException(com.google.gerrit.common.errors.PermissionDeniedException) BanCommitResult(com.google.gerrit.server.git.BanCommitResult) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)

Example 39 with UnprocessableEntityException

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

the class SetAccess method getAccessSections.

private List<AccessSection> getAccessSections(Map<String, AccessSectionInfo> sectionInfos) throws UnprocessableEntityException {
    if (sectionInfos == null) {
        return Collections.emptyList();
    }
    List<AccessSection> sections = new ArrayList<>(sectionInfos.size());
    for (Map.Entry<String, AccessSectionInfo> entry : sectionInfos.entrySet()) {
        AccessSection accessSection = new AccessSection(entry.getKey());
        if (entry.getValue().permissions == null) {
            continue;
        }
        for (Map.Entry<String, PermissionInfo> permissionEntry : entry.getValue().permissions.entrySet()) {
            Permission p = new Permission(permissionEntry.getKey());
            if (permissionEntry.getValue().exclusive != null) {
                p.setExclusiveGroup(permissionEntry.getValue().exclusive);
            }
            if (permissionEntry.getValue().rules == null) {
                continue;
            }
            for (Map.Entry<String, PermissionRuleInfo> permissionRuleInfoEntry : permissionEntry.getValue().rules.entrySet()) {
                PermissionRuleInfo pri = permissionRuleInfoEntry.getValue();
                GroupDescription.Basic group = groupsCollection.parseId(permissionRuleInfoEntry.getKey());
                if (group == null) {
                    throw new UnprocessableEntityException(permissionRuleInfoEntry.getKey() + " is not a valid group ID");
                }
                PermissionRule r = new PermissionRule(GroupReference.forGroup(group));
                if (pri != null) {
                    if (pri.max != null) {
                        r.setMax(pri.max);
                    }
                    if (pri.min != null) {
                        r.setMin(pri.min);
                    }
                    r.setAction(GetAccess.ACTION_TYPE.inverse().get(pri.action));
                    if (pri.force != null) {
                        r.setForce(pri.force);
                    }
                }
                p.add(r);
            }
            accessSection.getPermissions().add(p);
        }
        sections.add(accessSection);
    }
    return sections;
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionRule(com.google.gerrit.common.data.PermissionRule) ArrayList(java.util.ArrayList) AccessSection(com.google.gerrit.common.data.AccessSection) GroupDescription(com.google.gerrit.common.data.GroupDescription) PermissionInfo(com.google.gerrit.extensions.api.access.PermissionInfo) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) Permission(com.google.gerrit.common.data.Permission) PermissionRuleInfo(com.google.gerrit.extensions.api.access.PermissionRuleInfo) AccessSectionInfo(com.google.gerrit.extensions.api.access.AccessSectionInfo) Map(java.util.Map)

Example 40 with UnprocessableEntityException

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

the class PostReview method onBehalfOf.

private RevisionResource onBehalfOf(RevisionResource rev, ReviewInput in) throws BadRequestException, AuthException, UnprocessableEntityException, OrmException, PermissionBackendException {
    if (in.labels == null || in.labels.isEmpty()) {
        throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
    }
    if (in.drafts == null) {
        in.drafts = DraftHandling.KEEP;
    }
    if (in.drafts != DraftHandling.KEEP) {
        throw new AuthException("not allowed to modify other user's drafts");
    }
    CurrentUser caller = rev.getUser();
    PermissionBackend.ForChange perm = rev.permissions().database(db);
    LabelTypes labelTypes = rev.getControl().getLabelTypes();
    Iterator<Map.Entry<String, Short>> itr = in.labels.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Short> ent = itr.next();
        LabelType type = labelTypes.byLabel(ent.getKey());
        if (type == null && in.strictLabels) {
            throw new BadRequestException(String.format("label \"%s\" is not a configured label", ent.getKey()));
        } else if (type == null) {
            itr.remove();
            continue;
        }
        if (!caller.isInternalUser()) {
            try {
                perm.check(new LabelPermission.WithValue(ON_BEHALF_OF, type, ent.getValue()));
            } catch (AuthException e) {
                throw new AuthException(String.format("not permitted to modify label \"%s\" on behalf of \"%s\"", type.getName(), in.onBehalfOf));
            }
        }
    }
    if (in.labels.isEmpty()) {
        throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
    }
    IdentifiedUser reviewer = accounts.parseOnBehalfOf(caller, in.onBehalfOf);
    try {
        perm.user(reviewer).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new UnprocessableEntityException(String.format("on_behalf_of account %s cannot see change", reviewer.getAccountId()));
    }
    ChangeControl ctl = rev.getControl().forUser(reviewer);
    return new RevisionResource(changes.parse(ctl), rev.getPatchSet());
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) LabelTypes(com.google.gerrit.common.data.LabelTypes) CurrentUser(com.google.gerrit.server.CurrentUser) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ChangeControl(com.google.gerrit.server.project.ChangeControl) LabelType(com.google.gerrit.common.data.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) HashMap(java.util.HashMap) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Aggregations

UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)78 AuthException (com.google.gerrit.extensions.restapi.AuthException)31 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)27 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)25 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)23 Test (org.junit.Test)23 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 Account (com.google.gerrit.entities.Account)12 Ref (org.eclipse.jgit.lib.Ref)10 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)9 CurrentUser (com.google.gerrit.server.CurrentUser)9 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)9 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)9 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)9 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)8 Map (java.util.Map)8 ObjectId (org.eclipse.jgit.lib.ObjectId)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Nullable (com.google.gerrit.common.Nullable)7