Now we are in a world where there are a lot of ways to hack
the data and hence Security is now 1st priority for the business especially
for banking, healthcare domains. Most of the security feature is handle by
Salesforce. However, the program also needs to ensure from his side to provide another layer of security. We can think below problem and now we have a solution to our problem
Problem- One of the common security features is Object and
Field level check. Previously to check read access in SOQL we need to check for
each field and that increases lots of code in our class. Check below code we
only have two fields but imagine we have 50+ fields in SOQL and then we need to
put check for each field.
if (Schema.sObjectType.Contact.fields.Email.isAccessible() && Schema.sObjectType.Contact.fields.Phone.isAccessible()) {
Contact c = [SELECT Email, Phone FROM Contact WHERE Id= :Id];
}
Code will
be hard to maintain at the same time if we add/remove a field from SOQL we also
need to update these checks. What if we remove the field from SOQL but forgot
to remove the check then the user might get unexpected results.
Solution- To overcome this, we
can use WITH SECURITY_ENFORCED in our SOQL and we don’t need to put these extra checks as WITH SECURITY_ENFORCED handle these things out of the box. Now the
same query using WITH SECURITY_ENFORCED tag will look like
Contact c = [SELECT Email, Phone FROM Contact WHERE Id= :Id WITH SECURITY_ENFORCED];
No comments:
Post a Comment