Search in sources :

Example 6 with ValidationException

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

Example 7 with ValidationException

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

the class RepositoryApiImpl method updateRepositorySourceMirror.

@Override
public UpdateRepositorySourceMirrorResult updateRepositorySourceMirror(UpdateRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the mirror to update");
    final ObjectContext context = serverRuntime.newContext();
    RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySourceMirror.getRepositorySource().getRepository(), Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("the repository [" + repositorySourceMirror.getRepositorySource().getRepository() + "] is unable to be edited");
    }
    for (UpdateRepositorySourceMirrorRequest.Filter filter : CollectionUtils.emptyIfNull(request.filter)) {
        switch(filter) {
            case ACTIVE:
                if (repositorySourceMirror.getIsPrimary()) {
                    throw new ValidationException(new ValidationFailure(RepositorySourceMirror.ACTIVE.getName(), "confict"));
                }
                repositorySourceMirror.setActive(null != request.active && request.active);
                break;
            case BASE_URL:
                if (StringUtils.isBlank(request.baseUrl)) {
                    throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "required"));
                }
                if (!repositorySourceMirror.getBaseUrl().equals(request.baseUrl)) {
                    if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySourceMirror.getRepositorySource().getCode(), request.baseUrl).isPresent()) {
                        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
                    }
                    repositorySourceMirror.setBaseUrl(request.baseUrl);
                }
                break;
            case COUNTRY:
                Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
                repositorySourceMirror.setCountry(country);
                break;
            case IS_PRIMARY:
                boolean isPrimary = null != request.isPrimary && request.isPrimary;
                if (isPrimary != repositorySourceMirror.getIsPrimary()) {
                    if (isPrimary) {
                        // in this case, the former primary should loose it's primary
                        // status so that it can be swapped to this one.
                        repositorySourceMirror.getRepositorySource().getPrimaryMirror().setIsPrimary(false);
                        repositorySourceMirror.setIsPrimary(true);
                    } else {
                        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.IS_PRIMARY.getName(), "confict"));
                    }
                }
                break;
            case DESCRIPTION:
                repositorySourceMirror.setDescription(StringUtils.trimToNull(request.description));
                break;
            default:
                throw new IllegalStateException("unknown change filter for mirror [" + filter + "]");
        }
    }
    if (context.hasChanges()) {
        repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
    }
    context.commitChanges();
    LOGGER.info("did update mirror [{}]", repositorySourceMirror.getCode());
    return new UpdateRepositorySourceMirrorResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) UpdateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest) UpdateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorResult) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Aggregations

ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)7 ObjectContext (org.apache.cayenne.ObjectContext)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 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)1 IntegrationTestSupportService (org.haiku.haikudepotserver.IntegrationTestSupportService)1 CreateRepositoryRequest (org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryRequest)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