Sub topic 9
Enforcing sharing, object and field permissions
This chapter assumes you can write a basic @AuraEnabled method and call it from a Lightning web component. It also assumes you know Salesforce profiles, permission sets, and sharing rules.
Apex runs in system mode by default
When a component calls your method, Apex does not check what the running user may see or change. By default it runs in system mode. A user who lacks access to a field can still read it through your controller, and the component can then show it. Securing the controller is your job.
Three separate layers apply:
- Record access decides which rows the user can see. Sharing rules, role hierarchy, and ownership control it.
- Object permissions decide whether the user can read, create, edit, or delete a type of record.
- Field-level security (FLS) decides which fields the user can read or edit.
The with sharing keyword covers only the first layer. This is the most common mistake: developers add with sharing and assume the field and object checks are done.
Choose an enforcement mechanism
| Mechanism | Sharing rules | Object and field permissions |
|---|---|---|
| `with sharing` on the class | Yes | No |
| `WITH USER_MODE` in SOQL | Yes | Yes |
| `insert as user` (and other DML) | Yes | Yes |
| `WITH SECURITY_ENFORCED` | No | Yes, for fields in the query |
| `Security.stripInaccessible` | No | Removes inaccessible fields instead of failing |
Use with sharing on every controller class, plus user mode on every query and DML statement. The getContacts method from earlier chapters does this. with sharing sets the default for the class. WITH USER_MODE adds the object and field checks.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
}If the user cannot read Email, this query throws a QueryException. The component receives an error instead of data.
Fail loudly or strip quietly
User mode fails the whole operation when any field is inaccessible. That is correct for most screens. Sometimes you would rather return what the user may see. In that case, query without user mode and strip the fields afterward.
public with sharing class ContactLookupController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContactsLenient(Id accountId) {
List<Contact> rows = [
SELECT Id, Name, Email, Phone
FROM Contact
WHERE AccountId = :accountId
LIMIT 50
];
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.READABLE, rows);
return decision.getRecords();
}
}The returned records have the unreadable fields removed. The component sees undefined for them, so write the template to handle missing values.
Writes follow the same rule
Use the as user form for DML. The createContact method from the previous chapter does this with insert as user c. If the user lacks create permission on Contact, or edit access on a field you set, Apex throws a DmlException. Catch it and rethrow an AuraHandledException so the message reaches the component.
public with sharing class ContactUpdater {
@AuraEnabled
public static void renameContact(Id contactId, String lastName) {
try {
Contact c = [
SELECT Id, LastName FROM Contact
WHERE Id = :contactId
WITH USER_MODE
];
c.LastName = lastName;
update as user c;
} catch (Exception e) {
throw new AuraHandledException('Update failed: ' + e.getMessage());
}
}
}Pitfalls
- A class with no sharing keyword runs with the sharing mode of its caller, or
without sharingwhen it is the entry point. Always state the keyword. - Hiding a field in the component template does not protect it. Anything your method returns reaches the browser.
- Test with a user who has a restricted profile. An administrator passes every check, so the tests will not show the gaps.
Exercise
A controller is declared with sharing, but its SOQL query has no WITH USER_MODE. A user without read access to the Email field calls it. Is Email returned, and why?
Show answer
Yes. with sharing enforces only record-level sharing rules. Without user mode, the query does not check object permissions or field-level security, so Email is returned. Add WITH USER_MODE to the query, or strip the field with Security.stripInaccessible.