Sub topic 10
Sharing is not CRUD or field-level security
This chapter assumes you can read basic Apex and SOQL. It also assumes you know organization-wide defaults, roles, and sharing rules, and that you know what profiles and permission sets are. The previous chapters showed that with sharing makes a class respect the running user's record access. This chapter covers what that keyword does not do.
Three separate layers
Salesforce checks access in three layers. Each answers a different question.
| Layer | Question | Controlled by |
|---|---|---|
| Object permissions (CRUD) | Can the user create, read, edit, or delete this type of record? | Profiles and permission sets |
| Field-level security (FLS) | Can the user see or edit this field? | Profiles and permission sets |
| Record-level sharing | Can the user see this particular record? | Organization-wide defaults, roles, sharing rules, manual shares |
The sharing keywords (with sharing, without sharing, inherited sharing) affect only the third layer. Apex runs in system mode by default, so object permissions and FLS are not applied to your code, whatever keyword the class declares.
What this looks like in code
Suppose a user's profile has no read access to Contact.Email. The class below still returns that field.
public with sharing class ContactReader {
public static List<Contact> getContacts() {
// with sharing filters the rows to records the user can see.
// It does not remove fields the user has no permission to read.
return [SELECT Id, LastName, Email FROM Contact];
}
}The user sees only the contacts their sharing access allows. For each of those contacts, the query still returns Email. The same applies to DML. A with sharing class can update a field the user cannot edit, as long as the user has edit access to the record.
The reverse also holds. A user with full object and field access sees only the records that sharing grants them. Neither layer replaces the other.
Enforcing CRUD and FLS
You have two main options.
- Add user-mode operations to your queries and DML. The next chapter covers them in detail.
- Check permissions yourself with the
Schemadescribe methods, for exampleSchema.sObjectType.Contact.fields.Email.isAccessible().
public with sharing class SafeContactReader {
public static List<Contact> getContacts() {
if (!Schema.sObjectType.Contact.isAccessible()
|| !Schema.sObjectType.Contact.fields.Email.isAccessible()) {
throw new System.NoAccessException();
}
return [SELECT Id, LastName, Email FROM Contact];
}
}Manual checks are verbose and easy to forget for a single field. User-mode SOQL (WITH USER_MODE) and Database.update(records, AccessLevel.USER_MODE) enforce the checks for you. They also enforce record sharing for that operation, whatever the class's keyword says.
Common mistakes
- Assuming
with sharinghides fields the user cannot read. It only filters records. - Assuming
without sharingalso skips CRUD and FLS checks. Those checks were never applied in system mode. It changes record visibility only. - Adding
with sharingto fix a field-exposure finding. You need field checks or user mode.
Exercise
A with sharing class runs [SELECT Id, Salary__c FROM Employee__c] for a user who has no read access to Salary__c but can see some Employee_c records through sharing. Does the query return `Salary_c`, and what would you change to prevent it?
Show answer
Yes, it returns Salary__c for the records the user can see, because with sharing enforces only record-level sharing, not field-level security. Add WITH USER_MODE to the query, or check Schema.sObjectType.Employee__c.fields.Salary__c.isAccessible() before querying. In user mode the query fails with a security exception instead of returning the field.