Search in sources :

Example 6 with DataverseRole

use of edu.harvard.iq.dataverse.authorization.DataverseRole in project dataverse by IQSS.

the class Dataverses method createAssignment.

/**
 * This code for setting a dataverse logo via API was started when initially
 * investigating https://github.com/IQSS/dataverse/issues/3559 but it isn't
 * finished so it's commented out. See also * "No functionality should be
 * GUI-only. Make all functionality reachable via the API" at
 * https://github.com/IQSS/dataverse/issues/3440
 */
// File tempDir;
// 
// private void createTempDir(Dataverse editDv) {
// try {
// File tempRoot = java.nio.file.Files.createDirectories(Paths.get("../docroot/logos/temp")).toFile();
// tempDir = java.nio.file.Files.createTempDirectory(tempRoot.toPath(), editDv.getId().toString()).toFile();
// } catch (IOException e) {
// throw new RuntimeException("Error creating temp directory", e); // improve error handling
// }
// }
// 
// private DataverseTheme initDataverseTheme(Dataverse editDv) {
// DataverseTheme dvt = new DataverseTheme();
// dvt.setLinkColor(DEFAULT_LINK_COLOR);
// dvt.setLogoBackgroundColor(DEFAULT_LOGO_BACKGROUND_COLOR);
// dvt.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
// dvt.setTextColor(DEFAULT_TEXT_COLOR);
// dvt.setDataverse(editDv);
// return dvt;
// }
// 
// @PUT
// @Path("{identifier}/logo")
// @Consumes(MediaType.MULTIPART_FORM_DATA)
// public Response setDataverseLogo(@PathParam("identifier") String dvIdtf,
// @FormDataParam("file") InputStream fileInputStream,
// @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
// @QueryParam("key") String apiKey) {
// boolean disabled = true;
// if (disabled) {
// return error(Status.FORBIDDEN, "Setting the dataverse logo via API needs more work.");
// }
// try {
// final DataverseRequest req = createDataverseRequest(findUserOrDie());
// final Dataverse editDv = findDataverseOrDie(dvIdtf);
// 
// logger.finer("entering fileUpload");
// if (tempDir == null) {
// createTempDir(editDv);
// logger.finer("created tempDir");
// }
// File uploadedFile;
// try {
// String fileName = contentDispositionHeader.getFileName();
// 
// uploadedFile = new File(tempDir, fileName);
// if (!uploadedFile.exists()) {
// uploadedFile.createNewFile();
// }
// logger.finer("created file");
// File file = null;
// file = FileUtil.inputStreamToFile(fileInputStream);
// if (file.length() > systemConfig.getUploadLogoSizeLimit()) {
// return error(Response.Status.BAD_REQUEST, "File is larger than maximum size: " + systemConfig.getUploadLogoSizeLimit() + ".");
// }
// java.nio.file.Files.copy(fileInputStream, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// logger.finer("copied inputstream to file");
// editDv.setDataverseTheme(initDataverseTheme(editDv));
// editDv.getDataverseTheme().setLogo(fileName);
// 
// } catch (IOException e) {
// logger.finer("caught IOException");
// logger.throwing("ThemeWidgetFragment", "handleImageFileUpload", e);
// throw new RuntimeException("Error uploading logo file", e); // improve error handling
// }
// // If needed, set the default values for the logo
// if (editDv.getDataverseTheme().getLogoFormat() == null) {
// editDv.getDataverseTheme().setLogoFormat(DataverseTheme.ImageFormat.SQUARE);
// }
// logger.finer("end handelImageFileUpload");
// UpdateDataverseThemeCommand cmd = new UpdateDataverseThemeCommand(editDv, uploadedFile, req);
// Dataverse saved = execCommand(cmd);
// 
// /**
// * @todo delete the temp file:
// * docroot/logos/temp/1148114212463761832421/cc0.png
// */
// return ok("logo uploaded: " + saved.getDataverseTheme().getLogo());
// } catch (WrappedResponse ex) {
// return error(Status.BAD_REQUEST, "problem uploading logo: " + ex);
// }
// }
@POST
@Path("{identifier}/assignments")
public Response createAssignment(RoleAssignmentDTO ra, @PathParam("identifier") String dvIdtf, @QueryParam("key") String apiKey) {
    try {
        final DataverseRequest req = createDataverseRequest(findUserOrDie());
        final Dataverse dataverse = findDataverseOrDie(dvIdtf);
        RoleAssignee assignee = findAssignee(ra.getAssignee());
        if (assignee == null) {
            return error(Status.BAD_REQUEST, "Assignee not found");
        }
        DataverseRole theRole;
        Dataverse dv = dataverse;
        theRole = null;
        while ((theRole == null) && (dv != null)) {
            for (DataverseRole aRole : rolesSvc.availableRoles(dv.getId())) {
                if (aRole.getAlias().equals(ra.getRole())) {
                    theRole = aRole;
                    break;
                }
            }
            dv = dv.getOwner();
        }
        if (theRole == null) {
            return error(Status.BAD_REQUEST, "Can't find role named '" + ra.getRole() + "' in dataverse " + dataverse);
        }
        String privateUrlToken = null;
        return ok(json(execCommand(new AssignRoleCommand(assignee, theRole, dataverse, req, privateUrlToken))));
    } catch (WrappedResponse ex) {
        LOGGER.log(Level.WARNING, "Can''t create assignment: {0}", ex.getMessage());
        return ex.getResponse();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) JsonString(javax.json.JsonString) AssignRoleCommand(edu.harvard.iq.dataverse.engine.command.impl.AssignRoleCommand) Dataverse(edu.harvard.iq.dataverse.Dataverse) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 7 with DataverseRole

use of edu.harvard.iq.dataverse.authorization.DataverseRole in project dataverse by IQSS.

the class Datasets method createAssignment.

/**
 * @todo Make this real. Currently only used for API testing. Copied from
 * the equivalent API endpoint for dataverses and simplified with values
 * hard coded.
 */
@POST
@Path("{identifier}/assignments")
public Response createAssignment(String userOrGroup, @PathParam("identifier") String id, @QueryParam("key") String apiKey) {
    boolean apiTestingOnly = true;
    if (apiTestingOnly) {
        return error(Response.Status.FORBIDDEN, "This is only for API tests.");
    }
    try {
        Dataset dataset = findDatasetOrDie(id);
        RoleAssignee assignee = findAssignee(userOrGroup);
        if (assignee == null) {
            return error(Response.Status.BAD_REQUEST, "Assignee not found");
        }
        DataverseRole theRole = rolesSvc.findBuiltinRoleByAlias("admin");
        String privateUrlToken = null;
        return ok(json(execCommand(new AssignRoleCommand(assignee, theRole, dataset, createDataverseRequest(findUserOrDie()), privateUrlToken))));
    } catch (WrappedResponse ex) {
        logger.log(Level.WARNING, "Can''t create assignment: {0}", ex.getMessage());
        return ex.getResponse();
    }
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) AssignRoleCommand(edu.harvard.iq.dataverse.engine.command.impl.AssignRoleCommand) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 8 with DataverseRole

use of edu.harvard.iq.dataverse.authorization.DataverseRole in project dataverse by IQSS.

the class JsonPrinterTest method testJson_PrivateUrl.

@Test
public void testJson_PrivateUrl() {
    DataverseRole aRole = new DataverseRole();
    PrivateUrlUser privateUrlUserIn = new PrivateUrlUser(42);
    RoleAssignee anAssignee = privateUrlUserIn;
    Dataset dataset = new Dataset();
    String privateUrlToken = "e1d53cf6-794a-457a-9709-7c07629a8267";
    RoleAssignment ra = new RoleAssignment(aRole, anAssignee, dataset, privateUrlToken);
    String dataverseSiteUrl = "https://dataverse.example.edu";
    PrivateUrl privateUrl = new PrivateUrl(ra, dataset, dataverseSiteUrl);
    JsonObjectBuilder job = JsonPrinter.json(privateUrl);
    assertNotNull(job);
    JsonObject jsonObject = job.build();
    assertEquals("e1d53cf6-794a-457a-9709-7c07629a8267", jsonObject.getString("token"));
    assertEquals("https://dataverse.example.edu/privateurl.xhtml?token=e1d53cf6-794a-457a-9709-7c07629a8267", jsonObject.getString("link"));
    assertEquals("e1d53cf6-794a-457a-9709-7c07629a8267", jsonObject.getJsonObject("roleAssignment").getString("privateUrlToken"));
    assertEquals("#42", jsonObject.getJsonObject("roleAssignment").getString("assignee"));
}
Also used : PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) Dataset(edu.harvard.iq.dataverse.Dataset) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) Test(org.junit.Test)

