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

8092 timestamp of data access request #9257

Merged
3 changes: 3 additions & 0 deletions doc/release-notes/8092-timestamp-of-data-access-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A date column has been added to the restricted file access request overview, indicating when the earliest request by that user was made.

An issue was fixed where where the request list was not updated when a request was approved or rejected.
89 changes: 70 additions & 19 deletions src/main/java/edu/harvard/iq/dataverse/DataFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import edu.harvard.iq.dataverse.DatasetVersion.VersionState;
import edu.harvard.iq.dataverse.authorization.RoleAssignee;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.dataaccess.DataAccess;
import edu.harvard.iq.dataverse.dataaccess.StorageIO;
import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
import edu.harvard.iq.dataverse.datasetutility.FileSizeChecker;
import edu.harvard.iq.dataverse.ingest.IngestReport;
import edu.harvard.iq.dataverse.ingest.IngestRequest;
Expand All @@ -19,17 +18,17 @@
import edu.harvard.iq.dataverse.util.ShapefileHandler;
import edu.harvard.iq.dataverse.util.StringUtil;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.persistence.*;
Expand All @@ -47,9 +46,9 @@
query = "SELECT o FROM DataFile o WHERE o.creator.id=:creatorId"),
@NamedQuery(name = "DataFile.findByReleaseUserId",
query = "SELECT o FROM DataFile o WHERE o.releaseUser.id=:releaseUserId"),
@NamedQuery(name="DataFile.findDataFileByIdProtocolAuth",
@NamedQuery(name="DataFile.findDataFileByIdProtocolAuth",
query="SELECT s FROM DataFile s WHERE s.identifier=:identifier AND s.protocol=:protocol AND s.authority=:authority"),
@NamedQuery(name="DataFile.findDataFileThatReplacedId",
@NamedQuery(name="DataFile.findDataFileThatReplacedId",
query="SELECT s.id FROM DataFile s WHERE s.previousDataFileId=:identifier")
})
@Entity
Expand All @@ -73,7 +72,10 @@ public class DataFile extends DvObject implements Comparable {
@Column( nullable = false )
@Pattern(regexp = "^.*/.*$", message = "{contenttype.slash}")
private String contentType;


public void setFileAccessRequests(List<FileAccessRequest> fileAccessRequests) {
this.fileAccessRequests = fileAccessRequests;
}

// @Expose
// @SerializedName("storageIdentifier")
Expand Down Expand Up @@ -747,22 +749,71 @@ public String getUnf() {
}
return null;
}


@ManyToMany
@JoinTable(name = "fileaccessrequests",
joinColumns = @JoinColumn(name = "datafile_id"),
inverseJoinColumns = @JoinColumn(name = "authenticated_user_id"))
private List<AuthenticatedUser> fileAccessRequesters;
@OneToMany(mappedBy = "dataFile", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, orphanRemoval = true)
private List<FileAccessRequest> fileAccessRequests;

