Skip to content

Commit ad5e299

Browse files
authored
Merge pull request #20 from SalesforceLabs/fix/file-privacy
fix: File Sharing Privacy
2 parents 3dd44fe + 92aeffe commit ad5e299

4 files changed

Lines changed: 91 additions & 2 deletions

File tree

.DS_Store

-2 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/IlluminatedCloud/File_Explorer___Dev/OfflineSymbolTable.zip
33
/.idea/
44
File Explorer - Dev.iml
5+
.DS_Store

src/classes/qsydFileExplorerTest.cls

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ global with sharing class qsydFileExplorerTest {
1212
private static qsyd_FileList fileItemList;
1313
private static qsyd_Folder folderItem;
1414
private static qsyd_FolderList folderItemList;
15+
private static User standardUser;
1516

1617
@TestSetup
1718
private static void setupTestData() {
@@ -27,7 +28,8 @@ global with sharing class qsydFileExplorerTest {
2728
Title = 'Penguins',
2829
PathOnClient = 'Penguins.jpg',
2930
VersionData = Blob.valueOf(SIG_B64),
30-
IsMajorVersion = true
31+
IsMajorVersion = true,
32+
SharingPrivacy = 'P' // set it to private on records
3133
);
3234
insert contentVersion;
3335
List<ContentDocument> contentDocumentList = [
@@ -64,6 +66,44 @@ global with sharing class qsydFileExplorerTest {
6466
'tags',
6567
contentDocumentList[0].ContentSize,
6668
null);
69+
70+
71+
createStandardUser();
72+
73+
}
74+
75+
@future
76+
private static void createStandardUser() {
77+
String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@file-explorer.dev';
78+
79+
Profile p = [
80+
SELECT Id
81+
FROM Profile
82+
WHERE Name = 'Standard User'
83+
];
84+
85+
standardUser = new User(
86+
Alias = 'stduser',
87+
Email='standarduser@file-explorer.dev',
88+
EmailEncodingKey='UTF-8',
89+
LastName='Test',
90+
LanguageLocaleKey='en_US',
91+
LocaleSidKey='en_US',
92+
ProfileId = p.Id,
93+
TimeZoneSidKey='Australia/Sydney',
94+
UserName = uniqueUserName
95+
);
96+
insert standardUser;
97+
98+
PermissionSet ps = [
99+
SELECT Id
100+
FROM PermissionSet
101+
WHERE Name = 'File_Explorer_Permission_Set'];
102+
103+
insert new PermissionSetAssignment(
104+
AssigneeId = standardUser.id,
105+
PermissionSetId = ps.Id
106+
);
67107
}
68108

69109
@IsTest
@@ -150,4 +190,23 @@ global with sharing class qsydFileExplorerTest {
150190

151191
System.assertEquals(2, canonicalFolders.size());
152192
}
193+
194+
@IsTest
195+
private static void given_fileSetToPrivateOnRecords_when_fileRetreieved_success() {
196+
List<FileExplorerFile__c> fileExplorerFileList;
197+
198+
setupTestData();
199+
200+
System.runAs(standardUser) {
201+
fileExplorerFileList = fileItemList
202+
.retrieve(a.Id)
203+
.convertToCanonical()
204+
.getCanonicalList();
205+
206+
// does not return any file
207+
// as only the owner can see this file
208+
// https://help.salesforce.com/s/articleView?id=sf.collab_files_make_private_on_record.htm&type=5
209+
System.assertEquals(0, fileExplorerFileList.size());
210+
}
211+
}
153212
}

src/classes/qsyd_FileList.cls

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ global inherited sharing class qsyd_FileList implements qsyd_IItemList {
5151
*/
5252
global qsyd_FileList retrieve(String recordId) {
5353

54+
List<ContentDocumentLink> cdlList = [
55+
SELECT
56+
ContentDocumentId
57+
FROM ContentDocumentLink
58+
WHERE ContentDocument.IsDeleted = FALSE
59+
AND IsDeleted = FALSE
60+
AND LinkedEntityId = :recordId
61+
];
62+
63+
List<Id> contentDocumentIds = new List<Id>();
64+
for (ContentDocumentLink cdl : (List<ContentDocumentLink>)Security.stripInaccessible(AccessType.READABLE, cdlList).getRecords()) {
65+
contentDocumentIds.add(cdl.ContentDocumentId);
66+
}
67+
68+
List<ContentVersion> contentVersionList = [
69+
SELECT
70+
Id,
71+
ContentDocumentId
72+
FROM ContentVersion
73+
WHERE ContentDocumentId IN :contentDocumentIds
74+
];
75+
contentDocumentIds.clear();
76+
for (ContentVersion contentVersion : (List<ContentVersion>)Security.stripInaccessible(AccessType.READABLE, contentVersionList).getRecords()) {
77+
contentDocumentIds.add(contentVersion.ContentDocumentId);
78+
}
79+
80+
5481
this.fileExplorerFilesMap = new Map<Id, FileExplorerFile__c>([
5582
SELECT Folder__c,
5683
Label__c,
@@ -64,7 +91,9 @@ global inherited sharing class qsyd_FileList implements qsyd_IItemList {
6491
Tags__c,
6592
ContentSize__c
6693
FROM FileExplorerFile__c
67-
WHERE LinkedEntityId__c = :recordId
94+
WHERE
95+
ContentDocumentId__c IN :contentDocumentIds AND
96+
LinkedEntityId__c = :recordId
6897
WITH SECURITY_ENFORCED
6998
ORDER BY Label__c
7099
]);

0 commit comments

Comments
 (0)