Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10207 bug api datasets versions latest fix #10212

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,7 @@ private DatasetVersion getDatasetVersionOrDie(final DataverseRequest req, String

@Override
public Command<DatasetVersion> handleLatest() {
return new GetLatestAccessibleDatasetVersionCommand(req, ds, includeDeaccessioned);
return new GetLatestAccessibleDatasetVersionCommand(req, ds, includeDeaccessioned, checkPermsWhenDeaccessioned);
jp-tosca marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@
public class GetLatestAccessibleDatasetVersionCommand extends AbstractCommand<DatasetVersion> {
private final Dataset ds;
private final boolean includeDeaccessioned;
private boolean checkPerms;

public GetLatestAccessibleDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset) {
this(aRequest, anAffectedDataset, false);
this(aRequest, anAffectedDataset, false, false);
}

public GetLatestAccessibleDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset, boolean includeDeaccessioned) {
public GetLatestAccessibleDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset, boolean includeDeaccessioned, boolean checkPerms) {
super(aRequest, anAffectedDataset);
ds = anAffectedDataset;
this.includeDeaccessioned = includeDeaccessioned;
this.checkPerms = checkPerms;
}

@Override
public DatasetVersion execute(CommandContext ctxt) throws CommandException {
if (ds.getLatestVersion().isDraft() && ctxt.permissions().requestOn(getRequest(), ds).has(Permission.ViewUnpublishedDataset)) {
return ctxt.engine().submit(new GetDraftDatasetVersionCommand(getRequest(), ds));
}
return ctxt.engine().submit(new GetLatestPublishedDatasetVersionCommand(getRequest(), ds, includeDeaccessioned, true));
return ctxt.engine().submit(new GetLatestPublishedDatasetVersionCommand(getRequest(), ds, includeDeaccessioned, checkPerms));
jp-tosca marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,49 @@
public class GetLatestPublishedDatasetVersionCommand extends AbstractCommand<DatasetVersion> {
private final Dataset ds;
private final boolean includeDeaccessioned;
private boolean checkPerms;
private final boolean checkPermsWhenDeaccessioned;

public GetLatestPublishedDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset) {
this(aRequest, anAffectedDataset, false, false);
}

public GetLatestPublishedDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset, boolean includeDeaccessioned, boolean checkPerms) {
public GetLatestPublishedDatasetVersionCommand(DataverseRequest aRequest, Dataset anAffectedDataset, boolean includeDeaccessioned, boolean checkPermsWhenDeaccessioned) {
super(aRequest, anAffectedDataset);
ds = anAffectedDataset;
this.includeDeaccessioned = includeDeaccessioned;
this.checkPerms = checkPerms;
this.checkPermsWhenDeaccessioned = checkPermsWhenDeaccessioned;
}

/*
* This command depending on the requested parameters will return:
*
* If the user requested to include a deaccessioned dataset with the files, the command will return the deaccessioned version if the user has permissions to view the files. Otherwise, it will return null.
* If the user requested to include a deaccessioned dataset but did not request the files, the command will return the deaccessioned version.
* If the user did not request to include a deaccessioned dataset, the command will return the latest published version.
*
*/
@Override
public DatasetVersion execute(CommandContext ctxt) throws CommandException {
DatasetVersion dsVersionResult = getReleaseOrDeaccessionedDatasetVersion();
if (dsVersionResult != null && userHasPermissionsOnDatasetVersion(dsVersionResult, checkPermsWhenDeaccessioned, ctxt, ds)) {
return dsVersionResult;
}
return null;
}

for (DatasetVersion dsv : ds.getVersions()) {
if (dsv.isReleased() || (includeDeaccessioned && dsv.isDeaccessioned())) {

if(dsv.isDeaccessioned() && checkPerms){
if(!ctxt.permissions().requestOn(getRequest(), ds).has(Permission.EditDataset)){
return null;
}
}
return dsv;
private DatasetVersion getReleaseOrDeaccessionedDatasetVersion() {
for (DatasetVersion dsVersion : ds.getVersions()) {
if (dsVersion.isReleased() || (includeDeaccessioned && dsVersion.isDeaccessioned())) {
return dsVersion;
}
}
return null;
}

private boolean userHasPermissionsOnDatasetVersion(DatasetVersion dsVersionResult, boolean checkPermsWhenDeaccessioned, CommandContext ctxt, Dataset ds) {
if (dsVersionResult.isDeaccessioned() && checkPermsWhenDeaccessioned) {
return ctxt.permissions().requestOn(getRequest(), ds).has(Permission.EditDataset);
}
return true;
}
}
Loading
Loading