Search in sources :

Example 6 with User

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);
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) RsLn(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn) Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) UrlItem(nl.knaw.huygens.timbuctoo.remote.rs.xml.UrlItem) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd)

Example 7 with User

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;
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with User

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());
            }
        });
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) IOException(java.io.IOException) UUID(java.util.UUID) POST(javax.ws.rs.POST)

Example 9 with User

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();
        }
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) AlreadyUpdatedException(nl.knaw.huygens.timbuctoo.core.AlreadyUpdatedException) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) PUT(javax.ws.rs.PUT)

Example 10 with User

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());
    }
}
Also used : AuthenticationUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Aggregations

User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)42 Test (org.junit.Test)22 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)19 IOException (java.io.IOException)11 DataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData)9 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)7 PermissionFetchingException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException)6 Path (javax.ws.rs.Path)5 Urlset (nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset)5 ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)5 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 GET (javax.ws.rs.GET)4 Response (javax.ws.rs.core.Response)4 BasicDataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 GraphQLSchema (graphql.schema.GraphQLSchema)3 Optional (java.util.Optional)3 DataStoreCreationException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException)3 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)3