Search in sources :

Example 36 with RoleAssignment

use of edu.harvard.iq.dataverse.RoleAssignment in project dataverse by IQSS.

the class DeleteDatasetVersionCommand method executeImpl.

@Override
protected void executeImpl(CommandContext ctxt) throws CommandException {
    ctxt.permissions().checkEditDatasetLock(doomed, getRequest(), this);
    // if you are deleting a dataset that only has 1 draft, we are actually destroying the dataset
    if (doomed.getVersions().size() == 1) {
        ctxt.engine().submit(new DestroyDatasetCommand(doomed, getRequest()));
    } else {
        // we are only deleting a version
        // todo: for now, it's only the latest and if it's a draft
        // but we should add the ability to destroy a specific version
        DatasetVersion doomedVersion = doomed.getLatestVersion();
        if (doomedVersion.isDraft()) {
            Long versionId = doomedVersion.getId();
            // files
            Iterator<FileMetadata> fmIt = doomedVersion.getFileMetadatas().iterator();
            while (fmIt.hasNext()) {
                FileMetadata fmd = fmIt.next();
                if (!fmd.getDataFile().isReleased()) {
                    // if file is draft (ie. new to this version, delete
                    // and remove fileMetadata from list (so that it won't try to merge)
                    ctxt.engine().submit(new DeleteDataFileCommand(fmd.getDataFile(), getRequest()));
                    fmIt.remove();
                }
            }
            DatasetVersion doomedAndMerged = ctxt.em().merge(doomedVersion);
            ctxt.em().remove(doomedAndMerged);
            // remove version from ds obj before indexing....
            Iterator<DatasetVersion> dvIt = doomed.getVersions().iterator();
            while (dvIt.hasNext()) {
                DatasetVersion dv = dvIt.next();
                if (versionId.equals(dv.getId())) {
                    dvIt.remove();
                }
            }
            /**
             * DeleteDatasetDraft, which is required by this command,
             * DeleteDatasetVersionCommand is not sufficient for running
             * GetPrivateUrlCommand nor DeletePrivateUrlCommand, both of
             * which require ManageDatasetPermissions because
             * DeletePrivateUrlCommand calls RevokeRoleCommand which
             * requires ManageDatasetPermissions when executed on a dataset
             * so we make direct calls to the service bean so that a lowly
             * Contributor who does NOT have ManageDatasetPermissions can
             * still successfully delete a Private URL.
             */
            PrivateUrl privateUrl = ctxt.privateUrl().getPrivateUrlFromDatasetId(doomed.getId());
            if (privateUrl != null) {
                logger.fine("Deleting Private URL for dataset id " + doomed.getId());
                PrivateUrlUser privateUrlUser = new PrivateUrlUser(doomed.getId());
                List<RoleAssignment> roleAssignments = ctxt.roles().directRoleAssignments(privateUrlUser, doomed);
                for (RoleAssignment roleAssignment : roleAssignments) {
                    ctxt.roles().revoke(roleAssignment);
                }
            }
            boolean doNormalSolrDocCleanUp = true;
            ctxt.index().indexDataset(doomed, doNormalSolrDocCleanUp);
            return;
        }
        throw new IllegalCommandException("Cannot delete a released version", this);
    }
}
Also used : PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl)

Example 37 with RoleAssignment

use of edu.harvard.iq.dataverse.RoleAssignment in project dataverse by IQSS.

the class PublishDataverseCommand method execute.

