Sub topic 2
Sharing is not the same as CRUD and FLS
This chapter assumes you can read basic Apex and SOQL. It also assumes you know what profiles and permission sets are, and that you have seen object permissions (CRUD) and field-level security (FLS) in Setup. By the end, you should be able to say which of these controls a given Apex class enforces and which it ignores.
Two separate security layers
Salesforce decides what a user can see with two independent layers.
- Record access decides which rows a user can see or edit. It comes from organization-wide defaults, the role hierarchy, sharing rules, manual shares, and record ownership.
- Object and field access decides which objects and fields a user can touch at all. It comes from profiles and permission sets. Object permissions are create, read, edit, and delete (CRUD). Field-level security (FLS) sets whether each field is readable or editable.
A user needs both layers to pass. Having access to a record does not help if the profile cannot read the object. Having read access to the object does not help if the sharing model hides the record.
| Layer | Question it answers | Configured with |
|---|---|---|
| Sharing | Which records can this user see? | OWD, roles, sharing rules, ownership |
| CRUD | Can this user read, create, edit, or delete this object? | Profiles, permission sets |
| FLS | Can this user read or edit this field? | Profiles, permission sets |
What the sharing keywords control
Apex classes declare a sharing behavior. The keyword changes the first layer only.
with sharingmakes queries respect the running user's record access.without sharingmakes queries ignore it.inherited sharinguses the sharing setting of the calling class.- If you write no keyword, the class does not enforce sharing on its own. It follows the caller's setting when invoked from another class, and runs without sharing when it is the entry point. Always declare a keyword so the behavior is explicit.
None of these keywords check object permissions or FLS. A with sharing class still reads and writes every object and field that its code references, whatever the profile says.
public with sharing class SharingOnlyExample {
public static List<Account> getAccounts() {
// Sharing is enforced: only Accounts the user can see come back.
// CRUD and FLS are not enforced: Industry is returned even if the
// user's profile has no read access to that field.
return [SELECT Id, Name, Industry FROM Account LIMIT 50];
}
}Suppose a support agent can see an Account through the role hierarchy, but FLS hides Industry from them. This query still returns Industry. The class is with sharing, so the agent only sees the records they are allowed to see. Their field restriction was never checked.
Why the gap matters
Apex runs in system mode by default, as the previous chapter explained. In that mode, the code can reach any object and field. The sharing keyword narrows the rows. It does not narrow the columns or the operations.
This leads to two common mistakes.
- Assuming
with sharingis a full security setting. Developers add the keyword, see that the code "respects security", and ship it. Users can then read fields or create records that their profile forbids. - Assuming the UI limits apply to your code. Page layouts and Lightning components hide fields from users. An
@AuraEnabledmethod called from a component can still return fields that the layout hides, because the method is not bound by the layout.
The diagram below shows which checks apply in each mode.
Seeing the difference
You can confirm the gap with a short anonymous Apex test. Run it as a user whose profile lacks read access to Account.Industry, for example through a test that uses System.runAs. The next chapters cover that technique in detail.
public with sharing class FlsGapDemo {
public static Boolean canSeeIndustry() {
List<Account> rows = [SELECT Id, Industry FROM Account LIMIT 1];
if (rows.isEmpty()) {
return false;
}
// Describe tells us what the user is allowed to do.
Boolean allowedByFls = Schema.sObjectType.Account.fields.Industry.isAccessible();
// The query succeeded regardless, so the two answers can differ.
return allowedByFls;
}
}If isAccessible() returns false while the query still succeeded, the code has read a field the user is not permitted to read. User mode closes that gap without a manual check for each field.
Key points
- Sharing controls which records a user sees. CRUD and FLS control which objects and fields they can use.
- The
with sharing,without sharing, andinherited sharingkeywords affect sharing only. - System mode skips CRUD and FLS, even in a
with sharingclass. - User mode checks all three layers, which is why the rest of this tutorial uses it.
Exercise
A class is declared with sharing and runs SELECT Id, Salary__c FROM Employee__c. The running user can see the Employee_c records through the role hierarchy, but their profile has no read access to `Salaryc. Does the query return Salary_c`, and why?
Show answer
Yes, the query returns it. with sharing enforces only record access, and the user can see those records. Apex in system mode does not check FLS, so the missing field permission is ignored. Adding WITH USER_MODE to the query would make it fail instead.