Sub topic 3
What user mode enforces
This chapter assumes you know basic Apex classes, SOQL and DML, profiles and permission sets, and the difference between object permissions (CRUD) and field-level security (FLS). Earlier chapters showed that Apex runs in system mode by default and that the with sharing keyword controls only record sharing. Here you will see exactly what user mode adds.
The three checks
User mode runs an operation with the permissions of the current user. For a query or DML statement, Salesforce enforces three things:
- Object permissions. The user needs read access to query an object, and create, edit, or delete access for the matching DML.
- Field-level security. The user needs read access to every field the query references, and edit access (or create access, for inserts) to every field the DML writes.
- Sharing. Only records the user can see through ownership, the role hierarchy, sharing rules, and manual shares are returned or modified.
The sharing check does not depend on the class declaration. A class marked without sharing still gets sharing enforced on an operation that runs in user mode.
| Check | Default (system mode) | `with sharing` class | User mode operation |
|---|---|---|---|
| Sharing rules | Not enforced | Enforced | Enforced |
| Object permissions (CRUD) | Not enforced | Not enforced | Enforced |
| Field-level security | Not enforced | Not enforced | Enforced |
A static query in user mode
Add WITH USER_MODE to a static SOQL query to apply all three checks.
public with sharing class AccountService {
public static List<Account> getAccounts() {
return [
SELECT Id, Name, Industry
FROM Account
WHERE Industry != null
WITH USER_MODE
LIMIT 50
];
}
}Suppose the running user can read Account but has no read access to Industry. The query throws an exception, because Industry appears in two places. It is in the SELECT list, and it is also in the WHERE clause.
Fields in the WHERE clause count
This is the main difference from older approaches. Earlier tools checked only the fields you selected. User mode checks every field the query touches, including fields used in WHERE, ORDER BY, and relationship paths. That closes a leak where a user could learn the value of a hidden field by filtering on it and watching which records came back.
Fail or filter
User mode does not silently remove data you cannot see.
- Object and field violations stop the operation with an exception. Salesforce does not return a partial result with some fields removed.
- Sharing filters. Records the user cannot see are left out of the result. This is the same as
with sharingbehavior and does not raise an error.
So a user with full field access but limited record access gets fewer rows. A user who lacks access to one referenced field gets an exception. The next chapters cover how to handle those exceptions.
What user mode does not cover
User mode applies to the operation you mark, not to the whole transaction.
- Statements without
WITH USER_MODEoras userstill run in system mode, even in the same method. - Code that runs as a side effect of a user mode DML statement, such as a trigger, still runs in system mode. The user's permissions are checked on the statement you wrote, not on what your triggers do afterward.
- Your own logic is not checked. If you read a field in a system mode query and then return it to the user, nothing stops you. User mode protects the data access, not what you do with the result.
Exercise
Exercise
A class is declared without sharing. One method runs [SELECT Id, Name FROM Contact WITH USER_MODE]. The running user can read Contact and Name, but can see only half of the Contact records through sharing. What does the query return, and does the class declaration change that?
Show answer
The query returns only the Contacts the user can see through sharing, with no error, because object permission and FLS pass and sharing filters records. The without sharing declaration does not change this, because sharing is enforced for any operation that runs in user mode.
Exercise
A user can read Account and Name but not Industry. Their query is [SELECT Id, Name FROM Account WHERE Industry = 'Energy' WITH USER_MODE]. Does it succeed? Why?
Show answer
No. User mode checks field-level security on every field the query references, including fields in the WHERE clause. The user cannot read Industry, so the query throws an exception even though Industry is not selected.