Enforcing user mode for all database operations
In the preceding section, we saw how we could utilize the WITH SECURITY_ENFORCED
clause on a SOQL query to enforce permissions on our query with an error thrown if there is a field to which we have no access.
There are a number of problems with the use of the WITH SECURITY_ENFORCED
clause, including the following:
- Polymorphic fields are not taken into consideration
- Not all clauses are processed (the
WHERE
clause, for example, is not processed) - Only the first field exception is returned with no detailed error information
To help developers manage this more appropriately, Salesforce provided the ability to run SOQL, Salesforce Object Search Language (SOSL), and DML statements in user mode within your Apex code.
Running code within User Mode versus the default System Mode ensures that the following are always applied to the database operation:
- Object permissions
- Field-level security
- Sharing rules...