Search in sources :

Example 81 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class BaseCommand method handleError.

private int handleError(Throwable e) {
    if ((e.getClass() == IOException.class && "Pipe closed".equals(e.getMessage())) || // 
    (e.getClass() == SshException.class && "Already closed".equals(e.getMessage())) || // 
    e.getClass() == InterruptedIOException.class) {
        // 
        return 127;
    }
    if (!(e instanceof UnloggedFailure)) {
        final StringBuilder m = new StringBuilder();
        m.append("Internal server error");
        if (user.isIdentifiedUser()) {
            final IdentifiedUser u = user.asIdentifiedUser();
            m.append(" (user ");
            m.append(u.getUserName().orElse(null));
            m.append(" account ");
            m.append(u.getAccountId());
            m.append(")");
        }
        m.append(" during ");
        m.append(context.getCommandLine());
        logCauseIfRelevant(e, m);
    }
    if (e instanceof Failure) {
        final Failure f = (Failure) e;
        try {
            err.write((f.getMessage() + "\n").getBytes(ENC));
            err.flush();
        } catch (IOException e2) {
        // Ignored
        } catch (RuntimeException e2) {
            logger.atWarning().withCause(e2).log("Cannot send failure message to client");
        }
        return f.exitCode;
    }
    try {
        err.write("fatal: internal server error\n".getBytes(ENC));
        err.flush();
    } catch (IOException e2) {
    // Ignored
    } catch (RuntimeException e2) {
        logger.atWarning().withCause(e2).log("Cannot send internal server error message to client");
    }
    return 128;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 82 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class EqualsLabelPredicate method match.

protected boolean match(ChangeData cd, short value, Account.Id approver) {
    if (value != expVal) {
        return false;
    }
    if (account != null) {
        // case when account in query is numeric
        if (!account.equals(approver) && !isMagicUser()) {
            return false;
        }
        // case when account in query = owner
        if (account.equals(ChangeQueryBuilder.OWNER_ACCOUNT_ID) && !cd.change().getOwner().equals(approver)) {
            return false;
        }
        // case when account in query = non_uploader
        if (account.equals(ChangeQueryBuilder.NON_UPLOADER_ACCOUNT_ID) && cd.currentPatchSet().uploader().equals(approver)) {
            return false;
        }
    }
    IdentifiedUser reviewer = userFactory.create(approver);
    if (group != null && !reviewer.getEffectiveGroups().contains(group)) {
        return false;
    }
    // Check the user has 'READ' permission.
    try {
        PermissionBackend.ForChange perm = permissionBackend.absentUser(approver).change(cd);
        if (!projectCache.get(cd.project()).map(ProjectState::statePermitsRead).orElse(false)) {
            return false;
        }
        perm.check(ChangePermission.READ);
        return true;
    } catch (PermissionBackendException | AuthException e) {
        return false;
    }
}
Also used : PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 83 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class DeleteSshKey method apply.

@Override
public Response<?> apply(AccountResource.SshKey rsrc, Input input) throws AuthException, RepositoryNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
    if (!self.get().hasSameAccountId(rsrc.getUser())) {
        permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
    }
    IdentifiedUser user = rsrc.getUser();
    authorizedKeys.deleteKey(user.getAccountId(), rsrc.getSshKey().seq());
    try {
        deleteKeySenderFactory.create(user, rsrc.getSshKey()).send();
    } catch (EmailException e) {
        logger.atSevere().withCause(e).log("Cannot send SSH key deletion message to %s", user.getAccount().preferredEmail());
    }
    user.getUserName().ifPresent(sshKeyCache::evict);
    return Response.none();
}
Also used : EmailException(com.google.gerrit.exceptions.EmailException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 84 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class UploaderinPredicate method match.

@Override
public boolean match(ChangeData cd) {
    PatchSet latestPatchSet = cd.currentPatchSet();
    if (latestPatchSet == null) {
        return false;
    }
    IdentifiedUser uploader = userFactory.create(latestPatchSet.uploader());
    return uploader.getEffectiveGroups().contains(uuid);
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 85 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class PutAssignee method apply.

@Override
public Response<AccountInfo> apply(ChangeResource rsrc, AssigneeInput input) throws RestApiException, UpdateException, IOException, PermissionBackendException, ConfigInvalidException {
    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 = accountResolver.resolve(input.assignee).asUniqueUser();
    try {
        permissionBackend.absentUser(assignee.getAccountId()).change(rsrc.getNotes()).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new AuthException("read not permitted for " + input.assignee, e);
    }
    try (BatchUpdate bu = updateFactory.create(rsrc.getChange().getProject(), rsrc.getUser(), TimeUtil.now())) {
        SetAssigneeOp op = assigneeFactory.create(assignee);
        bu.addOp(rsrc.getId(), op);
        ReviewerSet currentReviewers = approvalsUtil.getReviewers(rsrc.getNotes());
        if (!currentReviewers.all().contains(assignee.getAccountId())) {
            ReviewerModification reviewersAddition = addAssigneeAsCC(rsrc, input.assignee);
            reviewersAddition.op.suppressEmail();
            bu.addOp(rsrc.getId(), reviewersAddition.op);
        }
        bu.execute();
        return Response.ok(accountLoaderFactory.create(true).fillOne(assignee.getAccountId()));
    }
}
Also used : ReviewerSet(com.google.gerrit.server.ReviewerSet) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ReviewerModification(com.google.gerrit.server.change.ReviewerModifier.ReviewerModification) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) SetAssigneeOp(com.google.gerrit.server.change.SetAssigneeOp)

Aggregations

IdentifiedUser (com.google.gerrit.server.IdentifiedUser)89 AuthException (com.google.gerrit.extensions.restapi.AuthException)27 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)19 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)15 CurrentUser (com.google.gerrit.server.CurrentUser)13 IOException (java.io.IOException)13 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)13 Project (com.google.gerrit.entities.Project)12 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)12 Inject (com.google.inject.Inject)12 Singleton (com.google.inject.Singleton)12 ArrayList (java.util.ArrayList)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Provider (com.google.inject.Provider)11 Change (com.google.gerrit.entities.Change)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 Repository (org.eclipse.jgit.lib.Repository)10 Account (com.google.gerrit.entities.Account)9 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)9