Skip to content

Commit

Permalink
Merged in task/dspace-cris-2023_02_x/DSC-1905 (pull request DSpace#2716)
Browse files Browse the repository at this point in the history
DSC-1905

Approved-by: Giuseppe Digilio
  • Loading branch information
vins01-4science authored and atarix83 committed Sep 12, 2024
2 parents 57d4c8d + 2faf90d commit 13681d6
Show file tree
Hide file tree
Showing 29 changed files with 825 additions and 69 deletions.
23 changes: 21 additions & 2 deletions dspace-api/src/main/java/org/dspace/administer/ProcessCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.cli.ParseException;
import org.apache.commons.lang.time.DateUtils;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.ProcessStatus;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.Process;
import org.dspace.scripts.factory.ScriptServiceFactory;
Expand Down Expand Up @@ -78,12 +81,14 @@ public void internalRun() throws Exception {
}

Context context = new Context();
assignCurrentUserInContext(context);
assignSpecialGroupsInContext(context);

try {
context.turnOffAuthorisationSystem();
handleAuthorizationSystem(context);
performDeletion(context);
} finally {
context.restoreAuthSystemState();
handleAuthorizationSystem(context);
context.complete();
}

Expand Down Expand Up @@ -137,4 +142,18 @@ public ProcessCleanerConfiguration<ProcessCleaner> getScriptConfiguration() {
.getServiceByName("process-cleaner", ProcessCleanerConfiguration.class);
}

private void assignCurrentUserInContext(Context context) throws SQLException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid);
context.setCurrentUser(ePerson);
}
}

private void assignSpecialGroupsInContext(Context context) throws SQLException {
for (UUID uuid : handler.getSpecialGroups()) {
context.setSpecialGroup(uuid);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void internalRun() throws Exception {
assignCurrentUserInContext();
assignSpecialGroupsInContext();

context.turnOffAuthorisationSystem();
handleAuthorizationSystem(context);

Collection collection = getCollection();
if (collection == null) {
Expand All @@ -76,7 +76,7 @@ public void internalRun() throws Exception {
try {
performExport(collection);
context.complete();
context.restoreAuthSystemState();
handleAuthorizationSystem(context);
} catch (Exception e) {
handler.handleException(e);
context.abort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.app.bulkedit;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.UUID;

Expand All @@ -18,6 +19,7 @@
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.MetadataDSpaceCsvExportService;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.handle.factory.HandleServiceFactory;
Expand Down Expand Up @@ -54,19 +56,31 @@ public void internalRun() throws Exception {
printHelp();
return;
}

Context context = new Context();
context.turnOffAuthorisationSystem();
try {
context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier()));
assignCurrentUserInContext(context);
assignSpecialGroupsInContext(context);
} catch (SQLException e) {
handler.handleException(e);
}
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService
.handleExport(context, exportAllItems, exportAllMetadata, identifier,
handler);
handler.writeFilestream(context, filename, dSpaceCSV.getInputStream(), EXPORT_CSV);
context.restoreAuthSystemState();
context.complete();

handleAuthorizationSystem(context);
try {
DSpaceCSV dSpaceCSV =
metadataDSpaceCsvExportService.handleExport(
context, exportAllItems, exportAllMetadata, identifier, handler
);
try (InputStream is = dSpaceCSV.getInputStream()) {
handler.writeFilestream(context, filename, is, EXPORT_CSV);
}

handleAuthorizationSystem(context);
context.complete();
} catch (Exception e) {
handler.handleException(e);
context.abort();
}
}

protected void logHelpInfo() {
Expand Down Expand Up @@ -119,4 +133,18 @@ protected String getFileNameForExportFile() throws ParseException {
}
return null;
}

private void assignCurrentUserInContext(Context context) throws SQLException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid);
context.setCurrentUser(ePerson);
}
}

