Sample Code Without Sharing: Give Guest Users Access to Create Records and Read Them Later
These code samples support two separate interactions. In the first interaction, the guest user creates a case. To allow for future access, an Apex method replaces the record ID with an encrypted string. When the guest user wants to read the case later, they enter the encrypted string. An Apex method decrypts the string and uses it to retrieve the case.
This sample Aura component displays several components where a guest user can enter details about a new case or the token from an existing case. After record creation, lightning:card components display the new case’s encrypted token or the status of the case that matches the token.
For demonstration purposes, this sample uses a field where the guest user can enter their case’s token. To implement this scenario, provide the guest user with a link that contains the token, and retrieve the token from the URL.
This sample JavaScript controller processes events for the Aura component and calls the methods in the helper file.
This JavaScript helper has two methods:
makeCase()
The makeCase() method creates an asynchronous request to create a case with the submitted data. When the request completes, the callback stores the new case’s unique token in the caseID field in a variable used by the Aura component.
getCase()
The getCase() method uses a token entered by the guest user to asynchronously retrieve a case that matches the token. The method’s callback catches the response from the Apex method and stores the value in the case_status variable.
This sample Apex controller receives calls to create and retrieve cases. It uses the User Encryption Decryption AppExchange package to encrypt and decrypt data.
CreateCase()
The CreateCase() Apex method creates a case with the guest user’s input. After record creation, it generates an encrypted string from the record ID, the record’s CreatedDate field, and the current timestamp. The Apex method returns the encrypted string.
GetCase()
The GetCase() method decrypts the provided string, validates the results, and passes the decrypted record ID and created timestamp to a helper method to retrieve the original record. The response is the status of the record.
Define the class with the without sharing keyword because we aren’t relying on object permissions and platform sharing to create and access records.
Any system or individual on the internet can invoke @AuraEnabled methods. Make sure that the query can retrieve only the newly created record, and select only the required fields.
If you’re loading highly sensitive information, consider one of these additional measures to increase security.
- Ask the user to enter additional information related to the data that they’re attempting to read or modify, which only they know.
- Require the user to log in to read or modify the data.
See Also