diff --git a/pom.xml b/pom.xml index a0936e1..8790205 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,7 @@ nl.knaw.dans dans-dataverse-client-lib + 0.33.0 org.apache.commons diff --git a/src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java b/src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java index 3962aa4..ad2fa96 100644 --- a/src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java +++ b/src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java @@ -35,15 +35,13 @@ @Command public abstract class AbstractAssignmentRole, API> extends AbstractCmd { -/* @ParentCommand - protected DatasetCmd datasetCmd;*/ static class CommandParameter { - @Parameters(description = "alias and role assignee (example: @dataverseAdmin=contributor)") + @Parameters(description = "Alias and role assignee (example: @dataverseAdmin=contributor)") String assignment = ""; @Option(names = { "-f", - "--parameter-file" }, description = "CSV file to read parameters from. The file should have a header row with columns 'PID', 'ROLE', and 'ASSIGNMENT'.") + "--parameter-file" }, description = "CSV file to read parameters from. The file should have a header row with columns 'PID', 'ASSIGNEE' and 'ROLE'.") Path parameterFile; } diff --git a/src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java b/src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java index 47b5e3c..908d2d5 100644 --- a/src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java +++ b/src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java @@ -20,20 +20,25 @@ import nl.knaw.dans.dvcli.action.ThrowingFunction; import nl.knaw.dans.lib.dataverse.DataverseApi; import nl.knaw.dans.lib.dataverse.DataverseException; +import nl.knaw.dans.lib.dataverse.model.RoleAssignment; +import nl.knaw.dans.lib.dataverse.model.RoleAssignmentReadOnly; import picocli.CommandLine.Command; import picocli.CommandLine.ParentCommand; import java.io.IOException; +import java.util.Optional; @Command(name = "role-assignment", mixinStandardHelpOptions = true, description = "Manage role assignments.", - subcommands = { CollectionRoleAssignment.CollectionListRoleAssignments.class, CollectionRoleAssignment.CollectionAssignRole.class }) + subcommands = { CollectionRoleAssignment.CollectionListRoleAssignments.class, CollectionRoleAssignment.CollectionAssignRole.class, CollectionRoleAssignment.CollectionDeleteRole.class }) public class CollectionRoleAssignment extends AbstractCmd { @ParentCommand static private CollectionCmd collectionCmd; - @Command(name = "list", mixinStandardHelpOptions = true, description = "List the role assignments of a Dataverse collection.") + @Command(name = "list", + mixinStandardHelpOptions = true, + description = "List the role assignments of a Dataverse collection.") static class CollectionListRoleAssignments extends AbstractCmd { @Override @@ -73,6 +78,43 @@ collectionCmd.> paramsBatchProcessorBuilder() } } + @Command(name = "remove", + mixinStandardHelpOptions = true, + description = "remove a role assignment from the specified dataverse collection") + static class CollectionDeleteRole extends AbstractAssignmentRole { + + @Override + protected DataverseApi getItem(String pid) { + return collectionCmd.dataverseClient.dataverse(pid); + } + + private static class RoleAssignmentAction implements ThrowingFunction, String, Exception> { + @Override + public String apply(RoleAssignmentParams roleAssignmentParams) throws IOException, DataverseException { + if (roleAssignmentParams.roleAssignment().isPresent()) { + RoleAssignment roleAssignment = roleAssignmentParams.roleAssignment().get(); + Optional role = roleAssignmentParams.pid().listRoleAssignments().getData().stream() + .filter(r -> r.get_roleAlias().equals(roleAssignment.getRole()) && r.getAssignee().equals(roleAssignment.getAssignee())).findFirst(); + if (role.isPresent()) { + var r = roleAssignmentParams.pid().deleteRoleAssignment(role.get().getId()); + return r.getEnvelopeAsString(); + } + } + return "There was no assignment-role to assign."; + } + } + + @Override + public void doCall() throws IOException, DataverseException { + collectionCmd.> paramsBatchProcessorBuilder() + .labeledItems(getRoleAssignmentParams(collectionCmd)) + .action(new CollectionRoleAssignment.CollectionDeleteRole.RoleAssignmentAction()) + .report(new ConsoleReport<>()) + .build() + .process(); + } + } + @Override public void doCall() throws IOException, DataverseException { diff --git a/src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java b/src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java index a53c75b..3b52156 100644 --- a/src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java +++ b/src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java @@ -20,20 +20,25 @@ import nl.knaw.dans.dvcli.action.ThrowingFunction; import nl.knaw.dans.lib.dataverse.DatasetApi; import nl.knaw.dans.lib.dataverse.DataverseException; +import nl.knaw.dans.lib.dataverse.model.RoleAssignment; +import nl.knaw.dans.lib.dataverse.model.RoleAssignmentReadOnly; import picocli.CommandLine.Command; import picocli.CommandLine.ParentCommand; import java.io.IOException; +import java.util.Optional; @Command(name = "role-assignment", mixinStandardHelpOptions = true, description = "Manage role assignments.", - subcommands = { DatasetRoleAssignment.DatasetListRoleAssignments.class, DatasetRoleAssignment.DatasetAssignRole.class }) + subcommands = { DatasetRoleAssignment.DatasetListRoleAssignments.class, DatasetRoleAssignment.DatasetAssignRole.class, DatasetRoleAssignment.DatasetDeleteRole.class }) public class DatasetRoleAssignment extends AbstractCmd { @ParentCommand static private DatasetCmd datasetCmd; - @Command(name = "list", mixinStandardHelpOptions = true, description = "List role assignments for the specified dataset.") + @Command(name = "list", + mixinStandardHelpOptions = true, + description = "List role assignments for the specified dataset.") static class DatasetListRoleAssignments extends AbstractCmd { @Override @@ -74,6 +79,43 @@ datasetCmd.> paramsBatchProcessorBuilder() } + @Command(name = "remove", + mixinStandardHelpOptions = true, + description = "remove role assignment from specified dataset(s)") + static class DatasetDeleteRole extends AbstractAssignmentRole { + + @Override + protected DatasetApi getItem(String pid) { + return datasetCmd.dataverseClient.dataset(pid); + } + + private static class RoleAssignmentAction implements ThrowingFunction, String, Exception> { + @Override + public String apply(RoleAssignmentParams roleAssignmentParams) throws IOException, DataverseException { + if (roleAssignmentParams.roleAssignment().isPresent()) { + RoleAssignment roleAssignment = roleAssignmentParams.roleAssignment().get(); + Optional role = roleAssignmentParams.pid().listRoleAssignments().getData().stream() + .filter(r -> r.get_roleAlias().equals(roleAssignment.getRole()) && r.getAssignee().equals(roleAssignment.getAssignee())).findFirst(); + if (role.isPresent()) { + var r = roleAssignmentParams.pid().deleteRoleAssignment(role.get().getId()); + return r.getEnvelopeAsString(); + } + } + return "There was no assignment-role to assign."; + } + } + + @Override + public void doCall() throws IOException, DataverseException { + datasetCmd.> paramsBatchProcessorBuilder() + .labeledItems(getRoleAssignmentParams(datasetCmd)) + .action(new RoleAssignmentAction()) + .report(new ConsoleReport<>()) + .build() + .process(); + } + } + @Override public void doCall() throws IOException, DataverseException {