private void assignSpecialGroupsInContext(Context context) throws SQLException {
for (UUID uuid : handler.getSpecialGroups()) {
context.setSpecialGroup(uuid);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
*/
package org.dspace.app.bulkedit;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.cli.Options;
import org.dspace.core.Context;
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.configuration.ScriptConfiguration;

/**
Expand All @@ -17,6 +22,16 @@ public class MetadataExportScriptConfiguration<T extends MetadataExport> extends

private Class<T> dspaceRunnableClass;

public boolean isAllowedToExecute(Context context, List<DSpaceCommandLineParameter> commandLineParameters) {
try {
return authorizeService.isAdmin(context) || authorizeService.isComColAdmin(context) ||
authorizeService.isItemAdmin(context);
} catch (SQLException e) {
throw new RuntimeException(
"SQLException occurred when checking if the current user is eligible to run the script", e);
}
}

@Override
public Class<T> getDspaceRunnableClass() {
return dspaceRunnableClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.dspace.app.bulkedit;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -32,6 +33,7 @@
import org.dspace.discovery.indexobject.IndexableCommunity;
import org.dspace.discovery.utils.DiscoverQueryBuilder;
import org.dspace.discovery.utils.parameter.QueryBuilderSearchFilter;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.scripts.DSpaceRunnable;
Expand Down Expand Up @@ -113,7 +115,8 @@ public void internalRun() throws Exception {

IndexableObject dso = null;
Context context = new Context();
context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier()));
assignCurrentUserInContext(context);
assignSpecialGroupsInContext(context);

if (hasScope) {
dso = resolveScope(context, identifier);
Expand All @@ -135,20 +138,28 @@ public void internalRun() throws Exception {
queryBuilderSearchFilters.add(queryBuilderSearchFilter);
}
}
handler.logDebug("building query");
DiscoverQuery discoverQuery =
queryBuilder.buildQuery(context, dso, discoveryConfiguration, query, queryBuilderSearchFilters,
"Item", 10, Long.getLong("0"), null, SortOption.DESCENDING);
handler.logDebug("creating iterator");

Iterator<Item> itemIterator = searchService.iteratorSearch(context, dso, discoverQuery);
handler.logDebug("creating dspacecsv");
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService.export(context, itemIterator, true);
handler.logDebug("writing to file " + getFileNameOrExportFile());
handler.writeFilestream(context, getFileNameOrExportFile(), dSpaceCSV.getInputStream(), EXPORT_CSV);
context.restoreAuthSystemState();
context.complete();
try {
handler.logDebug("building query");
DiscoverQuery discoverQuery =
queryBuilder.buildQuery(context, dso, discoveryConfiguration, query, queryBuilderSearchFilters,
"Item", 10, Long.getLong("0"), null, SortOption.DESCENDING);

handler.logDebug("creating iterator");
Iterator<Item> itemIterator = searchService.iteratorSearch(context, dso, discoverQuery);
handler.logDebug("creating dspacecsv");
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService.export(context, itemIterator, true);

try (InputStream is = dSpaceCSV.getInputStream()) {
handler.logDebug("writing to file " + getFileNameOrExportFile());
handler.writeFilestream(context, getFileNameOrExportFile(), is, EXPORT_CSV);
}

handleAuthorizationSystem(context);
context.complete();
} catch (Exception e) {
handler.handleException(e);
context.abort();
}
}

protected void loghelpinfo() {
Expand All @@ -159,6 +170,21 @@ protected String getFileNameOrExportFile() {
return "metadataExportSearch.csv";
}

private void assignCurrentUserInContext(Context context) throws SQLException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid);
context.setCurrentUser(ePerson);
}
}

private void assignSpecialGroupsInContext(Context context) throws SQLException {
for (UUID uuid : handler.getSpecialGroups()) {
context.setSpecialGroup(uuid);
}
}


public IndexableObject resolveScope(Context context, String id) throws SQLException {
UUID uuid = UUID.fromString(id);
IndexableObject scopeObj = new IndexableCommunity(communityService.find(context, uuid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

package org.dspace.app.bulkedit;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.cli.Options;
import org.dspace.core.Context;
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.configuration.ScriptConfiguration;

/**
Expand All @@ -18,6 +23,17 @@ public class MetadataExportSearchScriptConfiguration<T extends MetadataExportSea

private Class<T> dspaceRunnableclass;

@Override
public boolean isAllowedToExecute(Context context, List<DSpaceCommandLineParameter> commandLineParameters) {
try {
return authorizeService.isAdmin(context) || authorizeService.isComColAdmin(context) ||
authorizeService.isItemAdmin(context);
} catch (SQLException e) {
throw new RuntimeException(
"SQLException occurred when checking if the current user is eligible to run the script", e);
}
}

@Override
public Class<T> getDspaceRunnableClass() {
return dspaceRunnableclass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ public void internalRun() throws Exception {
// Create a context
Context c = null;
c = new Context();
c.turnOffAuthorisationSystem();

// Find the EPerson, assign to context
assignCurrentUserInContext(c);
assignSpecialGroupsInContext(c);

// Read commandLines from the CSV file
try {
Expand All @@ -207,6 +207,7 @@ public void internalRun() throws Exception {
initMetadataImport(csv);
List<BulkEditChange> changes;

handleAuthorizationSystem(c);
if (!commandLine.hasOption('s') || validateOnly) {
// See what has changed
try {
Expand Down Expand Up @@ -250,7 +251,7 @@ public void internalRun() throws Exception {
}

// Finsh off and tidy up
c.restoreAuthSystemState();
handleAuthorizationSystem(c);
c.complete();
} catch (Exception e) {
c.abort();
Expand All @@ -272,6 +273,12 @@ protected void assignCurrentUserInContext(Context context) throws ParseException
}
}

private void assignSpecialGroupsInContext(Context context) throws SQLException {
for (UUID uuid : handler.getSpecialGroups()) {
context.setSpecialGroup(uuid);
}
}

/**
* This method determines whether the changes should be applied or not. This is default set to true for the REST
* script as we don't want to interact with the caller. This will be overwritten in the CLI script to ask for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
package org.dspace.app.bulkedit;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.cli.Options;
import org.dspace.core.Context;
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.configuration.ScriptConfiguration;

/**
Expand All @@ -19,6 +23,17 @@ public class MetadataImportScriptConfiguration<T extends MetadataImport> extends

private Class<T> dspaceRunnableClass;

@Override
public boolean isAllowedToExecute(Context context, List<DSpaceCommandLineParameter> commandLineParameters) {
try {
return authorizeService.isAdmin(context) || authorizeService.isComColAdmin(context) ||
authorizeService.isItemAdmin(context);
} catch (SQLException e) {
throw new RuntimeException(
"SQLException occurred when checking if the current user is eligible to run the script", e);
}
}

@Override
public Class<T> getDspaceRunnableClass() {
return dspaceRunnableClass;
Expand Down
Loading

0 comments on commit 13681d6

Please sign in to comment.