Search in sources :

Example 1 with ValidationFailure

use of org.haiku.haikudepotserver.api1.support.ValidationFailure 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 ValidationFailure

use of org.haiku.haikudepotserver.api1.support.ValidationFailure in project haikudepotserver by haiku.

the class RepositoryApiImpl method createRepositorySourceMirror.

@Override
public CreateRepositorySourceMirrorResult createRepositorySourceMirror(CreateRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositorySourceCode), "the code for the new repository source mirror");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.countryCode), "the country code should be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.baseUrl), "the base url should be supplied");
    final ObjectContext context = serverRuntime.newContext();
    Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
    RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.repositorySourceCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("the repository [" + repositorySource.getRepository() + "] is not able to be edited");
    }
    if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySource.getCode(), request.baseUrl).isPresent()) {
        LOGGER.info("attempt to add a repository source mirror for a url [{}] that is " + " already in use", request.baseUrl);
        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
    }
    // if there is no other mirror then this should be the primary.
    RepositorySourceMirror mirror = context.newObject(RepositorySourceMirror.class);
    mirror.setIsPrimary(repositorySource.tryGetPrimaryMirror().isEmpty());
    mirror.setBaseUrl(request.baseUrl);
    mirror.setRepositorySource(repositorySource);
    mirror.setCountry(country);
    mirror.setDescription(StringUtils.trimToNull(request.description));
    mirror.setCode(UUID.randomUUID().toString());
    repositorySource.getRepository().setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did add mirror [{}] to repository source [{}]", country.getCode(), repositorySource.getCode());
    CreateRepositorySourceMirrorResult result = new CreateRepositorySourceMirrorResult();
    result.code = mirror.getCode();
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 3 with ValidationFailure

use of org.haiku.haikudepotserver.api1.support.ValidationFailure in project haikudepotserver by haiku.

the class RepositoryApiImpl method createRepositorySource.

@Override
public CreateRepositorySourceResult createRepositorySource(CreateRepositorySourceRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the new repository source must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository for the new repository source must be identified");
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, request.repositoryCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    Optional<RepositorySource> existingRepositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
    if (existingRepositorySourceOptional.isPresent()) {
        throw new ValidationException(new ValidationFailure(RepositorySource.CODE.getName(), "unique"));
    }
    RepositorySource repositorySource = context.newObject(RepositorySource.class);
    repositorySource.setRepository(repository);
    repositorySource.setCode(request.code);
    repository.setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did create a new repository source '{}' on the repository '{}'", repositorySource, repository);
    return new CreateRepositorySourceResult();
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) CreateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 4 with ValidationFailure

use of org.haiku.haikudepotserver.api1.support.ValidationFailure in project haikudepotserver by haiku.

the class RepositoryApiImpl method createRepository.

@Override
public CreateRepositoryResult createRepository(CreateRepositoryRequest createRepositoryRequest) {
    Preconditions.checkNotNull(createRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_ADD)) {
        throw new AccessDeniedException("unable to add a repository");
    }
    if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
        throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
    }
    // check to see if there is an existing repository with the same code; non-unique.
    {
        Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);
        if (repositoryOptional.isPresent()) {
            throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
        }
    }
    Repository repository = context.newObject(Repository.class);
    repository.setCode(createRepositoryRequest.code);
    repository.setName(createRepositoryRequest.name);
    repository.setInformationUrl(createRepositoryRequest.informationUrl);
    context.commitChanges();
    return new CreateRepositoryResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) Optional(java.util.Optional) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 5 with ValidationFailure

use of org.haiku.haikudepotserver.api1.support.ValidationFailure in project haikudepotserver by haiku.

the class AuthorizationApiImpl method createAuthorizationPkgRule.

@Override
public CreateAuthorizationPkgRuleResult createAuthorizationPkgRule(CreateAuthorizationPkgRuleRequest request) throws AuthorizationRuleConflictException {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.permissionCode), "the permission code is required");
    Preconditions.checkState(org.haiku.haikudepotserver.security.model.Permission.valueOf(request.permissionCode.toUpperCase()).getRequiredTargetType() == TargetType.PKG, "the permission should have a target type of; " + TargetType.PKG);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.userNickname), "the user nickname must be supplied");
    ensureCanAuthorizationManipulate();
    ObjectContext context = serverRuntime.newContext();
    org.haiku.haikudepotserver.dataobjects.Permission permission = ensurePermission(context, request.permissionCode);
    User user = ensureUser(context, request.userNickname);
    if (user.getIsRoot()) {
        throw new ValidationException(new ValidationFailure("user", "root"));
    }
    Pkg pkg = null;
    if (null != request.pkgName) {
        pkg = ensurePkg(context, request.pkgName);
    }
    if (authorizationPkgRulesService.wouldConflict(context, user, permission, pkg)) {
        throw new AuthorizationRuleConflictException();
    }
    authorizationPkgRulesService.create(context, user, permission, pkg);
    context.commitChanges();
    return new CreateAuthorizationPkgRuleResult();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)6 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)6 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)6 AccessDeniedException (org.springframework.security.access.AccessDeniedException)5 Repository (org.haiku.haikudepotserver.dataobjects.Repository)3 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)3 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)2 Country (org.haiku.haikudepotserver.dataobjects.Country)2 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)2 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)2 Optional (java.util.Optional)1 CreateRepositoryResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult)1 CreateRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult)1 CreateRepositorySourceResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult)1 UpdateRepositoryRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest)1 UpdateRepositoryResult (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult)1 UpdateRepositorySourceMirrorRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest)1 UpdateRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorResult)1 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)1 User (org.haiku.haikudepotserver.dataobjects.User)1