I have a large Salesforce application with hundreds of SOQL queries. Currently, the code uses Schema.DescribeFieldResult
data to determine if the logged in user has rights to each field in the SELECT clauses.
I am planning on changing the code to use the new Security.StripInnacessible method or the WITH SECURITY_ENFORCED
flag.
We are trying to use something like a “selector” layer to organize queries within a domain together, for maximum reuse. This means that sometimes the same query is run by a user via a VF UI where FLS would be important, and sometimes it is run by a trigger where FLS would be an obstacle to updating aggregates and other non-user data (see: Appleman article on security review process and non-user data)
My question is what is the best way to maximize SOQL query reuse while appropriately opting in and out of FLS checks based on the trigger context?
Here is some example code that I hope can be improved upon:
public static sobject getAccountDetails(id filterId) {
Object returnObject;
// really?
if (trigger.IsExecuting) {
returnObject = [SELECT id, name, AccountDollars__c
FROM Account WHERE id = :filterId
//WITH SECURITY_ENFORCED];
];
}
else {
returnObject = [SELECT id, name, AccountDollars__c
FROM Account WHERE id = :filterId
WITH SECURITY_ENFORCED];
}
return returnObject;
}