apex-enterprise-patterns / fflib-apex-common

Common Apex Library supporting Apex Enterprise Patterns and much more!
BSD 3-Clause "New" or "Revised" License
899 stars 514 forks source link

Add `withUserMode()` and `withSystemMode()` to SObjectSelector and QueryFactory #435

Closed chazwatkins closed 1 year ago

chazwatkins commented 1 year ago

Add withUserMode() and withSystemMode() to fflib_SObjectSelector and fflib_QueryFactory

fflib_QueryFactory

Specify WITH USER_MODE

fflib_QueryFactory qf = new fflib_QueryFactory(Account.SObjectType);

qf.withUserMode();
qf.getFLSEnforcement(); // USER_MODE

// OR

qf.setFLSEnforcement(fflib_QueryFactory.FLSEnforcement.USER_MODE);
qf.getFLSEnforcement(); // USER_MODE

Specify WITH SYSTEM_MODE

Salesforce uses SYSTEM_MODE as the default operation mode when not specified in the query. withSystemMode() allows users to make their queries explicit if desired.

fflib_QueryFactory qf = new fflib_QueryFactory(Account.SObjectType);

qf.withSystemMode();
qf.getFLSEnforcement(); // SYSTEM_MODE

// OR

qf.setFLSEnforcement(fflib_QueryFactory.FLSEnforcement.SYSTEM_MODE);
qf.getFLSEnforcement(); // SYSTEM_MODE

Switch back to LEGACY or NONE

fflib_QueryFactory qf = new fflib_QueryFactory(Account.SObjectType);

qf.withSystemMode();
qf.getFLSEnforcement(); // SYSTEM_MODE

qf.setEnforceFLS(true);
qf.getFLSEnforcement(); // LEGACY

// OR

qf.setEnforceFLS(FLSEnforcement.LEGACY);

// OR

qf.setEnforceFLS(false);
qf.getFLSEnforcement(); // NONE

// OR

qf.setEnforceFLS(FLSEnforcement.NONE);

fflib_SObjectSelector

isEnforcingFLS() and isEnforcingCRUD() now consider the DataAccess to determine their result instead of directly returning the values of m_enforceFLS and m_enforceCRUD. This allows the user to get the correct state from the methods, regardless if they are using legacy enforcement or one of the new operation modes.

Specify USER_MODE

AccountSelector selector = new AccountSelector();

selector.withUserMode();

selector.isEnforcingCRUD(); // true
selector.isEnforcingFLS(); // true

Specify SYSTEM_MODE

AccountSelector selector = new AccountSelector();

selector.withSystemMode();

selector.isEnforcingCRUD(); // false
selector.isEnforcingFLS(); // false

Switch back to LEGACY

AccountSelector selector = new AccountSelector();

selector.withSystemMode();

selector.isEnforcingCRUD(); // false
selector.isEnforcingFLS(); // false
selector.getDataAccess(); // SYSTEM_MODE

selector.enforceFLS();
selector.isEnforcingCRUD(): // true
selector.isEnforcingFLS(); // true
selector.getDataAccess(); // LEGACY

// OR

selector.setDataAccess(fflib_SObjectSelector.DataAccess.LEGACY);
selector.getDataAccess(); // LEGACY

Add

fflib_QueryFactory

fflib_SObjectSelector

Change

fflib_QueryFactoryTest

fflib_SObjectSelector

fflib_SObjectSelectorTest


This change is Reviewable

chazwatkins commented 1 year ago

@daveespo As requested, I incorporated the changes from #434 into #419. Let me know if you have any questions.

chazwatkins commented 1 year ago

Hi @daveespo - Should I update this pull request to include the builder style methods in master? Or has it been decided to not include them in the API?

daveespo commented 1 year ago

Hi @chazwatkins -- I didn't explicitly close out this PR -- it was implicitly closed when I merged the branch it targeted. Can you reopen it and target it at master? There is some good stuff in here I'd like to merge. I'm not sure if the withSystemMode and withUserMode convenience methods are going to make it but there are some other things in here that need to come over (i.e. getFLSEnforcement() on QueryFactory, etc.)

Don't go changing anything in this branch yet. First, we want to get PR #437 and your #436 merged in .. and those will create merge conflicts with this branch .. let's sort out what we want to keep and what should get removed on this PR after those other two PRs are merged

chazwatkins commented 1 year ago

Sounds good. I'll wait for the other PRs to get merged and submit this one to master