Skip to content

Commit

Permalink
Refactor permission check and set hasAccess = true for HH_CampaignDed…
Browse files Browse the repository at this point in the history
…upeBTN_TEST
  • Loading branch information
npsp-reedestockton committed Nov 18, 2023
1 parent 5ed8096 commit f1a3e1d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
59 changes: 42 additions & 17 deletions force-app/main/default/classes/HH_CampaignDedupeBTN_CTRL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ public without sharing class HH_CampaignDedupeBTN_CTRL {
//so we inform our action method to perform the update
private boolean updateSettingsWithID = false;

@TestVisible
public Boolean hasAccess {
get {
if (hasAccess == null) {
hasAccess = getCurrentUserHasAccess();
}
return hasAccess;
}
private set;
}

@TestVisible
private UTIL_Permissions perms {
get {
if (perms == null) {
perms = new UTIL_Permissions();
}

return perms;
}
set;
}

/*******************************************************************************************************
* @description Constructor
* @param controller StandardController to a Campaign
Expand Down Expand Up @@ -98,6 +121,10 @@ public without sharing class HH_CampaignDedupeBTN_CTRL {
public PageReference RunReport(){
Savepoint sp = Database.setSavepoint();
try {
if (!hasAccess) {
throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage);
}

string ActiveID = campaign.id;
ActiveID = ActiveID.substring(0,15);
string newPageUrl = '';
Expand Down Expand Up @@ -193,8 +220,6 @@ public without sharing class HH_CampaignDedupeBTN_CTRL {
********************************************************************************************************/
public static integer MarkDuplicatesFromList(ID campaignId, list<CampaignMember> listCM) {

validateAccessPermissions();

final String DUPE_STATUS_SUFFIX = System.Label.hhCmpDedupeStatus;

//check for the Household Duplicate status values we need
Expand Down Expand Up @@ -293,23 +318,23 @@ public without sharing class HH_CampaignDedupeBTN_CTRL {
return returnsize;
}

private static void validateAccessPermissions() {
if (!UTIL_Permissions.canUpdate('CampaignMember', 'Status', false)) {
throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage);
}

Set<String> cmsFields = new Set<String>{
'CampaignId',
'Label',
'HasResponded',
'SortOrder'
};
for (String cmsField : cmsFields) {
if (!(UTIL_Permissions.canRead('CampaignMemberStatus', cmsField, false) &&
UTIL_Permissions.canCreate('CampaignMemberStatus', cmsField, false))) {
throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage);
private Boolean getCurrentUserHasAccess() {
Boolean accessOK = false;

if (UTIL_Permissions.canUpdate('CampaignMember', 'Status', false)) {
Set<SObjectField> cmsFields = new Set<SObjectField>{
CampaignMemberStatus.fields.CampaignId,
CampaignMemberStatus.fields.Label,
CampaignMemberStatus.fields.HasResponded,
CampaignMemberStatus.fields.SortOrder
};
if ((perms.canRead(CampaignMemberStatus.getSObjectType(), cmsFields) &&
perms.canCreate(CampaignMemberStatus.getSObjectType(), cmsFields))) {
accessOK = true;
}
}

return accessOK;
}

/*******************************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private class HH_CampaignDedupeBTN_TEST {
Test.startTest();
ApexPages.StandardController sc = new ApexPages.StandardController(camp);
HH_CampaignDedupeBTN_CTRL deduper = new HH_CampaignDedupeBTN_CTRL(sc);
deduper.hasAccess = true;
deduper.RunReport();
Test.stopTest();

Expand Down

0 comments on commit f1a3e1d

Please sign in to comment.