use of nl.knaw.huygens.timbuctoo.v5.security.dto.User in project timbuctoo by HuygensING.
the class RsDocumentBuilder method getCapabilityList.
/**
* Get the capability list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>.
* The {@link Optional} is empty if the dataSet is not published and the given <code>user</code> == <code>null</code>
* or has no read access for the dataSet or the dataSet does not exist.
*
* @param user User that requests the list, may be <code>null</code>
* @param ownerId ownerId
* @param dataSetId dataSetId
* @return the capability list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>
*/
public Optional<Urlset> getCapabilityList(@Nullable User user, String ownerId, String dataSetId) {
Urlset capabilityList = null;
Optional<DataSet> maybeDataSet = dataSetRepository.getDataSet(user, ownerId, dataSetId);
if (maybeDataSet.isPresent()) {
RsMd rsMd = new RsMd(Capability.CAPABILITYLIST.xmlValue);
capabilityList = new Urlset(rsMd).addLink(new RsLn(REL_UP, rsUriHelper.uriForWellKnownResourceSync()));
DataSetMetaData dataSetMetaData = maybeDataSet.get().getMetadata();
String descriptionUrl = rsUriHelper.uriForRsDocument(dataSetMetaData, DESCRIPTION_FILENAME);
capabilityList.addLink(new RsLn(REL_DESCRIBED_BY, descriptionUrl).withType(DESCRIPTION_TYPE));
String loc = rsUriHelper.uriForRsDocument(dataSetMetaData, Capability.RESOURCELIST);
UrlItem item = new UrlItem(loc).withMetadata(new RsMd(Capability.RESOURCELIST.xmlValue));
capabilityList.addItem(item);
}
return Optional.ofNullable(capabilityList);
}
use of nl.knaw.huygens.timbuctoo.v5.security.dto.User in project timbuctoo by HuygensING.
the class EntityToJsonMapper method mapChange.
private JsonNode mapChange(Change change) {
String userId = change.getUserId();
ObjectNode changeNode = new ObjectMapper().valueToTree(change);
try {
userValidator.getUserFromUserId(userId).ifPresent(user -> changeNode.set("username", jsn(user.getDisplayName())));
} catch (UserValidationException e) {
LOG.error("Could not retrieve user store", e);
}
return changeNode;
}
use of nl.knaw.huygens.timbuctoo.v5.security.dto.User in project timbuctoo by HuygensING.
the class Index method createNew.
@POST
public Response createNew(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, ObjectNode body) throws URISyntaxException {
Optional<User> user;
try {
user = userValidator.getUserFromAccessToken(authHeader);
} catch (UserValidationException e) {
user = Optional.empty();
}
Optional<User> newUser = user;
if (!user.isPresent()) {
return Response.status(Response.Status.UNAUTHORIZED).build();
} else {
return transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
UUID id = crudService.create(collectionName, body, newUser.get());
return commitAndReturn(Response.created(SingleEntity.makeUrl(collectionName, id)).build());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn(e.getMessage()))).build());
} catch (IOException e) {
return rollbackAndReturn(Response.status(Response.Status.BAD_REQUEST).entity(jsnO("message", jsn(e.getMessage()))).build());
} catch (PermissionFetchingException e) {
return rollbackAndReturn(Response.status(Response.Status.FORBIDDEN).entity(jsnO("message", jsn(e.getMessage()))).build());
}
});
}
}
use of nl.knaw.huygens.timbuctoo.v5.security.dto.User in project timbuctoo by HuygensING.
the class SingleEntity method put.
@PUT
public Response put(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id, ObjectNode body) {
Optional<User> user;
try {
user = userValidator.getUserFromAccessToken(authHeader);
} catch (UserValidationException e) {
user = Optional.empty();
}
Optional<User> newUser = user;
if (!newUser.isPresent()) {
return Response.status(Response.Status.UNAUTHORIZED).build();
} else {
UpdateMessage updateMessage = transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
crudService.replace(collectionName, id.get(), body, newUser.get());
return commitAndReturn(UpdateMessage.success());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.NOT_FOUND));
} catch (NotFoundException e) {
return rollbackAndReturn(UpdateMessage.failure("not found", Response.Status.NOT_FOUND));
} catch (IOException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (AlreadyUpdatedException e) {
return rollbackAndReturn(UpdateMessage.failure("Entry was already updated", Response.Status.EXPECTATION_FAILED));
} catch (PermissionFetchingException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.FORBIDDEN));
}
});
// committed in the database
if (updateMessage.isSuccess()) {
return transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
JsonNode jsonNode = crudService.get(collectionName, id.get());
return commitAndReturn(Response.ok(jsonNode).build());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("Collection '" + collectionName + "' was available a moment ago, but not anymore: " + e.getMessage()))).build());
} catch (NotFoundException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
}
});
} else {
return Response.status(updateMessage.getResponseStatus()).entity(jsnO("message", jsn(updateMessage.getException().get()))).build();
}
}
}
use of nl.knaw.huygens.timbuctoo.v5.security.dto.User in project timbuctoo by HuygensING.
the class LocalFileUserAccess method addUser.
@Override
public void addUser(User user) throws AuthenticationUnavailableException {
final List<User> users;
try {
synchronized (usersFile) {
users = objectMapper.readValue(usersFile.toFile(), new TypeReference<List<User>>() {
});
}
users.add(user);
objectMapper.writeValue(usersFile.toFile(), users.toArray(new User[users.size()]));
} catch (IOException e) {
JsonBasedUserStore.LOG.error("Cannot read {}", usersFile.toAbsolutePath());
JsonBasedUserStore.LOG.error("Exception thrown", e);
throw new AuthenticationUnavailableException(e.getMessage());
}
}
Aggregations