Sub topic 8
inherited sharing: letting the caller decide
This chapter assumes you know basic Apex classes and SOQL. It also assumes you know with sharing and without sharing, and the difference between record access and profile permissions.
The problem it solves
Some classes have no business reason to pick a sharing mode. A data-access class that queries accounts is one example. If you declare it with sharing, a trusted admin process can't use it to read everything. If you declare it without sharing, a user-facing controller can leak records. Declaring nothing is also risky, as the omitted-keyword chapter showed.
inherited sharing handles this case. The class takes on the sharing mode of whichever class calls it.
The declaration
public inherited sharing class AccountRepository {
public static List<Account> findByName(String name) {
return [SELECT Id, Name FROM Account WHERE Name = :name];
}
}The query in findByName has no fixed behavior. The caller determines what it returns.
How the mode is chosen
The mode depends on how execution reached the class:
- Called from a
with sharingclass, it runs with sharing. - Called from a
without sharingclass, it runs without sharing. - Called as the entry point of a transaction, it runs with sharing. Entry points include an
@AuraEnabledmethod, a Visualforce controller, an Apex REST endpoint, and theexecutemethod of a batch or scheduled job.
Worked example
Two callers use the same repository.
public with sharing class AccountController {
public static Integer countForUser(String name) {
// AccountRepository runs with sharing here
return AccountRepository.findByName(name).size();
}
}
public without sharing class AccountMaintenanceJob {
public static Integer countForAdminReport(String name) {
// AccountRepository runs without sharing here
return AccountRepository.findByName(name).size();
}
}Suppose a sales user can see 3 of the 10 accounts named Acme. AccountController.countForUser('Acme') returns 3. AccountMaintenanceJob.countForAdminReport('Acme') returns 10. The repository code is the same in both cases. Only the caller differs.
Behavior to remember
- The mode is set per call. The same class can run both ways in one transaction, depending on who invoked it.
- It applies to the class's own methods. If the repository calls a class that declares
without sharing, that class still runs without sharing. The call-chain rule from the previous chapter still holds. - It controls record-level sharing only. Object permissions and field-level security are not affected. Add
WITH USER_MODEorAccessLevel.USER_MODEfor those.
Common mistakes
- Using
inherited sharingand assuming it enforces sharing in every case. Awithout sharingcaller makes it bypass sharing. - Putting
inherited sharingon a class that must always behave one way, such as a class that must always see all records. Declare that classwithout sharingand document why. - Forgetting that a
without sharingcaller anywhere up the chain widens access for every inherited class below it.
Exercise
A utility class is declared inherited sharing. A scheduled job class declared without sharing calls it from its execute method. Which sharing mode does the utility class use? What mode would it use if the scheduled job class had no keyword and the job was the entry point?
Show answer
The utility class runs without sharing, because it takes the mode of its caller. If the job class had no keyword and was the entry point, it would run in system mode, so the utility class would also ignore record-level sharing. The utility class would only enforce sharing if it were itself the entry point, or if it were called from a with sharing class.