Example 9 with DataverseRole

use of edu.harvard.iq.dataverse.authorization.DataverseRole in project dataverse by IQSS.

the class PrivateUrlUtilTest method testGetPrivateUrlUserFromRoleAssignmentAndAssigneeNonPrivateUrlUser.

@Test
public void testGetPrivateUrlUserFromRoleAssignmentAndAssigneeNonPrivateUrlUser() {
    DataverseRole aRole = null;
    RoleAssignee assignee = GuestUser.get();
    DvObject dataset = new Dataset();
    String privateUrlToken = "cd71e9d7-73a7-4ec8-b890-3d00499e8693";
    RoleAssignment assignment = new RoleAssignment(aRole, assignee, dataset, privateUrlToken);
    PrivateUrlUser privateUrl = PrivateUrlUtil.getPrivateUrlUserFromRoleAssignment(assignment, assignee);
    assertNull(privateUrl);
}
Also used : DvObject(edu.harvard.iq.dataverse.DvObject) Dataset(edu.harvard.iq.dataverse.Dataset) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) Test(org.junit.Test)

Example 10 with DataverseRole

use of edu.harvard.iq.dataverse.authorization.DataverseRole in project dataverse by IQSS.

the class PrivateUrlUtilTest method testGetDraftDatasetVersionFromRoleAssignmentNullDataset.

