Sub topic 12
Enforcing sharing and field-level security
This chapter assumes you can write an @AuraEnabled method, call it imperatively from a component, and read basic SOQL. It covers what the Apex controller must do so that the data your component receives respects the running user's permissions.
Why the controller is responsible
Apex called from LWC runs in system context by default. That means it can read and write records and fields the user has no access to. The component cannot fix this, because anyone can call an @AuraEnabled method directly with their own parameters. The controller is the only place where access can be enforced reliably.
Three separate checks apply:
| Check | Question it answers | How you enforce it |
|---|---|---|
| Sharing | Can this user see or edit this record? | `with sharing` on the class |
| Object permissions (CRUD) | Can this user read, create, update, or delete this object? | `WITH USER_MODE` or `as user` |
| Field-level security (FLS) | Can this user read or edit this field? | `WITH USER_MODE` or `as user` |
with sharing only covers the first row. A class can be with sharing and still return a field the user is not allowed to see.
Sharing: use `with sharing`
Declare the class with with sharing so record-level access (ownership, roles, sharing rules) applies to every query and DML statement in it.
public with sharing class ContactController {
// queries here respect record sharing
}If you omit the keyword, the class inherits the sharing mode of its caller. When an @AuraEnabled method is the entry point, that is an easy way to end up without sharing enforced. State the mode explicitly.
CRUD and FLS: use user mode
User mode applies the running user's object and field permissions, together with sharing, to a single operation. The controller from this tutorial uses it in two places.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> searchContacts(String searchTerm) {
String key = '%' + searchTerm + '%';
return [
SELECT Id, Name, Email
FROM Contact
WHERE Name LIKE :key
WITH USER_MODE
ORDER BY Name
LIMIT 20
];
}
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
try {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
} catch (DmlException e) {
throw new AuraHandledException(e.getDmlMessage(0));
}
}
}WITH USER_MODEon a query checks read access toContact,Id,Name, andEmail. If the user cannot read one of them, the query throws aQueryExceptioninstead of silently dropping the field.update as userchecks update access on the object and onEmail. A violation throws aDmlException, which the method converts to anAuraHandledExceptionso the component can display a readable message.
What the component sees
When access is denied, the imperative call rejects. Your existing catch block receives the error, and the error handling from earlier chapters displays it. No special permission code is needed in the component.
Alternatives and common mistakes
WITH SECURITY_ENFORCEDis an older query clause. It checks only the fields in theSELECTclause and does not coverWHERE. PreferWITH USER_MODEin new code.Security.stripInaccessible()removes fields the user cannot access instead of throwing. Use it when you want to return partial data rather than fail.- Using
with sharingand assuming FLS is covered. It is not. - Using
without sharingin an@AuraEnabledclass for convenience. Reserve it for narrow, reviewed cases, and keep that logic in a separate class. - Trusting parameters such as
contactId. User mode protects the record, but still validate the input your logic depends on. - Checking permissions in JavaScript with
ifstatements. This improves the interface but gives no security.
Exercise
A controller class is declared with sharing. Its @AuraEnabled method runs SELECT Id, Name, SSN__c FROM Contact with no other clause. A user can see the contact record but has no read access to SSN__c. Does the user receive the value, and how would you fix the query?
Show answer
Yes, the user receives the value. with sharing only enforces record-level access, not field-level security, and a query without a user-mode clause runs with system permissions for objects and fields. Add WITH USER_MODE to the query. The query then throws a QueryException because the user cannot read SSN__c, and the imperative call rejects so the component can show the error. If you would rather return the contact without that field, use Security.stripInaccessible(AccessType.READABLE, records) on the query results.