@Override
public Dataverse execute(CommandContext ctxt) throws CommandException {
    if (dataverse.isReleased()) {
        throw new IllegalCommandException("Dataverse " + dataverse.getAlias() + " has already been published.", this);
    }
    Dataverse parent = dataverse.getOwner();
    // root dataverse doesn't have a parent
    if (parent != null) {
        if (!parent.isReleased()) {
            throw new IllegalCommandException("Dataverse " + dataverse.getAlias() + " may not be published because its host dataverse (" + parent.getAlias() + ") has not been published.", this);
        }
    }
    // Before setting dataverse to released send notifications to users with download file
    List<RoleAssignment> ras = ctxt.roles().directRoleAssignments(dataverse);
    for (RoleAssignment ra : ras) {
        if (ra.getRole().permissions().contains(Permission.DownloadFile)) {
            for (AuthenticatedUser au : ctxt.roleAssignees().getExplicitUsers(ctxt.roleAssignees().getRoleAssignee(ra.getAssigneeIdentifier()))) {
                ctxt.notifications().sendNotification(au, new Timestamp(new Date().getTime()), UserNotification.Type.ASSIGNROLE, dataverse.getId());
            }
        }
    }
    dataverse.setPublicationDate(new Timestamp(new Date().getTime()));
    dataverse.setReleaseUser((AuthenticatedUser) getUser());
    Dataverse savedDataverse = ctxt.dataverses().save(dataverse);
    /**
     * @todo consider also
     * ctxt.solrIndex().indexPermissionsOnSelfAndChildren(savedDataverse.getId());
     */
    /**
     * @todo what should we do with the indexRespose?
     */
    IndexResponse indexResponse = ctxt.solrIndex().indexPermissionsForOneDvObject(savedDataverse);
    return savedDataverse;
}
Also used : IndexResponse(edu.harvard.iq.dataverse.search.IndexResponse) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) Dataverse(edu.harvard.iq.dataverse.Dataverse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 38 with RoleAssignment

use of edu.harvard.iq.dataverse.RoleAssignment in project dataverse by IQSS.

the class CreatePrivateUrlCommand method execute.

@Override
public PrivateUrl execute(CommandContext ctxt) throws CommandException {
    logger.fine("Executing CreatePrivateUrlCommand...");
    if (dataset == null) {
        /**
         * @todo Internationalize this.
         */
        String message = "Can't create Private URL. Dataset is null.";
        logger.info(message);
        throw new IllegalCommandException(message, this);
    }
    PrivateUrl existing = ctxt.privateUrl().getPrivateUrlFromDatasetId(dataset.getId());
    if (existing != null) {
        /**
         * @todo Internationalize this.
         */
        String message = "Private URL already exists for dataset id " + dataset.getId() + ".";
        logger.info(message);
        throw new IllegalCommandException(message, this);
    }
    DatasetVersion latestVersion = dataset.getLatestVersion();
    if (!latestVersion.isDraft()) {
        /**
         * @todo Internationalize this.
         */
        String message = "Can't create Private URL because the latest version of dataset id " + dataset.getId() + " is not a draft.";
        logger.info(message);
        throw new IllegalCommandException(message, this);
    }
    PrivateUrlUser privateUrlUser = new PrivateUrlUser(dataset.getId());
    DataverseRole memberRole = ctxt.roles().findBuiltinRoleByAlias(DataverseRole.MEMBER);
    final String privateUrlToken = UUID.randomUUID().toString();
    RoleAssignment roleAssignment = ctxt.engine().submit(new AssignRoleCommand(privateUrlUser, memberRole, dataset, getRequest(), privateUrlToken));
    PrivateUrl privateUrl = new PrivateUrl(roleAssignment, dataset, ctxt.systemConfig().getDataverseSiteUrl());
    return privateUrl;
}
Also used : PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) 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)

Aggregations

RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)38 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)23 PrivateUrlUser (edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser)22 RoleAssignee (edu.harvard.iq.dataverse.authorization.RoleAssignee)19 Test (org.junit.Test)19 Dataset (edu.harvard.iq.dataverse.Dataset)16 DvObject (edu.harvard.iq.dataverse.DvObject)14 Dataverse (edu.harvard.iq.dataverse.Dataverse)8 ArrayList (java.util.ArrayList)8 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)7 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)7 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)6 PrivateUrl (edu.harvard.iq.dataverse.privateurl.PrivateUrl)5 DataverseRoleServiceBean (edu.harvard.iq.dataverse.DataverseRoleServiceBean)4 User (edu.harvard.iq.dataverse.authorization.users.User)4 TestCommandContext (edu.harvard.iq.dataverse.engine.TestCommandContext)4 TestDataverseEngine (edu.harvard.iq.dataverse.engine.TestDataverseEngine)4 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)4 Before (org.junit.Before)4 DataverseFieldTypeInputLevel (edu.harvard.iq.dataverse.DataverseFieldTypeInputLevel)3