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();
}
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();
}
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]+$");
}
Aggregations