Search in sources :

Example 1 with UpdateRepositoryRequest

use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest in project haikudepotserver by haiku.

the class RepositoryApiImpl method updateRepository.

@Override
public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRepositoryRequest) {
    Preconditions.checkNotNull(updateRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, updateRepositoryRequest.code);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
        switch(filter) {
            case ACTIVE:
                if (null == updateRepositoryRequest.active) {
                    throw new IllegalStateException("the active flag must be supplied");
                }
                if (repository.getActive() != updateRepositoryRequest.active) {
                    repository.setActive(updateRepositoryRequest.active);
                    LOGGER.info("did set the active flag on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.active);
                }
                if (!updateRepositoryRequest.active) {
                    for (RepositorySource repositorySource : repository.getRepositorySources()) {
                        if (repositorySource.getActive()) {
                            repositorySource.setActive(false);
                            LOGGER.info("did set the active flag on the repository source {} to false", repositorySource.getCode());
                        }
                    }
                }
                break;
            case NAME:
                if (null == updateRepositoryRequest.name) {
                    throw new IllegalStateException("the name must be supplied to update the repository");
                }
                String name = updateRepositoryRequest.name.trim();
                if (0 == name.length()) {
                    throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
                }
                repository.setName(name);
                break;
            case INFORMATIONURL:
                repository.setInformationUrl(updateRepositoryRequest.informationUrl);
                LOGGER.info("did set the information url on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.informationUrl);
                break;
            case PASSWORD:
                if (StringUtils.isBlank(updateRepositoryRequest.passwordClear)) {
                    repository.setPasswordSalt(null);
                    repository.setPasswordHash(null);
                    LOGGER.info("cleared the password for repository [{}]", repository);
                } else {
                    repositoryService.setPassword(repository, updateRepositoryRequest.passwordClear);
                    LOGGER.info("did update the repository [{}] password", repository);
                }
                break;
            default:
                throw new IllegalStateException("unhandled filter for updating a repository");
        }
    }
    if (context.hasChanges()) {
        context.commitChanges();
    } else {
        LOGGER.info("update repository {} with no changes made", updateRepositoryRequest.code);
    }
    return new UpdateRepositoryResult();
}
Also used : UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UpdateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 2 with UpdateRepositoryRequest

use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest in project haikudepotserver by haiku.

the class RepositoryApiIT method testUpdateRepository.

@Test
public void testUpdateRepository() {
    IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    UpdateRepositoryRequest request = new UpdateRepositoryRequest();
    request.active = false;
    request.code = data.repository.getCode();
    request.filter = Collections.singletonList(UpdateRepositoryRequest.Filter.ACTIVE);
    // ------------------------------------
    repositoryApi.updateRepository(request);
    // ------------------------------------
    ObjectContext context = serverRuntime.newContext();
    Repository repository = ObjectSelect.query(Repository.class).where(Repository.CODE.eq(data.repository.getCode())).selectOne(context);
    Assertions.assertThat(repository.getActive()).isFalse();
}
Also used : UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ObjectContext(org.apache.cayenne.ObjectContext) IntegrationTestSupportService(org.haiku.haikudepotserver.IntegrationTestSupportService) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with UpdateRepositoryRequest

use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest in project haikudepotserver by haiku.

the class RepositoryApiIT method testUpdateRepository_password.

@Test
public void testUpdateRepository_password() {
    IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
    Assertions.assertThat(data.repository.getPasswordHash()).isNull();
    setAuthenticatedUserToRoot();
    UpdateRepositoryRequest request = new UpdateRepositoryRequest();
    request.code = "testrepo";
    request.active = false;
    request.passwordClear = "Quatsch";
    request.filter = Collections.singletonList(UpdateRepositoryRequest.Filter.PASSWORD);
    // ------------------------------------
    repositoryApi.updateRepository(request);
    // ------------------------------------
    ObjectContext context = serverRuntime.newContext();
    Repository repository = ObjectSelect.query(Repository.class).where(Repository.CODE.eq(data.repository.getCode())).selectOne(context);
    Assertions.assertThat(repository.getPasswordHash()).matches("^[A-Za-z0-9]+$");
}
Also used : UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ObjectContext(org.apache.cayenne.ObjectContext) IntegrationTestSupportService(org.haiku.haikudepotserver.IntegrationTestSupportService) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)3 UpdateRepositoryRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest)3 Repository (org.haiku.haikudepotserver.dataobjects.Repository)3 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)2 IntegrationTestSupportService (org.haiku.haikudepotserver.IntegrationTestSupportService)2 Test (org.junit.jupiter.api.Test)2 UpdateRepositoryResult (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult)1 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)1 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)1 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1