Search in sources :

Example 46 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class JobApiImpl method getJob.

@Override
public GetJobResult getJob(GetJobRequest request) {
    Preconditions.checkArgument(null != request);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.guid));
    final ObjectContext context = serverRuntime.newContext();
    JobSnapshot job = jobService.tryGetJob(request.guid).orElseThrow(() -> new ObjectNotFoundException(JobSnapshot.class.getSimpleName(), request.guid));
    if (Strings.isNullOrEmpty(job.getOwnerUserNickname())) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
            throw new AccessDeniedException("attempt to access jobs view");
        }
    } else {
        User ownerUser = User.tryGetByNickname(context, job.getOwnerUserNickname()).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), job.getOwnerUserNickname()));
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            throw new AccessDeniedException("attempt to access jobs view for [" + job + "]");
        }
    }
    // now output the result.
    GetJobResult result = new GetJobResult();
    result.guid = job.getGuid();
    result.jobStatus = JobStatus.valueOf(job.getStatus().name());
    result.jobTypeCode = job.getJobTypeCode();
    result.ownerUserNickname = job.getOwnerUserNickname();
    result.startTimestamp = null == job.getStartTimestamp() ? null : job.getStartTimestamp().getTime();
    result.finishTimestamp = null == job.getFinishTimestamp() ? null : job.getFinishTimestamp().getTime();
    result.queuedTimestamp = null == job.getQueuedTimestamp() ? null : job.getQueuedTimestamp().getTime();
    result.failTimestamp = null == job.getFailTimestamp() ? null : job.getFailTimestamp().getTime();
    result.cancelTimestamp = null == job.getCancelTimestamp() ? null : job.getCancelTimestamp().getTime();
    result.progressPercent = job.getProgressPercent();
    result.generatedDatas = new ArrayList<>();
    for (String guid : job.getGeneratedDataGuids()) {
        JobData jobData = jobService.tryGetData(guid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), guid));
        GetJobResult.JobData resultJobData = new GetJobResult.JobData();
        resultJobData.useCode = jobData.getUseCode();
        resultJobData.guid = jobData.getGuid();
        resultJobData.mediaTypeCode = jobData.getMediaTypeCode();
        resultJobData.filename = jobService.deriveDataFilename(guid);
        result.generatedDatas.add(resultJobData);
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) ObjectContext(org.apache.cayenne.ObjectContext) JobData(org.haiku.haikudepotserver.job.model.JobData)

Example 47 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class JobApiImpl method searchJobs.

@Override
public SearchJobsResult searchJobs(SearchJobsRequest request) {
    Preconditions.checkArgument(null != request);
    final SearchJobsResult result = new SearchJobsResult();
    final ObjectContext context = serverRuntime.newContext();
    User ownerUser = null;
    if (Strings.isNullOrEmpty(request.ownerUserNickname)) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
            throw new AccessDeniedException("attempt to access jobs view");
        }
    } else {
        ownerUser = User.tryGetByNickname(context, request.ownerUserNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.ownerUserNickname));
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            throw new AccessDeniedException("attempt to access jobs view for [" + ownerUser + "]");
        }
    }
    if (null != request.statuses && request.statuses.isEmpty()) {
        result.items = Collections.emptyList();
        result.total = 0L;
    } else {
        Set<JobSnapshot.Status> statuses = null;
        if (null != request.statuses) {
            statuses = request.statuses.stream().map(s -> JobSnapshot.Status.valueOf(s.name())).collect(Collectors.toSet());
        }
        result.total = (long) jobService.totalJobs(ownerUser, statuses);
        result.items = jobService.findJobs(ownerUser, statuses, null == request.offset ? 0 : request.offset, null == request.limit ? Integer.MAX_VALUE : request.limit).stream().map(js -> {
            SearchJobsResult.Job resultJob = new SearchJobsResult.Job();
            resultJob.guid = js.getGuid();
            resultJob.jobStatus = JobStatus.valueOf(js.getStatus().name());
            resultJob.jobTypeCode = js.getJobTypeCode();
            resultJob.ownerUserNickname = js.getOwnerUserNickname();
            resultJob.startTimestamp = null == js.getStartTimestamp() ? null : js.getStartTimestamp().getTime();
            resultJob.finishTimestamp = null == js.getFinishTimestamp() ? null : js.getFinishTimestamp().getTime();
            resultJob.queuedTimestamp = null == js.getQueuedTimestamp() ? null : js.getQueuedTimestamp().getTime();
            resultJob.failTimestamp = null == js.getFailTimestamp() ? null : js.getFailTimestamp().getTime();
            resultJob.cancelTimestamp = null == js.getCancelTimestamp() ? null : js.getCancelTimestamp().getTime();
            resultJob.progressPercent = js.getProgressPercent();
            return resultJob;
        }).collect(Collectors.toList());
        LOGGER.info("search for jobs found {} results", result.items.size());
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 48 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserAuthenticationServiceIT method testHashPassword_2.

@Test
public void testHashPassword_2() {
    User user = new User();
    // -----------------
    userAuthenticationService.setPassword(user, "Pa55word0");
    // -----------------
    Assertions.assertThat(userAuthenticationService.matchPassword(user, "Pa55word0")).isTrue();
    Assertions.assertThat(userAuthenticationService.matchPassword(user, "Other")).isFalse();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) Test(org.junit.jupiter.api.Test) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest)

Example 49 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserAuthenticationServiceIT method testHashPassword.

@Test
public void testHashPassword() {
    User user = new User();
    // -----------------
    userAuthenticationService.setPassword(user, "p4mphl3t");
    // -----------------
    Assertions.assertThat(userAuthenticationService.matchPassword(user, "p4mphl3t")).isTrue();
    Assertions.assertThat(userAuthenticationService.matchPassword(user, "Other")).isFalse();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) Test(org.junit.jupiter.api.Test) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest)

Example 50 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserAuthenticationServiceIT method testClearPassword.

@Test
public void testClearPassword() {
    org.junit.jupiter.api.Assertions.assertThrows(Exception.class, () -> {
        User user = new User();
        // -----------------
        userAuthenticationService.setPassword(user, null);
    // -----------------
    // expecting an exception.
    });
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) Test(org.junit.jupiter.api.Test) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest)

Aggregations

User (org.haiku.haikudepotserver.dataobjects.User)51 ObjectContext (org.apache.cayenne.ObjectContext)47 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)16 Test (org.junit.jupiter.api.Test)16 AccessDeniedException (org.springframework.security.access.AccessDeniedException)14 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)7 ObjectId (org.apache.cayenne.ObjectId)5 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)5 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)5 PermissionUserPkg (org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)4 AuthenticateUserRequest (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserRequest)3 AuthenticateUserResult (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserResult)3 Captcha (org.haiku.haikudepotserver.captcha.model.Captcha)3 Preconditions (com.google.common.base.Preconditions)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 CSVWriter (com.opencsv.CSVWriter)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Instant (java.time.Instant)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2