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;
}
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));
}
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));
}
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();
}
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;
}
Aggregations