Created
July 26, 2020 15:42
-
-
Save Sunil02kumar/81a6e61ca74ad3f725462666c5e74009 to your computer and use it in GitHub Desktop.
Compare CRUD (OLS) for a profile in 2 different environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string sourceOrgDomainURL = 'https://xxxxzzzxxxxx.salesforce.com'; | |
string sourceOrgSessionId = UserInfo.getSessionID(); //specify source org sessionid URL | |
string targetOrgDomainURL = 'https://xxxvvvvvxxxxx.salesforce.com'; | |
//specify target org sessionid or access_token | |
string targetOrgSessionId ='00DxxxxxxxxxxxxxxxxxxxxeeeeeeeeeeeeezzzzzzzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbMD7q__aWFV3XmO3CNLdyqsY'; | |
string profileName = 'System Administrator'; | |
profileName = profileName.replaceAll(' ','+'); | |
//First fetch CRUD permission for profile from Source Org | |
string sourceEndPointURL = sourceOrgDomainURL+'/services/data/v48.0/query/?q=SELECT+Id,ParentId,SobjectType,PermissionsCreate,PermissionsRead,PermissionsEdit,PermissionsDelete,PermissionsViewAllRecords,PermissionsModifyAllRecords+FROM+ObjectPermissions+WHERE+ParentId+IN+(SELECT+PermissionSetId+FROM+PermissionSetAssignment+WHERE+PermissionSet.Profile.Name=\''+profileName+'\')+ORDER+BY+SobjectType'; | |
system.debug('***sourceEndPointURL:'+sourceEndPointURL); | |
HttpRequest reqForSourceOrg = new HttpRequest(); | |
reqForSourceOrg.setHeader('Authorization', 'Bearer ' + sourceOrgSessionId); | |
reqForSourceOrg.setHeader('Content-Type', 'application/json'); | |
reqForSourceOrg.setEndpoint(sourceEndPointURL); | |
reqForSourceOrg.setMethod('GET'); | |
Http h = new Http(); | |
HttpResponse resFromSourceOrg = h.send(reqForSourceOrg); | |
system.debug('***response from sorce org:'+resFromSourceOrg.getBody()); | |
String response=resFromSourceOrg.getBody(); | |
List<OLSWrapper> sourceOrgOLSList = new List<OLSWrapper>(); | |
sourceOrgOLSList = parseOLSJSON(response); | |
system.debug('****sourceOrgOLSList size:'+sourceOrgOLSList.size()); | |
//create Map with object and OLS settings | |
Map<string,OLSWrapper> sourceOrgObjOLSWMap = new Map<string,OLSWrapper>(); | |
for(OLSWrapper olsv : sourceOrgOLSList){ | |
sourceOrgObjOLSWMap.put(olsv.SobjectType,olsv); | |
} | |
system.debug('****sourceOrgObjOLSWMap size:'+sourceOrgObjOLSWMap.size()); | |
//Now fetch CRUD permission for profile from target Org | |
string targetEndPointURL = targetOrgDomainURL+'/services/data/v48.0/query/?q=SELECT+Id,ParentId,SobjectType,PermissionsCreate,PermissionsRead,PermissionsEdit,PermissionsDelete,PermissionsViewAllRecords,PermissionsModifyAllRecords+FROM+ObjectPermissions+WHERE+ParentId+IN+(SELECT+PermissionSetId+FROM+PermissionSetAssignment+WHERE+PermissionSet.Profile.Name=\''+profileName+'\')+ORDER+BY+SobjectType'; | |
system.debug('********targetEndPointURL:'+targetEndPointURL); | |
HttpRequest reqForTargetOrg = new HttpRequest(); | |
reqForTargetOrg.setHeader('Authorization', 'Bearer ' + targetOrgSessionId); | |
reqForTargetOrg.setHeader('Content-Type', 'application/json'); | |
reqForTargetOrg.setEndpoint(targetEndPointURL); | |
reqForTargetOrg.setMethod('GET'); | |
h = new Http(); | |
HttpResponse resFromTargetOrg = h.send(reqForTargetOrg); | |
system.debug('***response from target org:'+resFromTargetOrg.getBody()); | |
response=resFromTargetOrg.getBody(); | |
List<OLSWrapper> targetOLSList = new List<OLSWrapper>(); | |
targetOLSList = parseOLSJSON(response); | |
system.debug('****targetOLSList size:'+targetOLSList.size()); | |
//create Map with object and OLS settings | |
Map<string,OLSWrapper> targetOrgObjOLSWMap = new Map<string,OLSWrapper> (); | |
for(OLSWrapper olsv : targetOLSList){ | |
targetOrgObjOLSWMap.put(olsv.SobjectType,olsv); | |
} | |
system.debug('****targetOrgObjOLSWMap size:'+targetOrgObjOLSWMap.size()); | |
//now send result in csv format | |
string csvString ='Object Name,Source-Create,Source-Read,Source-Edit,Source-Delete,Source-View All,Source-Modify All All,Target-Create,Target-Read,Target-Edit,Target-Delete,Target-View All,Target-Modify All\n'; | |
for(string objName : sourceOrgObjOLSWMap.keyset()){ | |
system.debug('***objName:'+ objName); | |
OLSWrapper sOLSp = sourceOrgObjOLSWMap.get(objName); | |
OLSWrapper tOLSp = targetOrgObjOLSWMap.get(objName); | |
if(targetOrgObjOLSWMap.get(objName)!=null){ | |
csvString = csvString + sOLSp.SobjectType + ',' +sOLSp.PermissionsCreate + ','+sOLSp.PermissionsRead + ',' + sOLSp.PermissionsEdit + ',' + sOLSp.PermissionsDelete + ','+sOLSp.PermissionsViewAllRecords + ',' + sOLSp.PermissionsModifyAllRecords; | |
csvString = csvString + ',' +tOLSp.PermissionsCreate + ','+tOLSp.PermissionsRead + ',' + tOLSp.PermissionsEdit + ',' + tOLSp.PermissionsDelete + ','+tOLSp.PermissionsViewAllRecords + ',' + tOLSp.PermissionsModifyAllRecords +'\n'; | |
}else{ | |
csvString = csvString + sOLSp.SobjectType + ',' +sOLSp.PermissionsCreate + ','+sOLSp.PermissionsRead + ',' + sOLSp.PermissionsEdit + ',' + sOLSp.PermissionsDelete + ','+sOLSp.PermissionsViewAllRecords + ',' + sOLSp.PermissionsModifyAllRecords +'\n'; | |
} | |
} | |
//specify permissions which are in target org but not in source org | |
for(string objName : targetOrgObjOLSWMap.keyset()){ | |
if(sourceOrgObjOLSWMap.get(objName)==null){ | |
OLSWrapper tOLSp = targetOrgObjOLSWMap.get(objName); | |
csvString = csvString + tOLSp.SobjectType + ',,,,,,,' +tOLSp.PermissionsCreate + ','+tOLSp.PermissionsRead + ',' + tOLSp.PermissionsEdit + ',' + tOLSp.PermissionsDelete + ','+tOLSp.PermissionsViewAllRecords + ',' + tOLSp.PermissionsModifyAllRecords +'\n'; | |
} | |
} | |
system.debug('***csvString:'+csvString); | |
Messaging.EmailFileAttachment csvAtt = new Messaging.EmailFileAttachment(); | |
blob csvBlob = Blob.valueOf(csvString); | |
string csvFileName= profileName + ' : CRUD (OLS) permission comparison.csv'; | |
csvAtt.setFileName(csvFileName); | |
csvAtt.setBody(csvBlob); | |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
List<string> toAddresses= new List<string>{UserInfo.getUserEmail()}; | |
mail.setToAddresses(toAddresses); | |
mail.setSaveAsActivity(false); | |
mail.setSubject(profileName + ' : CRUD (OLS) permission comparison-'+system.now()); | |
mail.setPlainTextBody(sourceOrgDomainURL + ' and ' + targetOrgDomainURL + 'CRUD (OLS) comparison for ' + profileName); | |
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAtt}); | |
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); | |
public static List<OLSWrapper> parseOLSJSON(string Response){ | |
List<OLSWrapper> OLSList = new List<OLSWrapper>(); | |
JSONParser parser = JSON.createParser(Response); | |
OLSWrapper olsp ; | |
while (parser.nextToken() != null) { | |
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){ | |
String fieldName = parser.getText(); | |
if(fieldName == 'SobjectType'){ | |
olsp = new OLSWrapper(); | |
parser.nextToken(); | |
olsp.SobjectType=parser.getText(); | |
}else if(fieldName == 'PermissionsCreate') { | |
parser.nextToken(); | |
olsp.PermissionsCreate=parser.getText(); | |
}else if(fieldName == 'PermissionsRead') { | |
parser.nextToken(); | |
olsp.PermissionsRead=parser.getText(); | |
}else if(fieldName == 'PermissionsEdit') { | |
parser.nextToken(); | |
olsp.PermissionsEdit=parser.getText(); | |
}else if(fieldName == 'PermissionsDelete') { | |
parser.nextToken(); | |
olsp.PermissionsDelete=parser.getText(); | |
}else if(fieldName == 'PermissionsViewAllRecords') { | |
parser.nextToken(); | |
olsp.PermissionsViewAllRecords=parser.getText(); | |
}else if(fieldName == 'PermissionsModifyAllRecords') { | |
parser.nextToken(); | |
olsp.PermissionsModifyAllRecords=parser.getText(); | |
OLSList.add(olsp); | |
} | |
} | |
} | |
return OLSList; | |
} | |
public class OLSWrapper{ | |
public string SobjectType; | |
public string PermissionsCreate; | |
public string PermissionsRead; | |
public string PermissionsEdit; | |
public string PermissionsDelete; | |
public string PermissionsViewAllRecords; | |
public string PermissionsModifyAllRecords; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment