Sub topic 14
FLS in Lightning components and @AuraEnabled methods
This chapter assumes you know basic Apex and SOQL, and that you know how to set field access with profiles and permission sets. It also assumes you know Apex runs in system mode by default.
Two paths, two behaviors
A Lightning component can reach data in two ways. Only one of them enforces field-level security (FLS) for you.
- Lightning Data Service and base components. These include
lightning-record-form,lightning-record-edit-form,lightning-input-field, and thegetRecordwire adapter. They use the User Interface API, which applies the current user's FLS. Fields the user cannot read are omitted. Fields the user cannot edit are shown read-only. - Your own Apex. A method marked
@AuraEnabledruns in system mode, so FLS is not checked. The same is true for LWC and Aura components. If the method returnsAnnualRevenueand the user has no read access, the value still reaches the browser.
Enforcing FLS in the controller
Add user mode to the query and to the DML. This controller does both.
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getTechAccounts() {
try {
return [
SELECT Id, Name, AnnualRevenue
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODE
LIMIT 50
];
} catch (QueryException e) {
throw new AuraHandledException(e.getMessage());
}
}
@AuraEnabled
public static void saveRevenue(Id accountId, Decimal revenue) {
try {
Account acc = new Account(Id = accountId, AnnualRevenue = revenue);
Database.update(acc, AccessLevel.USER_MODE);
} catch (DmlException e) {
throw new AuraHandledException(e.getMessage());
}
}
}with sharing controls which records the user sees. WITH USER_MODE and AccessLevel.USER_MODE control object permissions and FLS. You need both for full enforcement.
If you want the component to work even when some fields are hidden, use Security.stripInaccessible(AccessType.READABLE, records) instead. It removes the hidden fields and does not throw an exception.
Handling the result in the component
User mode throws when a field is inaccessible. Catching the exception and rethrowing AuraHandledException lets the component show a readable message instead of a generic error.
On the client side, the getRecord wire adapter has a related distinction. A field in fields that the user cannot read makes the whole request fail. A field in optionalFields is silently left out. Use optionalFields for fields that some users may not see.
Common mistakes
- Marking a controller
with sharingand assuming that enforces FLS. It enforces record sharing only. - Using
cacheable=truewithstripInaccessibleor user mode and forgetting that cached results are per user, not shared across users. - Returning a wrapper class built from system-mode data. Apply the check before you copy values into the wrapper.
Exercise
A component calls an @AuraEnabled method declared with sharing that runs SELECT Id, Internal_Notes__c FROM Account. The user has no read access to InternalNotes_c. What happens, and how do you fix it?
Show answer
The query succeeds and returns InternalNotesc, because with sharing only applies record sharing and the method runs in system mode. Fix it by adding `WITH USERMODE to the query, which throws a QueryException for the hidden field. Alternatively, run the results through Security.stripInaccessible(AccessType.READABLE, records)` to remove the field without throwing.