@Test
public void testGetDraftDatasetVersionFromRoleAssignmentNullDataset() {
    DataverseRole aRole = null;
    PrivateUrlUser privateUrlUser = new PrivateUrlUser(42);
    RoleAssignee anAssignee = privateUrlUser;
    DvObject dataset = null;
    String privateUrlToken = null;
    RoleAssignment ra = new RoleAssignment(aRole, anAssignee, dataset, privateUrlToken);
    DatasetVersion datasetVersion = PrivateUrlUtil.getDraftDatasetVersionFromRoleAssignment(ra);
    assertNull(datasetVersion);
}
Also used : DvObject(edu.harvard.iq.dataverse.DvObject) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) Test(org.junit.Test)

Aggregations

DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)44 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)23 RoleAssignee (edu.harvard.iq.dataverse.authorization.RoleAssignee)19 Test (org.junit.Test)19 PrivateUrlUser (edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser)18 Dataset (edu.harvard.iq.dataverse.Dataset)16 DvObject (edu.harvard.iq.dataverse.DvObject)12 Dataverse (edu.harvard.iq.dataverse.Dataverse)9 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)8 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)6 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)6 ArrayList (java.util.ArrayList)5 User (edu.harvard.iq.dataverse.authorization.users.User)4 DataverseRoleServiceBean (edu.harvard.iq.dataverse.DataverseRoleServiceBean)3 Permission (edu.harvard.iq.dataverse.authorization.Permission)3 TestCommandContext (edu.harvard.iq.dataverse.engine.TestCommandContext)3 TestDataverseEngine (edu.harvard.iq.dataverse.engine.TestDataverseEngine)3 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)3 PrivateUrl (edu.harvard.iq.dataverse.privateurl.PrivateUrl)3 Timestamp (java.sql.Timestamp)3