public List<AuthenticatedUser> getFileAccessRequesters() {
return fileAccessRequesters;
public List<FileAccessRequest> getFileAccessRequests() {
return fileAccessRequests;
}

public void setFileAccessRequesters(List<AuthenticatedUser> fileAccessRequesters) {
this.fileAccessRequesters = fileAccessRequesters;
public void addFileAccessRequester(AuthenticatedUser authenticatedUser) {
if (this.fileAccessRequests == null) {
this.fileAccessRequests = new ArrayList<>();
}

Set<AuthenticatedUser> existingUsers = this.fileAccessRequests.stream()
.map(FileAccessRequest::getAuthenticatedUser)
.collect(Collectors.toSet());

if (existingUsers.contains(authenticatedUser)) {
return;
}

FileAccessRequest request = new FileAccessRequest();
request.setCreationTime(new Date());
request.setDataFile(this);
request.setAuthenticatedUser(authenticatedUser);

FileAccessRequest.FileAccessRequestKey key = new FileAccessRequest.FileAccessRequestKey();
key.setAuthenticatedUser(authenticatedUser.getId());
key.setDataFile(this.getId());

request.setId(key);

this.fileAccessRequests.add(request);
}


public boolean removeFileAccessRequester(RoleAssignee roleAssignee) {
if (this.fileAccessRequests == null) {
return false;
}

FileAccessRequest request = this.fileAccessRequests.stream()
.filter(fileAccessRequest -> fileAccessRequest.getAuthenticatedUser().equals(roleAssignee))
.findFirst()
.orElse(null);

if (request != null) {
this.fileAccessRequests.remove(request);
return true;
}

return false;
}

public boolean containsFileAccessRequestFromUser(RoleAssignee roleAssignee) {
if (this.fileAccessRequests == null) {
return false;
}

Set<AuthenticatedUser> existingUsers = this.fileAccessRequests.stream()
.map(FileAccessRequest::getAuthenticatedUser)
.collect(Collectors.toSet());

return existingUsers.contains(roleAssignee);
}

public boolean isHarvested() {

Dataset ownerDataset = this.getOwner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,14 +797,13 @@ public void findFileMetadataOptimizedExperimental(Dataset owner, DatasetVersion
}

if (dataFile.isRestricted() && accessRequestFileIds.contains(dataFile.getId())) {
dataFile.setFileAccessRequesters(Collections.singletonList(au));
}
dataFile.addFileAccessRequester(au);
}

dataFiles.add(dataFile);
filesMap.put(dataFile.getId(), i++);
}
fileResults = null;


logger.fine("Retrieved and cached "+i+" datafiles.");

i = 0;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5038,10 +5038,9 @@ public boolean isFileAccessRequestMultiButtonRequired(){
// return false;
}
for (FileMetadata fmd : workingVersion.getFileMetadatas()){
AuthenticatedUser authenticatedUser = (AuthenticatedUser) session.getUser();
//Change here so that if all restricted files have pending requests there's no Request Button
if ((!this.fileDownloadHelper.canDownloadFile(fmd) && (fmd.getDataFile().getFileAccessRequesters() == null
|| ( fmd.getDataFile().getFileAccessRequesters() != null
&& !fmd.getDataFile().getFileAccessRequesters().contains((AuthenticatedUser)session.getUser()))))){
if ((!this.fileDownloadHelper.canDownloadFile(fmd) && !fmd.getDataFile().containsFileAccessRequestFromUser(authenticatedUser))) {
return true;
}
}
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/FileAccessRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "fileaccessrequests")
public class FileAccessRequest {
@EmbeddedId
private FileAccessRequestKey id;
@ManyToOne
@MapsId("dataFile")
@JoinColumn(name = "datafile_id")
private DataFile dataFile;
@ManyToOne
@MapsId("authenticatedUser")
@JoinColumn(name = "authenticated_user_id")
private AuthenticatedUser authenticatedUser;

@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "creation_time")
private Date creationTime;

public FileAccessRequestKey getId() {
return id;
}

public void setId(FileAccessRequestKey id) {
this.id = id;
}

public DataFile getDataFile() {
return dataFile;
}

public void setDataFile(DataFile dataFile) {
this.dataFile = dataFile;
}

public AuthenticatedUser getAuthenticatedUser() {
return authenticatedUser;
}

public void setAuthenticatedUser(AuthenticatedUser authenticatedUser) {
this.authenticatedUser = authenticatedUser;
}

public Date getCreationTime() {
return creationTime;
}

public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}

@Embeddable
public static class FileAccessRequestKey implements Serializable {
@Column(name = "datafile_id")
private Long dataFile;
@Column(name = "authenticated_user_id")
private Long authenticatedUser;

public Long getDataFile() {
return dataFile;
}

public void setDataFile(Long dataFile) {
this.dataFile = dataFile;
}

public Long getAuthenticatedUser() {
return authenticatedUser;
}

public void setAuthenticatedUser(Long authenticatedUser) {
this.authenticatedUser = authenticatedUser;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,12 @@ public void requestAccessIndirect() {
private boolean processRequestAccess(DataFile file, Boolean sendNotification) {
if (fileDownloadService.requestAccess(file.getId())) {
// update the local file object so that the page properly updates
if(file.getFileAccessRequesters() == null){
file.setFileAccessRequesters(new ArrayList());
}
file.getFileAccessRequesters().add((AuthenticatedUser) session.getUser());
AuthenticatedUser user = (AuthenticatedUser) session.getUser();
file.addFileAccessRequester(user);

// create notification if necessary
if (sendNotification) {
fileDownloadService.sendRequestFileAccessNotification(file, (AuthenticatedUser) session.getUser());
fileDownloadService.sendRequestFileAccessNotification(file, user);
}
JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("file.accessRequested.success"));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public boolean requestAccess(Long fileId) {
return false;
}
DataFile file = datafileService.find(fileId);
if (!file.getFileAccessRequesters().contains((AuthenticatedUser)session.getUser())) {
if (!file.containsFileAccessRequestFromUser(session.getUser())) {
try {
commandEngine.submit(new RequestAccessCommand(dvRequestService.getDataverseRequest(), file));
return true;
Expand Down
Loading