Sub topic 9
Securing data for guest and external users
This chapter assumes you can write a basic Apex controller and a wired Lightning web component. You should also know Salesforce profiles, permission sets, and sharing. Securing a site component means controlling what the server returns. The UI only decides what to show.
Who is visiting
A site has two kinds of visitors:
- Guest users are unauthenticated visitors. They all share one guest user record and one guest profile.
- External users are logged-in customers or partners. They have licenses such as Customer Community or Partner Community.
Both run as a user whose access is narrower than an internal user's. Apex and wire calls from your component run under that user's permissions.
The layers of access
A request for data passes through several checks. Failing any one blocks the data.
Apex: enforce the user's access
Apex runs in system mode by default. That ignores the user's permissions, so you must ask for enforcement. Declare the class with sharing and add WITH USER_MODE to the query.
public with sharing class CaseListController {
@AuraEnabled(cacheable=true)
public static List<Case> getMyCases(Integer maxRows) {
return [
SELECT Id, CaseNumber, Subject, Status
FROM Case
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :maxRows
];
}
}with sharing applies record sharing. WITH USER_MODE also applies object permissions and field-level security. If the user lacks access to a field in the query, the query throws an exception. It does not silently drop the field.
Granting access
Configure each layer for the user type you serve:
- Apex class access. Add
CaseListControllerto the guest profile or to a permission set for external users. Without it, the call fails. - Object and field permissions. Grant only the read access the component needs.
- Record access. Guest users get records only through guest user sharing rules, which are read-only. External users get records through sharing sets, sharing rules, or share groups.
Handling guests in the component
You can check the visitor type with the @salesforce/user/isGuest import. The template can then show a login prompt instead of an empty or broken list.
import { LightningElement, wire } from 'lwc';
import isGuest from '@salesforce/user/isGuest';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseList extends LightningElement {
isGuest = isGuest;
@wire(getMyCases, { maxRows: 5 })
cases;
}Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Class declared without sharing | Users see records they shouldn't | Use `with sharing` |
| Query without `WITH USER_MODE` | Field-level security is skipped | Add `WITH USER_MODE` |
| Class missing from the guest profile | The call fails with an access error | Enable the class on the profile |
| Broad guest permissions | Public data exposure | Grant minimum read access and review guest sharing rules |
Test with a real guest session and a real external user. An admin account will pass checks that the site users fail.
Exercise
A guest visitor opens a page with your case list component. The Apex class is with sharing and the query uses WITH USER_MODE, but the component shows an error. Name two settings you should check.
Show answer
Check that the Apex class is enabled for the guest user profile, and that the guest profile has read access to the Case object and the queried fields. Record visibility also needs a guest user sharing rule, though missing rules normally return no rows instead of an error.