Search in sources :

Example 66 with IdentifiedUser

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

the class Submit method apply.

@Override
public Response<ChangeInfo> apply(RevisionResource rsrc, SubmitInput input) throws RestApiException, RepositoryNotFoundException, IOException, PermissionBackendException, UpdateException, ConfigInvalidException {
    input.onBehalfOf = Strings.emptyToNull(input.onBehalfOf);
    IdentifiedUser submitter;
    if (input.onBehalfOf != null) {
        submitter = onBehalfOf(rsrc, input);
    } else {
        rsrc.permissions().check(ChangePermission.SUBMIT);
        submitter = rsrc.getUser().asIdentifiedUser();
    }
    projectCache.get(rsrc.getProject()).orElseThrow(illegalState(rsrc.getProject())).checkStatePermitsWrite();
    return Response.ok(json.noOptions().format(mergeChange(rsrc, submitter, input)));
}
Also used : IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 67 with IdentifiedUser

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

the class PutDescription method apply.

@Override
public Response<String> apply(ProjectResource resource, DescriptionInput input) throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException, PermissionBackendException {
    if (input == null) {
        // Delete would set description to null.
        input = new DescriptionInput();
    }
    IdentifiedUser user = resource.getUser().asIdentifiedUser();
    permissionBackend.user(user).project(resource.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    try (MetaDataUpdate md = updateFactory.get().create(resource.getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        String desc = input.description;
        config.updateProject(p -> p.setDescription(Strings.emptyToNull(desc)));
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), "Update description\n");
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evictAndReindex(resource.getProjectState().getProject());
        md.getRepository().setGitwebDescription(config.getProject().getDescription());
        return Strings.isNullOrEmpty(config.getProject().getDescription()) ? Response.none() : Response.ok(config.getProject().getDescription());
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(resource.getName(), notFound);
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) DescriptionInput(com.google.gerrit.extensions.api.projects.DescriptionInput) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 68 with IdentifiedUser

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

the class SetParent method apply.

public String apply(ProjectResource rsrc, ParentInput input, boolean checkIfAdmin) throws AuthException, ResourceConflictException, ResourceNotFoundException, UnprocessableEntityException, IOException, PermissionBackendException, BadRequestException {
    IdentifiedUser user = rsrc.getUser().asIdentifiedUser();
    String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
    validateParentUpdate(rsrc.getProjectState().getNameKey(), user, parentName, checkIfAdmin);
    try (MetaDataUpdate md = updateFactory.get().create(rsrc.getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setParent(parentName));
        String msg = Strings.emptyToNull(input.commitMessage);
        if (msg == null) {
            msg = String.format("Changed parent to %s.\n", parentName);
        } else if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evictAndReindex(rsrc.getProjectState().getProject());
        Project.NameKey parent = config.getProject().getParent(allProjects);
        requireNonNull(parent);
        return parent.get();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(rsrc.getName(), notFound);
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 69 with IdentifiedUser

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

the class InMemoryTestEnvironment method setApiUser.

public void setApiUser(Account.Id id) {
    IdentifiedUser user = userFactory.create(id);
    requestContext.setContext(() -> user);
}
Also used : IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 70 with IdentifiedUser

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

the class GerritPublicKeyChecker method checkIdsForArbitraryUser.

private CheckResult checkIdsForArbitraryUser(PGPPublicKey key) throws PGPException {
    List<AccountState> accountStates = accountQueryProvider.get().byExternalId(toExtIdKey(key));
    if (accountStates.isEmpty()) {
        return CheckResult.bad("Key is not associated with any users");
    }
    if (accountStates.size() > 1) {
        return CheckResult.bad("Key is associated with multiple users");
    }
    IdentifiedUser user = userFactory.create(accountStates.get(0));
    Set<String> allowedUserIds = getAllowedUserIds(user);
    if (allowedUserIds.isEmpty()) {
        return CheckResult.bad("No identities found for user");
    }
    if (hasAllowedUserId(key, allowedUserIds)) {
        return CheckResult.trusted();
    }
    return CheckResult.bad("Key does not contain any valid certifications for user's identities");
}
Also used : AccountState(com.google.gerrit.server.account.AccountState) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

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