Search in sources :

Example 1 with QueueUserRatingSpreadsheetJobResult

use of org.haiku.haikudepotserver.api1.model.userrating.job.QueueUserRatingSpreadsheetJobResult in project haikudepotserver by haiku.

the class UserRatingJobApiImpl method queueUserRatingSpreadsheetJob.

@Override
public QueueUserRatingSpreadsheetJobResult queueUserRatingSpreadsheetJob(QueueUserRatingSpreadsheetJobRequest request) {
    Preconditions.checkArgument(null != request);
    Preconditions.checkArgument(Strings.isNullOrEmpty(request.pkgName) || Strings.isNullOrEmpty(request.userNickname), "the user nickname or pkg name can be supplied, but not both");
    final ObjectContext context = serverRuntime.newContext();
    User user = obtainAuthenticatedUser(context);
    UserRatingSpreadsheetJobSpecification spec = new UserRatingSpreadsheetJobSpecification();
    if (!Strings.isNullOrEmpty(request.repositoryCode)) {
        spec.setRepositoryCode(getRepository(context, request.repositoryCode).getCode());
    }
    if (!Strings.isNullOrEmpty(request.userNickname)) {
        Optional<User> requestUserOptional = User.tryGetByNickname(context, request.userNickname);
        if (requestUserOptional.isEmpty()) {
            throw new AccessDeniedException("attempt to produce user rating report for user [" + request.userNickname + "], but that user does not exist -- not allowed");
        }
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), requestUserOptional.get(), Permission.BULK_USERRATINGSPREADSHEETREPORT_USER)) {
            throw new AccessDeniedException("attempt to access a user rating report for user [" + request.userNickname + "], but this was disallowed");
        }
        spec.setUserNickname(request.userNickname);
    } else {
        if (!Strings.isNullOrEmpty(request.pkgName)) {
            Optional<Pkg> requestPkgOptional = Pkg.tryGetByName(context, request.pkgName);
            if (requestPkgOptional.isEmpty()) {
                throw new AccessDeniedException("attempt to produce user rating report for pkg [" + request.pkgName + "], but that pkg does not exist -- not allowed");
            }
            if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), requestPkgOptional.get(), Permission.BULK_USERRATINGSPREADSHEETREPORT_PKG)) {
                throw new AccessDeniedException("attempt to access a user rating report for pkg [" + request.pkgName + "], but this was disallowed");
            }
            spec.setPkgName(request.pkgName);
        } else {
            if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.BULK_USERRATINGSPREADSHEETREPORT_ALL)) {
                throw new AccessDeniedException("attempt to access a user rating report, but was unauthorized");
            }
        }
    }
    spec.setOwnerUserNickname(user.getNickname());
    return new QueueUserRatingSpreadsheetJobResult(jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_QUEUED_STARTED));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) UserRatingSpreadsheetJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingSpreadsheetJobSpecification) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) QueueUserRatingSpreadsheetJobResult(org.haiku.haikudepotserver.api1.model.userrating.job.QueueUserRatingSpreadsheetJobResult)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 QueueUserRatingSpreadsheetJobResult (org.haiku.haikudepotserver.api1.model.userrating.job.QueueUserRatingSpreadsheetJobResult)1 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)1 User (org.haiku.haikudepotserver.dataobjects.User)1 UserRatingSpreadsheetJobSpecification (org.haiku.haikudepotserver.userrating.model.UserRatingSpreadsheetJobSpecification)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1