How to prevent certain profile users to download files in Salesforce?

How to prevent certain profile users to download files in Salesforce?

Use the following code to prevent certain Profile users to download files in Salesforce.

Apex Classes that implements Sfc.ContentDownloadHandlerFactory interface will be triggered when the files are downloaded in Salesforce.

Sample Code:

public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {

    public Sfc.ContentDownloadHandler getContentDownloadHandler( List<ID> listIds, Sfc.ContentDownloadContext context ) {
    
        Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
        
        system.debug( 'Inside the Content Download' );     
        Id profileId = UserInfo.getProfileId();
        
        if( String.isNotBlank( profileId ) ) {
        
            Profile objProfile = [ SELECT Id, Name FROM Profile WHERE Id =: profileId ];
            
            if ( objProfile != null && objProfile.Name == 'System Administrator' ) {
            
                contentDownloadHandler.isDownloadAllowed = false;
                contentDownloadHandler.downloadErrorMessage = 'Downloading a file is not allowed for your profile';
                return contentDownloadHandler;
            
            }
        
        }
    
        contentDownloadHandler.isDownloadAllowed = true;
        return contentDownloadHandler;
    
    }

}

Output:

Leave a Reply