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

fix possible npe on attempt to retrieve parent folder of shared folder #256

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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.researchspace.core.util.jsonserialisers.ISO8601DateTimeSerialiser;
import com.researchspace.model.User;
import com.researchspace.model.record.BaseRecord;
import com.researchspace.model.record.Folder;
import java.util.Optional;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -49,13 +51,16 @@ public class RecordTreeItemInfo extends IdentifiableNameableApiObject {
@JsonProperty("type")
private ApiRecordType type = null;

public RecordTreeItemInfo(BaseRecord record, User authorisedSubject) {
public RecordTreeItemInfo(BaseRecord record, User user) {
super(record.getId(), record.getGlobalIdentifier(), record.getName());
setCreatedMillis(record.getCreationDateMillis());
setLastModifiedMillis(record.getModificationDateMillis());
// set parent folder if user is owner of document else null
if (authorisedSubject.equals(record.getOwner()) && record.hasParents()) {
setParentFolderId(record.getOwnerParent().get().getId());
if (user.equals(record.getOwner()) && record.hasParents()) {
Optional<Folder> parentForCurrentUser = record.getOwnerOrSharedParentForUser(user);
if (parentForCurrentUser.isPresent()) {
setParentFolderId(parentForCurrentUser.get().getId());
}
}
setOwner(new ApiUser(record.getOwner()));
if (record.isNotebook()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,7 @@ public AjaxReturnObject<List<RecordInformation>> getDocumentsLinkedToAttachment(

/**
* Gets record information for a list of IDs (and revision numbers) of EcatMediaFiles
* If info cannot be retrieved, value of map will be null and there will be an error
*
*
* @param ids
* @param revisions
* @return a map with keys in "$id-$revision" format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void traverseFolderAndSharedFolder() throws Exception {
result = deleteByIdFailsWith4xxStatus(u1, apiKey, info.getId());
}

// ser shouldn't be able to create content inside these folders either.
// user shouldn't be able to create content inside these folders either.
ApiFolder folderPost = new ApiFolder();
folderPost.setName("TestFolder");
folderPost.setParentFolderId(sharedId);
Expand Down Expand Up @@ -294,6 +294,30 @@ public void traverseFolderAndSharedFolder() throws Exception {
deleteByIdFailsWith4xxStatus(group.getPi(), piApiKey, getRootFolderForUser(u1).getId());
}

@Test
public void listOwnSharedFolderInNonOwnedFolder_RSDEV_488() throws Exception {
TestGroup group = createTestGroup(2);
User u1 = group.u1();
logoutAndLoginAs(u1);
String apiKey = createNewApiKeyForUser(u1);

Long groupSharedFolderId = group.getGroup().getCommunalGroupFolderId();
ApiFolder folderPost = new ApiFolder();
folderPost.setName("u1 test folder in group's shared folder");
folderPost.setParentFolderId(groupSharedFolderId);
MvcResult result = this.mockMvc.perform(folderCreate(u1, apiKey, folderPost)).andReturn();
ApiFolder created = getFromJsonResponseBody(result, ApiFolder.class);
assertNotNull(created.getId());

// listing of top-level folder works fine for u1 (RSDEV-488)
ApiRecordTreeItemListing sharedFolderListing =
performFolderListingById(u1, apiKey, groupSharedFolderId);
assertEquals(1, sharedFolderListing.getTotalHits().intValue());
RecordTreeItemInfo retrievedFolder = sharedFolderListing.getRecords().get(0);
assertEquals(u1.getUsername(), retrievedFolder.getOwner().getUsername());
assertEquals(groupSharedFolderId, retrievedFolder.getParentFolderId());
}

private long getIdFromNameForListing(String name, ApiRecordTreeItemListing listing) {
return listing.getRecords().stream()
.filter(item -> item.getName().equals(name))
Expand Down