Search in sources :

Example 1 with User

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

the class PkgJobApiImpl method queueSimplePkgJob.

private <R extends AbstractQueueJobResult> R queueSimplePkgJob(Class<R> resultClass, Class<? extends AbstractJobSpecification> jobSpecificationClass, Permission permission) {
    final ObjectContext context = serverRuntime.newContext();
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (user.isEmpty()) {
        throw new AccessDeniedException("attempt to queue [" + jobSpecificationClass.getSimpleName() + "] without a user");
    }
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, permission)) {
        throw new AccessDeniedException("attempt to queue [" + jobSpecificationClass.getSimpleName() + "] without sufficient authorization");
    }
    AbstractJobSpecification spec;
    try {
        spec = jobSpecificationClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("unable to create the job specification for class; " + jobSpecificationClass.getSimpleName(), e);
    }
    spec.setOwnerUserNickname(user.get().getNickname());
    R result;
    try {
        result = resultClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("unable to create the result; " + resultClass.getSimpleName(), e);
    }
    result.guid = jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_QUEUED_STARTED);
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) AbstractJobSpecification(org.haiku.haikudepotserver.job.model.AbstractJobSpecification)

Example 2 with User

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

the class PkgJobApiImpl method queuePkgScreenshotExportArchiveJob.

@Override
public QueuePkgScreenshotExportArchiveJobResult queuePkgScreenshotExportArchiveJob(QueuePkgScreenshotExportArchiveJobRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    final ObjectContext context = serverRuntime.newContext();
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.BULK_PKGSCREENSHOTEXPORTARCHIVE)) {
        throw new AccessDeniedException("attempt to export pkg screenshots as an archive, but was not authorized");
    }
    PkgScreenshotExportArchiveJobSpecification specification = new PkgScreenshotExportArchiveJobSpecification();
    specification.setOwnerUserNickname(user.get().getNickname());
    specification.setPkgName(request.pkgName);
    return new QueuePkgScreenshotExportArchiveJobResult(jobService.submit(specification, JobSnapshot.COALESCE_STATUSES_NONE));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext)

Example 3 with User

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

the class PkgJobApiImpl method queuePkgCategoryCoverageImportSpreadsheetJob.

@Override
public QueuePkgCategoryCoverageImportSpreadsheetJobResult queuePkgCategoryCoverageImportSpreadsheetJob(QueuePkgCategoryCoverageImportSpreadsheetJobRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.inputDataGuid), "the input data must be identified by guid");
    final ObjectContext context = serverRuntime.newContext();
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.BULK_PKGCATEGORYCOVERAGEIMPORTSPREADSHEET)) {
        throw new AccessDeniedException("attempt to import package categories, but was not authorized");
    }
    // now check that the data is present.
    jobService.tryGetData(request.inputDataGuid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), request.inputDataGuid));
    // setup and go
    PkgCategoryCoverageImportSpreadsheetJobSpecification spec = new PkgCategoryCoverageImportSpreadsheetJobSpecification();
    spec.setOwnerUserNickname(user.map(_User::getNickname).orElse(null));
    spec.setInputDataGuid(request.inputDataGuid);
    return new QueuePkgCategoryCoverageImportSpreadsheetJobResult(jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_NONE));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) ObjectContext(org.apache.cayenne.ObjectContext)

Example 4 with User

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

the class UserApiImpl method updateUser.

@Override
public UpdateUserResult updateUser(UpdateUserRequest updateUserRequest) {
    Preconditions.checkNotNull(updateUserRequest);
    Preconditions.checkState(!Strings.isNullOrEmpty(updateUserRequest.nickname));
    Preconditions.checkNotNull(updateUserRequest.filter);
    final ObjectContext context = serverRuntime.newContext();
    boolean activeDidChange = false;
    User user = User.tryGetByNickname(context, updateUserRequest.nickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), User.NICKNAME.getName()));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), user, Permission.USER_EDIT)) {
        throw new AccessDeniedException("cannot edit [" + user + "]");
    }
    for (UpdateUserRequest.Filter filter : updateUserRequest.filter) {
        switch(filter) {
            case NATURALLANGUAGE:
                if (Strings.isNullOrEmpty(updateUserRequest.naturalLanguageCode)) {
                    throw new IllegalStateException("the natural language code is required to update the natural language on a user");
                }
                user.setNaturalLanguage(getNaturalLanguage(context, updateUserRequest.naturalLanguageCode));
                LOGGER.info("will update the natural language on the user {} to {}", user.toString(), updateUserRequest.naturalLanguageCode);
                break;
            case EMAIL:
                user.setEmail(updateUserRequest.email);
                break;
            case ACTIVE:
                if (null == updateUserRequest.active) {
                    throw new IllegalStateException("the 'active' attribute is required to configure active on the user.");
                }
                activeDidChange = user.getActive() != updateUserRequest.active;
                user.setActive(updateUserRequest.active);
                break;
            default:
                throw new IllegalStateException("unknown filter in edit user; " + filter.name());
        }
    }
    if (context.hasChanges()) {
        context.commitChanges();
        LOGGER.info("did update the user {}", user.toString());
        if (activeDidChange) {
            List<String> pkgNames = userRatingService.pkgNamesEffectedByUserActiveStateChange(context, user);
            LOGGER.info("will update user rating derivation for {} packages owing to active state change on user {}", pkgNames.size(), user.toString());
            for (String pkgName : pkgNames) {
                jobService.submit(new UserRatingDerivationJobSpecification(pkgName), JobSnapshot.COALESCE_STATUSES_QUEUED);
            }
        }
    } else {
        LOGGER.info("no changes in updating the user {}", user.toString());
    }
    return new UpdateUserResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)

Example 5 with User

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

the class UserApiImpl method searchUsers.

@Override
public SearchUsersResult searchUsers(SearchUsersRequest searchUsersRequest) {
    Preconditions.checkNotNull(searchUsersRequest);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.USER_LIST)) {
        throw new AccessDeniedException("unable to list users");
    }
    UserSearchSpecification specification = new UserSearchSpecification();
    String exp = searchUsersRequest.expression;
    if (null != exp) {
        exp = Strings.emptyToNull(exp.trim().toLowerCase());
    }
    specification.setExpression(exp);
    if (null != searchUsersRequest.expressionType) {
        specification.setExpressionType(PkgSearchSpecification.ExpressionType.valueOf(searchUsersRequest.expressionType.name()));
    }
    specification.setLimit(searchUsersRequest.limit);
    specification.setOffset(searchUsersRequest.offset);
    specification.setIncludeInactive(null != searchUsersRequest.includeInactive && searchUsersRequest.includeInactive);
    SearchUsersResult result = new SearchUsersResult();
    result.total = userService.total(context, specification);
    result.items = Collections.emptyList();
    if (0 != result.total) {
        List<User> searchedUsers = userService.search(context, specification);
        result.items = searchedUsers.stream().map(u -> {
            SearchUsersResult.User resultUser = new SearchUsersResult.User();
            resultUser.active = u.getActive();
            resultUser.nickname = u.getNickname();
            return resultUser;
        }).collect(Collectors.toList());
    }
    return result;
}
Also used : UserSearchSpecification(org.haiku.haikudepotserver.user.model.UserSearchSpecification) AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext)

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