Sub topic 11
Enforcing permissions with user-mode queries and DML
This chapter assumes you can write basic Apex and SOQL. It also assumes you know that the sharing keywords control record-level access, and that profiles and permission sets control object and field access. The previous chapter covered that split. This one shows how to enforce the second half in code.
The gap that sharing leaves
A with sharing class limits which records a query returns. It does not check whether the user can read the object or a given field. Apex runs in system mode by default, so a with sharing query can still return a field the user has no permission to see.
User mode closes that gap. When you run a query or DML operation in user mode, Apex enforces the running user's object permissions and field-level security. It also applies the user's record-level sharing, whatever sharing keyword the class declares.
User-mode SOQL
Add WITH USER_MODE to a query:
public with sharing class ContactService {
public static List<Contact> getContacts() {
// with sharing limits the records; USER_MODE also enforces object and field permissions
return [SELECT Id, LastName, Email FROM Contact WITH USER_MODE];
}
}If the user cannot read Contact, or cannot read Email, the query throws a QueryException. It does not silently drop the field. The query either runs as the user sees the data or it fails.
User-mode DML
For DML, pass an access level to the Database methods:
public with sharing class ContactWriter {
public static void saveContacts(List<Contact> contacts) {
Database.update(contacts, AccessLevel.USER_MODE);
}
}If the user lacks edit permission on the object or on a field being written, the call throws a DmlException. You can use AccessLevel.USER_MODE with Database.insert, update, upsert, delete, and Database.query. You can use AccessLevel.SYSTEM_MODE to state the default explicitly.
How the pieces combine
| Setup | Record sharing | Object permissions and FLS |
|---|---|---|
| `with sharing`, plain query | Enforced | Not enforced |
| `without sharing`, plain query | Not enforced | Not enforced |
| Any keyword, user-mode query | Enforced | Enforced |
The last row is the main point. A user-mode operation enforces the running user's permissions even in a without sharing class. The operation's mode overrides the class keyword for that statement.
Handling the failures
Because user mode throws instead of filtering, catch the exception where you can give the user a clear message.
public with sharing class ContactSafeReader {
public static List<Contact> tryGetContacts() {
try {
return [SELECT Id, LastName, Email FROM Contact WITH USER_MODE];
} catch (QueryException e) {
// The user lacks access to Contact or to one of the fields
System.debug('Access problem: ' + e.getMessage());
return new List<Contact>();
}
}
}Common errors
- Assuming
with sharingchecks field-level security. It does not. - Adding
WITH USER_MODEto a query that selects a field the user cannot read, then being surprised by the exception. This is the intended behavior. - Mixing
WITH USER_MODEandWITH SECURITY_ENFORCEDin the same query. ChooseWITH USER_MODE, which also applies sharing and checks more cases.
Exercise
A without sharing class runs [SELECT Id, Email FROM Contact WITH USER_MODE]. The running user can see every Contact record but has no read access to the Email field. What happens?
Show answer
The query throws a QueryException. User mode enforces field-level security for the running user, and it overrides the class's without sharing keyword for that statement, so the query does not return Email.
Exercise
Which statement enforces the user's edit permissions on Contact: update contacts; or Database.update(contacts, AccessLevel.USER_MODE);?
Show answer
Database.update(contacts, AccessLevel.USER_MODE). The plain update statement runs in system mode, so it checks neither object permissions nor field-level security.