Last active
February 27, 2024 12:46
-
-
Save sudikrt/f8157172cef3ad1223280cd6f62a6a10 to your computer and use it in GitHub Desktop.
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
public with sharing class PermissionExtractorHelper { | |
private static PermissionExtractorHelper handler; | |
String email_address = '[email protected]'; | |
private PermissionExtractorHelper() { | |
} | |
public static PermissionExtractorHelper getInstance() { | |
if(handler == null) { | |
handler = new PermissionExtractorHelper(); | |
} | |
return handler; | |
} | |
public void getAllPermissionSets() { | |
//Get permission sets | |
Map<String, Schema.DescribeFieldResult> standard_permissions_permissionset = getStandardPermissionForPermissionSet(); | |
Map<Id, PermissionSet> permissionSetsMap = new Map<Id, PermissionSet> (getPermissionSets(standard_permissions_permissionset)); | |
List<PermissionSet> permission_sets = getPermissionSets(standard_permissions_permissionset); | |
//Get all custom permission | |
Map<id, List<String>> entity_id_to_custom_permissions = getAllCustomPermission(permission_sets); | |
//Get Enabled Apex class | |
Map<Id, List<String>> enabledClassesForPermissionMap = getAllEnabledApexClasss(permission_sets); | |
//Get Enabled Apex Page | |
Map<Id, List<String>> enabledPageForPermissionMap = getAllEnabledApexPages(permission_sets); | |
//Get All Object Permission | |
Map<String, String> object_permissions = getObejctPermissionAttributes(); | |
Map<Id, List<ObjectPermissions>> assigendObjectPermission = getPermissionSetAssignedObjPermission(object_permissions, permission_sets); | |
//Get All the user Assignment | |
Map<Id, List<User>> permissionSetAssignedUserMap = getPermissionSetAssignment(permission_sets); | |
Map<Id, Map<String, Boolean>> stdPermissionSetPermissionAssigned = new Map<Id, Map<String, Boolean>>(); | |
Map<Id, List<String>> customPermissionAssignedtoPermissionSet = new Map<Id, List<String>>(); | |
//Prepare the data for every permission set | |
for(PermissionSet p :permission_sets) { | |
Map<String, Boolean> tempStdPermAssignedMap = new Map<String, Boolean>(); | |
List<String> tempCustPermAssignedList = new List<String>(); | |
for(String standard_permission :standard_permissions_permissionset.keySet()) { | |
Boolean permission_assigned = (Boolean) p.get(standard_permission); | |
tempStdPermAssignedMap.put(standard_permission, permission_assigned); | |
} | |
stdPermissionSetPermissionAssigned.put(p.Id, tempStdPermAssignedMap); | |
if(entity_id_to_custom_permissions.containsKey(p.Id)) { | |
tempCustPermAssignedList = entity_id_to_custom_permissions.get(p.Id); | |
customPermissionAssignedtoPermissionSet.put(p.Id, tempCustPermAssignedList); | |
} | |
} | |
String permission_text = ''; | |
String pemLabel = ''; | |
String pemVal = ''; | |
List<String> objectPermissions_list = new List<String>(object_permissions.keySet()); | |
for(PermissionSet eachPermission :permission_sets) { | |
permission_text += 'Permission set Name :,'; | |
permission_text += eachPermission.Label; | |
permission_text += '\n'; | |
if(stdPermissionSetPermissionAssigned.containsKey(eachPermission.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Standard permissions:\n'; | |
permission_text += 'Label, Name, Value\n'; //HelpText, | |
for (String key :stdPermissionSetPermissionAssigned.get(eachPermission.Id).keySet()) { | |
if(standard_permissions_permissionset.containsKey(key)) { | |
pemLabel += standard_permissions_permissionset.get(key).getLabel() + ','; | |
} else { | |
pemLabel += ','; | |
} | |
/*if(standard_permissions_permissionset.containsKey(key)) { | |
Schema.DescribeFieldResult ps_field_describe = standard_permissions_permissionset.get(key); | |
pemLabel += ps_field_describe.getLabel() + ','; | |
//pemLabel += String.isNotBlank(ps_field_describe.getInlineHelpText()) ? ps_field_describe.getInlineHelpText() : '' + ','; | |
} else { | |
pemLabel += ','; | |
}*/ | |
pemLabel += key + ','; | |
pemLabel += stdPermissionSetPermissionAssigned.get(eachPermission.Id) | |
.get(key) + '\n'; | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(customPermissionAssignedtoPermissionSet.containsKey(eachPermission.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Custom permissions:\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
pemVal = String.join(customPermissionAssignedtoPermissionSet.get(eachPermission.Id), ','); | |
permission_text += pemVal; | |
permission_text += '\n'; | |
} | |
if(permissionSetAssignedUserMap.containsKey(eachPermission.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Assigned users :\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
Integer i = 0; | |
for (User eachUser :permissionSetAssignedUserMap.get(eachPermission.Id)) { | |
i++; | |
pemLabel += eachUser.Name; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(assigendObjectPermission.containsKey(eachPermission.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Object Permission :'; | |
permission_text += '\n'; | |
permission_text += 'SobjectType,' + String.join(objectPermissions_list, ',') + '\n'; | |
for (ObjectPermissions eachPerm :assigendObjectPermission.get(eachPermission.Id)) { | |
String tempText = eachPerm.SobjectType + ','; | |
Integer i = 0; | |
for (String eachStr :objectPermissions_list) { | |
i++; | |
tempText += eachPerm.get(eachStr); | |
if(i >= objectPermissions_list.size()) { | |
tempText += '\n'; | |
} else { | |
tempText += ','; | |
} | |
} | |
permission_text += tempText; | |
} | |
permission_text += '\n'; | |
} | |
if(enabledClassesForPermissionMap.containsKey(eachPermission.Id)) { | |
permission_text += 'Apex classes :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachClass :enabledClassesForPermissionMap.get(eachPermission.Id)) { | |
i++; | |
pemLabel += eachClass; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(enabledPageForPermissionMap.containsKey(eachPermission.Id)) { | |
permission_text += 'Apex pages :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachPage :enabledPageForPermissionMap.get(eachPermission.Id)) { | |
i++; | |
pemLabel += eachPage; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
permission_text += '\n'; | |
permission_text += '----------End------------'; | |
permission_text += '\n'; | |
} | |
sendEmail (permission_text); | |
} | |
private void sendEmail(String permission_text) { | |
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
efa.setFileName('permissions.csv'); | |
Blob permissions_content_blob = Blob.valueof(permission_text); | |
efa.setBody(permissions_content_blob); | |
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); | |
email.setSubject('Permissions Export'); | |
email.setToAddresses(new List<String>{ email_address }); | |
email.setPlainTextBody('Your permissions export'); | |
email.setFileAttachments(new Messaging.EmailFileAttachment [] { | |
efa}); | |
Messaging.sendEmail(new Messaging.SingleEmailMessage [] { | |
email}); | |
} | |
private Map<Id, List<User>> getPermissionSetAssignment(List<PermissionSet> permSet) { | |
List<PermissionSetAssignment> assignments = [SELECT Id, PermissionSetId, AssigneeId FROM PermissionSetAssignment where PermissionSetId in :permSet]; | |
Map<Id, List<User>> permission_sets_map = new Map<Id, List<User>>(); | |
Set<Id> userAssignedId = new Set<Id>(); | |
for (PermissionSetAssignment eachAssignment :assignments) { | |
userAssignedId.add(eachAssignment.AssigneeId); | |
} | |
Map<Id, User> users = new Map<Id, User>([SELECT Id, Name, ProfileId FROM User WHERE IsActive = true AND Id in :userAssignedId]); | |
for(PermissionSetAssignment pas :assignments) { | |
List<User> user_in_ps = new List<User>(); | |
if(permission_sets_map.containsKey(pas.PermissionSetId)) { | |
user_in_ps = permission_sets_map.get(pas.PermissionSetId); | |
} | |
if(users.containsKey(pas.AssigneeId)) { | |
user_in_ps.add(users.get(pas.AssigneeId)); | |
} | |
permission_sets_map.put(pas.PermissionSetId, user_in_ps); | |
} | |
return permission_sets_map; | |
} | |
private List<PermissionSet> getPermissionSets(Map<String, Schema.DescribeFieldResult> standard_permissions_permissionset) { | |
List<String> permissions_List = new List<String>(standard_permissions_permissionset.keySet()); | |
String query = 'SELECT Id, Label, ' + String.join(permissions_list, ',') + ' FROM PermissionSet WHERE IsOwnedByProfile = false AND Label=\'DreamInvest\''; | |
return (List<PermissionSet>) Database.query(query); | |
} | |
private Map<String, Schema.DescribeFieldResult> getStandardPermissionForPermissionSet() { | |
Map<String, Schema.DescribeFieldResult> standard_permissions_permissionset = new Map<String, Schema.DescribeFieldResult>(); | |
Schema.DescribeSObjectResult ps_describe = PermissionSet.sObjectType.getDescribe(); | |
for(Schema.SObjectField ps_field :ps_describe.fields.getMap().values()) { | |
Schema.DescribeFieldResult ps_field_describe = ps_field.getDescribe(); | |
if(ps_field_describe.getName().startsWith('Permissions')) { | |
standard_permissions_permissionset.put(ps_field_describe.getName(), ps_field_describe); | |
} | |
} | |
return standard_permissions_permissionset; | |
} | |
private Map<Id, List<ObjectPermissions>> getPermissionSetAssignedObjPermission(Map<String, String> object_permissions, List<PermissionSet> permSet) { | |
List<String> objectPermissions_list = new List<String>(object_permissions.keySet()); | |
String query = 'SELECT Id, ParentId, SObjectType, ' + String.join(objectPermissions_list, ',') + ' FROM ObjectPermissions ' | |
+ 'WHERE ParentId in : permSet '; | |
List<ObjectPermissions> objectPermissionsList = (List<ObjectPermissions>) Database.query(query); | |
Map<Id, List<ObjectPermissions>> assigendObjectPermission = new Map<Id, List<ObjectPermissions>>(); | |
for (ObjectPermissions eachObjectPermission :objectPermissionsList) { | |
if(assigendObjectPermission.containsKey(eachObjectPermission.ParentId)) { | |
assigendObjectPermission.get(eachObjectPermission.ParentId).add(eachObjectPermission); | |
} else { | |
assigendObjectPermission.put(eachObjectPermission.ParentId, new List<ObjectPermissions>{ eachObjectPermission }); | |
} | |
} | |
return assigendObjectPermission; | |
} | |
private Map<String, String> getObejctPermissionAttributes() { | |
Map<String, String> object_permissions = new Map<String, String>(); | |
for(Schema.SObjectField objectPerm_field :Schema.sObjectType.ObjectPermissions.fields.getMap().values()) { | |
Schema.DescribeFieldResult objectPerm_field_describe = objectPerm_field.getDescribe(); | |
if(objectPerm_field_describe.getName().startsWith('Permissions')) { | |
object_permissions.put(objectPerm_field_describe.getName(), objectPerm_field_describe.getLabel()); | |
} | |
} | |
return object_permissions; | |
} | |
private Map<Id, SetupEntityAccess> getSetupEntityAccesByType(String entityPype, List<PermissionSet> permSet) { | |
return new Map<Id, SetupEntityAccess>([SELECT Id, SetupEntityId, ParentId | |
FROM SetupEntityAccess | |
WHERE ParentId in :permSet | |
AND SetupEntityType = :entityPype]); | |
} | |
private Map<id, List<String>> getAllEnabledApexClasss(List<PermissionSet> permSet) { | |
Map<id, List<String>> entity_id_to_custom_permissions = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('ApexClass', permSet); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, ApexClass> apexClassMap = new Map<Id, ApexClass>([SELECT Id, Name FROM ApexClass where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<String> tempList; | |
if(entity_id_to_custom_permissions.containsKey(eachAccess.ParentId)) { | |
tempList = entity_id_to_custom_permissions.get(eachAccess.ParentId); | |
} else { | |
tempList = new List<String>(); | |
} | |
if(apexClassMap.containsKey(eachAccess.SetupEntityId)) { | |
tempList.add(apexClassMap.get(eachAccess.SetupEntityId).Name); | |
} | |
entity_id_to_custom_permissions.put(eachAccess.ParentId, tempList); | |
} | |
return entity_id_to_custom_permissions; | |
} | |
private Map<id, List<String>> getAllEnabledApexPages(List<PermissionSet> permSet) { | |
Map<id, List<String>> entity_id_to_custom_permissions = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('ApexPage', permSet); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, ApexPage> apexPageMap = new Map<Id, ApexPage>([SELECT Id, Name FROM ApexPage where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<String> tempList; | |
if(entity_id_to_custom_permissions.containsKey(eachAccess.ParentId)) { | |
tempList = entity_id_to_custom_permissions.get(eachAccess.ParentId); | |
} else { | |
tempList = new List<String>(); | |
} | |
if(apexPageMap.containsKey(eachAccess.SetupEntityId)) { | |
tempList.add(apexPageMap.get(eachAccess.SetupEntityId).Name); | |
} | |
entity_id_to_custom_permissions.put(eachAccess.ParentId, tempList); | |
} | |
return entity_id_to_custom_permissions; | |
} | |
private Map<id, List<String>> getAllCustomPermission(List<PermissionSet> permSet) { | |
Map<id, List<String>> entity_id_to_custom_permissions = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('CustomPermission', permSet); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, CustomPermission> customPermMap = new Map<Id, CustomPermission>([SELECT Id, DeveloperName, MasterLabel FROM CustomPermission where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<Id> custom_permissions_list; | |
if(entity_id_to_custom_permissions.containsKey(eachAccess.ParentId)) { | |
custom_permissions_list = entity_id_to_custom_permissions.get(eachAccess.ParentId); | |
} else { | |
custom_permissions_list = new List<String>(); | |
} | |
if(customPermMap.containsKey(eachAccess.SetupEntityId)) { | |
custom_permissions_list.add(customPermMap.get(eachAccess.SetupEntityId).MasterLabel); | |
} | |
entity_id_to_custom_permissions.put(eachAccess.ParentId, custom_permissions_list); | |
} | |
return entity_id_to_custom_permissions; | |
} | |
} |
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
public class ProfilePermissionExtractionHelper { | |
private static ProfilePermissionExtractionHelper handler; | |
private ProfilePermissionExtractionHelper() { | |
} | |
String email_address = '[email protected]'; | |
public static ProfilePermissionExtractionHelper getInstance() { | |
if(handler == null) { | |
handler = new ProfilePermissionExtractionHelper(); | |
} | |
return handler; | |
} | |
private void getAllProfiles() { | |
List<Profile> profileList = [Select Id, Name from Profile]; | |
} | |
public void getProfileDetailsByProfile (Profile currentProfile, String parentId) { | |
//Get the standard profile permissions | |
Map<String, String> standard_permissions_profile = new Map<String, String>(); | |
Schema.DescribeSObjectResult profile_describe = Profile.sObjectType.getDescribe(); | |
for(Schema.SObjectField profile_field :profile_describe.fields.getMap().values()) { | |
Schema.DescribeFieldResult profile_field_describe = profile_field.getDescribe(); | |
if(profile_field_describe.getName().startsWith('Permissions')) { | |
standard_permissions_profile.put(profile_field_describe.getName(), profile_field_describe.getLabel()); | |
} | |
} | |
List<String> permissions_List = new List<String>(standard_permissions_profile.keySet()); | |
String query = 'SELECT Id, Name, ' + String.join(permissions_list, ',') + ' FROM Profile WHERE Id = \'' + currentProfile.Id + '\''; | |
List <Profile> profileList = (List<Profile>) Database.query(query); | |
Map<Id,Profile> profileMap = new Map <Id, Profile> (profileList); | |
//Get custom permissions and their assignments to profile sand permission sets | |
Map<id, List<String>> entity_id_to_custom_permissions = new Map<Id, List<String>>(); | |
List<CustomPermission> custom_permissions = [SELECT Id, DeveloperName, MasterLabel, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM CustomPermission]; | |
for(CustomPermission custom_permission :custom_permissions) { | |
for(SetupEntityAccess entity_access :custom_permission.SetupEntityAccessItems) { | |
List<Id> custom_permissions_list; | |
if(entity_id_to_custom_permissions.containsKey(entity_access.Parent.ProfileId)) { | |
custom_permissions_list = entity_id_to_custom_permissions.get(entity_access.Parent.ProfileId); | |
} else { | |
custom_permissions_list = new List<String>(); | |
} | |
custom_permissions_list.add(custom_permission.MasterLabel); | |
entity_id_to_custom_permissions.put(entity_access.Parent.ProfileId, custom_permissions_list); | |
} | |
} | |
// Apex classes | |
Map<Id, List<String>> enabledClassesForProfileMap = new Map<Id, List<String>>(); | |
List<ApexClass> enabledApexClasses = [SELECT Id, Name, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM ApexClass]; | |
for (ApexClass eachClass :enabledApexClasses) { | |
for(SetupEntityAccess entityAccess :eachClass.SetupEntityAccessItems) { | |
List<String> tempList; | |
if(enabledClassesForProfileMap.containsKey(entityAccess.Parent.ProfileId)) { | |
tempList = enabledClassesForProfileMap.get(entityAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
tempList.add(eachClass.Name); | |
enabledClassesForProfileMap.put(entityAccess.Parent.ProfileId, tempList); | |
} | |
} | |
// ApexPage | |
Map<Id, List<String>> enabledPagesForProfileMap = new Map<Id, List<String>>(); | |
List<ApexPage> enabledApexPages = [SELECT Id, Name, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM ApexPage]; | |
for (ApexPage eachPage :enabledApexPages) { | |
for(SetupEntityAccess entityAccess :eachPage.SetupEntityAccessItems) { | |
List<String> tempList; | |
if(enabledPagesForProfileMap.containsKey(entityAccess.Parent.ProfileId)) { | |
tempList = enabledPagesForProfileMap.get(entityAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
tempList.add(eachPage.Name); | |
enabledPagesForProfileMap.put(entityAccess.Parent.ProfileId, tempList); | |
} | |
} | |
//get All object permision | |
Map<String, String> object_permissions_profile = new Map<String, String>(); | |
for(Schema.SObjectField objectPerm_field :Schema.sObjectType.ObjectPermissions.fields.getMap().values()) { | |
Schema.DescribeFieldResult objectPerm_field_describe = objectPerm_field.getDescribe(); | |
if(objectPerm_field_describe.getName().startsWith('Permissions')) { | |
object_permissions_profile.put(objectPerm_field_describe.getName(), objectPerm_field_describe.getLabel()); | |
} | |
} | |
Set<Id> profileIds = profileMap.keySet(); | |
List<String> objectPermissions_list = new List<String>(object_permissions_profile.keySet()); | |
query = 'SELECT Id, Parent.ProfileId, SObjectType, ' + String.join(objectPermissions_list, ',') + ' FROM ObjectPermissions ' | |
+ 'WHERE parentid in (select id from permissionset where PermissionSet.ProfileId in : profileIds)'; | |
System.debug('query :' + query); | |
List<ObjectPermissions> objectPermissionsList = (List<ObjectPermissions>) Database.query(query); | |
Map<Id, List<ObjectPermissions>> profileAssigendObjectPermission = new Map<Id, List<ObjectPermissions>>(); | |
for (ObjectPermissions eachObjectPermission :objectPermissionsList) { | |
if(profileAssigendObjectPermission.containsKey(eachObjectPermission.Parent.ProfileId)) { | |
profileAssigendObjectPermission.get(eachObjectPermission.Parent.ProfileId).add(eachObjectPermission); | |
} else { | |
profileAssigendObjectPermission.put(eachObjectPermission.Parent.ProfileId, new List<ObjectPermissions>{ eachObjectPermission }); | |
} | |
} | |
//Query permission set assignments | |
Map<Id, User> users = new Map<Id, User>([SELECT Id, Name, ProfileId FROM User WHERE IsActive = true]); | |
Map<Id, List<User>> userAssignedToProfileMap = new Map<Id, List<User>>(); | |
Map<Id, Map<String, Boolean>> profileStdPermissionAssigned = new Map<Id, Map<String, Boolean>>(); | |
Map<Id, List<String>> profileCustPermissionAssigned = new Map<Id, List<String>>(); | |
for (User eachUser :users.values()) { | |
List<User> tempList = new List<User>(); | |
if(userAssignedToProfileMap.containsKey(eachUser.ProfileId)) { | |
tempList = userAssignedToProfileMap.get(eachUser.ProfileId); | |
} | |
tempList.add(eachUser); | |
userAssignedToProfileMap.put(eachUser.ProfileId, tempList); | |
} | |
//Prepare the data for every profile | |
for(Profile p :profileMap.values()) { | |
Map<String, Boolean> tempStdPermAssignedMap = new Map<String, Boolean>(); | |
List<String> tempCustPermAssignedList = new List<String>(); | |
for(String standard_permission :standard_permissions_profile.keySet()) { | |
Boolean permission_assigned = (Boolean) p.get(standard_permission); | |
tempStdPermAssignedMap.put(standard_permission, permission_assigned); | |
} | |
profileStdPermissionAssigned.put(p.Id, tempStdPermAssignedMap); | |
if(entity_id_to_custom_permissions.containsKey(p.Id)) { | |
tempCustPermAssignedList = entity_id_to_custom_permissions.get(p.Id); | |
} | |
profileCustPermissionAssigned.put(p.Id, tempCustPermAssignedList); | |
} | |
Map<id, List<String>> entityIdToNamedCredentialMap = getNamedCredential (profileMap.keySet()); | |
Map<id, List<String>> entityIdToConnectedAppMap = getConnectedApplication (profileMap.keySet()); | |
Map<id, List<String>> entityIdToExternalDataSourceMap = getExternalDataSource(profileMap.keySet()); | |
String permission_text = ''; | |
String pemLabel = ''; | |
String pemVal = ''; | |
for (Profile eachProfile :profileMap.values()) { | |
pemLabel = ''; | |
pemVal = ''; | |
permission_text = 'Profile Name,'; | |
permission_text += eachProfile.Name; | |
permission_text += '\n'; | |
if(profileStdPermissionAssigned.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Standard permissions:\n'; | |
permission_text += 'Label, Name, Value\n'; //HelpText, | |
for (String key :profileStdPermissionAssigned.get(eachProfile.Id).keySet()) { | |
if (standard_permissions_profile.containsKey(key)) { | |
pemLabel += standard_permissions_profile.get(key) + ',' ; | |
} else { | |
pemLabel += ','; | |
} | |
/*if(standard_permissions_permissionset.containsKey(key)) { | |
Schema.DescribeFieldResult ps_field_describe = standard_permissions_permissionset.get(key); | |
pemLabel += ps_field_describe.getLabel() + ','; | |
//pemLabel += String.isNotBlank(ps_field_describe.getInlineHelpText()) ? ps_field_describe.getInlineHelpText() : '' + ','; | |
} else { | |
pemLabel += ','; | |
}*/ | |
pemLabel += key + ','; | |
pemLabel += profileStdPermissionAssigned.get(eachProfile.Id).get(key) + '\n'; | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(profileCustPermissionAssigned.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Custom permissions:\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
pemVal = String.join(profileCustPermissionAssigned.get(eachProfile.Id), ','); | |
permission_text += pemVal; | |
permission_text += '\n'; | |
} | |
if(userAssignedToProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Assigned users :\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
Integer i = 0; | |
for (User eachUser :userAssignedToProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachUser.Name; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(profileAssigendObjectPermission.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Object Permission :'; | |
permission_text += '\n'; | |
permission_text += 'SobjectType,' + String.join(objectPermissions_list, ',') + '\n'; | |
for (ObjectPermissions eachPerm :profileAssigendObjectPermission.get(eachProfile.Id)) { | |
String tempText = eachPerm.SobjectType + ','; | |
Integer i = 0; | |
for (String eachStr :objectPermissions_list) { | |
i++; | |
tempText += eachPerm.get(eachStr); | |
if(i >= objectPermissions_list.size()) { | |
tempText += '\n'; | |
} else { | |
tempText += ','; | |
} | |
} | |
permission_text += tempText; | |
} | |
permission_text += '\n'; | |
} | |
if(enabledClassesForProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Apex classes :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachClass :enabledClassesForProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachClass; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(enabledPagesForProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Apex pages :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachPage :enabledPagesForProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachPage; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if (entityIdToConnectedAppMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Connected app :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachConnApp :entityIdToConnectedAppMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachConnApp; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if (entityIdToExternalDataSourceMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Connected app :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachExtDataSource :entityIdToExternalDataSourceMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachExtDataSource; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if (entityIdToNamedCredentialMap.containsKey(eachProfile.Id)) { | |
permission_text += 'Named credentials :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachNamedCred :entityIdToNamedCredentialMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachNamedCred; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
permission_text += '\n'; | |
permission_text += '---------------End------------------'; | |
permission_text += '\n'; | |
//sendEmail (eachProfile.Name, permission_text); | |
ContentVersionUtil.createDocument (parentId, currentProfile.Name + ' - ' + System.now() ,permission_text); | |
} | |
} | |
private void sendEmail(String fileName, String permission_text) { | |
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
efa.setFileName(fileName+'.csv'); | |
Blob permissions_content_blob = Blob.valueof(permission_text); | |
efa.setBody(permissions_content_blob); | |
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); | |
email.setSubject('Permissions Export :- ' + fileName); | |
email.setToAddresses(new List<String>{ email_address }); | |
email.setPlainTextBody('Your permissions export for the profile :-' + fileName); | |
email.setFileAttachments(new Messaging.EmailFileAttachment [] { | |
efa}); | |
Messaging.sendEmail(new Messaging.SingleEmailMessage [] { | |
email}); | |
} | |
private Map<Id, SetupEntityAccess> getSetupEntityAccesByType(String entityPype, Set<Id> profileIds) { | |
return new Map<Id, SetupEntityAccess>([SELECT Id, SetupEntityId, ParentId, Parent.ProfileId | |
FROM SetupEntityAccess | |
WHERE Parent.ProfileId in :profileIds | |
AND SetupEntityType = :entityPype]); | |
} | |
private Map<id, List<String>> getConnectedApplication (Set<Id> profileIds) { | |
Map<id, List<String>> entityIdToConnectedAppMap = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('ConnectedApplication', profileIds); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, ConnectedApplication> connectedAppMap = new Map<Id, ConnectedApplication>([SELECT Id, Name FROM ConnectedApplication where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<String> tempList; | |
if(entityIdToConnectedAppMap.containsKey(eachAccess.Parent.ProfileId)) { | |
tempList = entityIdToConnectedAppMap.get(eachAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
if(connectedAppMap.containsKey(eachAccess.SetupEntityId)) { | |
tempList.add(connectedAppMap.get(eachAccess.SetupEntityId).Name); | |
} | |
entityIdToConnectedAppMap.put(eachAccess.Parent.ProfileId, tempList); | |
} | |
return entityIdToConnectedAppMap; | |
} | |
private Map<id, List<String>> getExternalDataSource (Set<Id> profileIdSet) { | |
Map<id, List<String>> entityIdToExternalDataSourceMap = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('ExternalDataSource', profileIdSet); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, ExternalDataSource> externalDataSourceMap = new Map<Id, ExternalDataSource>([SELECT Id, DeveloperName, MasterLabel FROM ExternalDataSource where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<String> tempList; | |
if(entityIdToExternalDataSourceMap.containsKey(eachAccess.Parent.ProfileId)) { | |
tempList = entityIdToExternalDataSourceMap.get(eachAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
if(externalDataSourceMap.containsKey(eachAccess.SetupEntityId)) { | |
tempList.add(externalDataSourceMap.get(eachAccess.SetupEntityId).MasterLabel); | |
} | |
entityIdToExternalDataSourceMap.put(eachAccess.Parent.ProfileId, tempList); | |
} | |
return entityIdToExternalDataSourceMap; | |
} | |
private Map<id, List<String>> getNamedCredential (Set<Id> profileIdSet) { | |
Map<id, List<String>> entityIdToNamedCredentialMap = new Map<Id, List<String>>(); | |
Map<Id, SetupEntityAccess> setEntityMap = getSetupEntityAccesByType('NamedCredential', profileIdSet); | |
Set<Id> setupEntityIds = new Set<Id>(); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
setupEntityIds.add(eachAccess.SetupEntityId); | |
} | |
Map<Id, NamedCredential> namedCredentialMap = new Map<Id, NamedCredential>([SELECT Id, DeveloperName, MasterLabel FROM NamedCredential where Id in :setupEntityIds]); | |
for (SetupEntityAccess eachAccess :setEntityMap.values()) { | |
List<String> tempList; | |
if(entityIdToNamedCredentialMap.containsKey(eachAccess.Parent.ProfileId)) { | |
tempList = entityIdToNamedCredentialMap.get(eachAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
if(namedCredentialMap.containsKey(eachAccess.SetupEntityId)) { | |
tempList.add(namedCredentialMap.get(eachAccess.SetupEntityId).MasterLabel); | |
} | |
entityIdToNamedCredentialMap.put(eachAccess.Parent.ProfileId, tempList); | |
} | |
return entityIdToNamedCredentialMap; | |
} | |
} |
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
public class ProfilePermissionExtractionHelper { | |
private static ProfilePermissionExtractionHelper handler; | |
private ProfilePermissionExtractionHelper() { | |
} | |
String email_address = '[email protected]'; | |
public static ProfilePermissionExtractionHelper getInstance() { | |
if(handler == null) { | |
handler = new ProfilePermissionExtractionHelper(); | |
} | |
return handler; | |
} | |
private Map<String, Schema.DescribeFieldResult> getStandardPermissionForPermissionSet() { | |
Map<String, Schema.DescribeFieldResult> standard_permissions_permissionset = new Map<String, Schema.DescribeFieldResult>(); | |
Schema.DescribeSObjectResult ps_describe = PermissionSet.sObjectType.getDescribe(); | |
for(Schema.SObjectField ps_field :ps_describe.fields.getMap().values()) { | |
Schema.DescribeFieldResult ps_field_describe = ps_field.getDescribe(); | |
if(ps_field_describe.getName().startsWith('Permissions')) { | |
standard_permissions_permissionset.put(ps_field_describe.getName(), ps_field_describe); | |
} | |
} | |
return standard_permissions_permissionset; | |
} | |
private void getAllProfiles() { | |
List<Profile> profileList = [Select Id, Name from Profile]; | |
} | |
public void getPermissionSetInfo() { | |
/*Map<String, Schema.DescribeFieldResult> standard_permissions_permissionset = getStandardPermissionForPermissionSet(); | |
List<String> permissions_List = new List<String>(standard_permissions_permissionset.keySet()); | |
String query = 'SELECT Id, Label, ' + String.join(permissions_list, ',') + ' FROM PermissionSet WHERE IsOwnedByProfile = false'; | |
List<PermissionSet> permission_sets = (List<PermissionSet>) Database.query(query); | |
Map<Id, String> permission_sets_name = new Map<Id, String>(); | |
for(PermissionSet permission_set :permission_sets) { | |
permission_sets_name.put(permission_set.Id, permission_set.Label); | |
}*/ | |
//Get the standard profile permissions | |
Map<String, String> standard_permissions_profile = new Map<String, String>(); | |
Schema.DescribeSObjectResult profile_describe = Profile.sObjectType.getDescribe(); | |
for(Schema.SObjectField profile_field :profile_describe.fields.getMap().values()) { | |
Schema.DescribeFieldResult profile_field_describe = profile_field.getDescribe(); | |
if(profile_field_describe.getName().startsWith('Permissions')) { | |
standard_permissions_profile.put(profile_field_describe.getName(), profile_field_describe.getLabel()); | |
} | |
} | |
List<String> permissions_List = new List<String>(standard_permissions_profile.keySet()); | |
String query = 'SELECT Id, Name, ' + String.join(permissions_list, ',') + ' FROM Profile LIMIT 1'; | |
List<Profile> profiles = (List<Profile>) Database.query(query); | |
//Get custom permissions and their assignments to profile sand permission sets | |
Map<id, List<String>> entity_id_to_custom_permissions = new Map<Id, List<String>>(); | |
List<CustomPermission> custom_permissions = [SELECT Id, DeveloperName, MasterLabel, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM CustomPermission]; | |
for(CustomPermission custom_permission :custom_permissions) { | |
for(SetupEntityAccess entity_access :custom_permission.SetupEntityAccessItems) { | |
List<Id> custom_permissions_list; | |
if(entity_id_to_custom_permissions.containsKey(entity_access.Parent.ProfileId)) { | |
custom_permissions_list = entity_id_to_custom_permissions.get(entity_access.Parent.ProfileId); | |
} else { | |
custom_permissions_list = new List<String>(); | |
} | |
custom_permissions_list.add(custom_permission.MasterLabel); | |
entity_id_to_custom_permissions.put(entity_access.Parent.ProfileId, custom_permissions_list); | |
} | |
} | |
// Apex classes | |
Map<Id, List<String>> enabledClassesForProfileMap = new Map<Id, List<String>>(); | |
List<ApexClass> enabledApexClasses = [SELECT Id, Name, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM ApexClass]; | |
for (ApexClass eachClass :enabledApexClasses) { | |
for(SetupEntityAccess entityAccess :eachClass.SetupEntityAccessItems) { | |
List<String> tempList; | |
if(enabledClassesForProfileMap.containsKey(entityAccess.Parent.ProfileId)) { | |
tempList = enabledClassesForProfileMap.get(entityAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
tempList.add(eachClass.Name); | |
enabledClassesForProfileMap.put(entityAccess.Parent.ProfileId, tempList); | |
} | |
} | |
// ApexPage | |
Map<Id, List<String>> enabledPagesForProfileMap = new Map<Id, List<String>>(); | |
List<ApexPage> enabledApexPages = [SELECT Id, Name, (select Id, ParentId, Parent.ProfileId from SetupEntityAccessItems) FROM ApexPage]; | |
for (ApexPage eachPage :enabledApexPages) { | |
for(SetupEntityAccess entityAccess :eachPage.SetupEntityAccessItems) { | |
List<String> tempList; | |
if(enabledPagesForProfileMap.containsKey(entityAccess.Parent.ProfileId)) { | |
tempList = enabledPagesForProfileMap.get(entityAccess.Parent.ProfileId); | |
} else { | |
tempList = new List<String>(); | |
} | |
tempList.add(eachPage.Name); | |
enabledPagesForProfileMap.put(entityAccess.Parent.ProfileId, tempList); | |
} | |
} | |
//get All object permision | |
Map<String, String> object_permissions_profile = new Map<String, String>(); | |
for(Schema.SObjectField objectPerm_field :Schema.sObjectType.ObjectPermissions.fields.getMap().values()) { | |
Schema.DescribeFieldResult objectPerm_field_describe = objectPerm_field.getDescribe(); | |
if(objectPerm_field_describe.getName().startsWith('Permissions')) { | |
object_permissions_profile.put(objectPerm_field_describe.getName(), objectPerm_field_describe.getLabel()); | |
} | |
} | |
List<String> objectPermissions_list = new List<String>(object_permissions_profile.keySet()); | |
query = 'SELECT Id, Parent.ProfileId, SObjectType, ' + String.join(objectPermissions_list, ',') + ' FROM ObjectPermissions ' | |
+ 'WHERE parentid in (select id from permissionset where PermissionSet.ProfileId in :profiles )'; | |
System.debug('query :' + query); | |
List<ObjectPermissions> objectPermissionsList = (List<ObjectPermissions>) Database.query(query); | |
Map<Id, List<ObjectPermissions>> profileAssigendObjectPermission = new Map<Id, List<ObjectPermissions>>(); | |
for (ObjectPermissions eachObjectPermission :objectPermissionsList) { | |
if(profileAssigendObjectPermission.containsKey(eachObjectPermission.Parent.ProfileId)) { | |
profileAssigendObjectPermission.get(eachObjectPermission.Parent.ProfileId).add(eachObjectPermission); | |
} else { | |
profileAssigendObjectPermission.put(eachObjectPermission.Parent.ProfileId, new List<ObjectPermissions>{ eachObjectPermission }); | |
} | |
} | |
//Query permission set assignments | |
Map<Id, User> users = new Map<Id, User>([SELECT Id, Name, ProfileId FROM User WHERE IsActive = true]); | |
System.debug('users : ' + users); | |
List<PermissionSetAssignment> assignments = [SELECT Id, PermissionSetId, AssigneeId FROM PermissionSetAssignment]; | |
Map<Id, List<User>> permission_sets_map = new Map<Id, List<User>>(); | |
for(PermissionSetAssignment pas :assignments) { | |
List<User> user_in_ps = new List<User>(); | |
if(permission_sets_map.containsKey(pas.PermissionSetId)) { | |
user_in_ps = permission_sets_map.get(pas.PermissionSetId); | |
} | |
if(users.containsKey(pas.AssigneeId)) { | |
user_in_ps.add(users.get(pas.AssigneeId)); | |
} | |
permission_sets_map.put(pas.PermissionSetId, user_in_ps); | |
} | |
Map<Id, List<User>> userAssignedToProfileMap = new Map<Id, List<User>>(); | |
Map<Id, Map<String, Boolean>> profileStdPermissionAssigned = new Map<Id, Map<String, Boolean>>(); | |
Map<Id, List<String>> profileCustPermissionAssigned = new Map<Id, List<String>>(); | |
for (User eachUser :users.values()) { | |
List<User> tempList = new List<User>(); | |
if(userAssignedToProfileMap.containsKey(eachUser.ProfileId)) { | |
tempList = userAssignedToProfileMap.get(eachUser.ProfileId); | |
} | |
tempList.add(eachUser); | |
userAssignedToProfileMap.put(eachUser.ProfileId, tempList); | |
} | |
//Prepare the data for every profile | |
for(Profile p :profiles) { | |
Map<String, Boolean> tempStdPermAssignedMap = new Map<String, Boolean>(); | |
List<String> tempCustPermAssignedList = new List<String>(); | |
for(String standard_permission :standard_permissions_profile.keySet()) { | |
Boolean permission_assigned = (Boolean) p.get(standard_permission); | |
tempStdPermAssignedMap.put(standard_permission, permission_assigned); | |
} | |
profileStdPermissionAssigned.put(p.Id, tempStdPermAssignedMap); | |
if(entity_id_to_custom_permissions.containsKey(p.Id)) { | |
tempCustPermAssignedList = entity_id_to_custom_permissions.get(p.Id); | |
} | |
profileCustPermissionAssigned.put(p.Id, tempCustPermAssignedList); | |
} | |
/*Map<Id, Map<String, Boolean>> permissionSetStdPermissionAssigned = new Map<Id, Map<String, Boolean>>(); | |
Map<Id, List<User>> assignedUsersInPermSetMap = new Map<Id, List<User>>(); | |
//Prepare the data for every permission set | |
for(PermissionSet p :permission_sets) { | |
Map<String, Boolean> tempStdPermAssignedMap = new Map<String, Boolean>(); | |
for(String standard_permission :standard_permissions_permissionset.keySet()) { | |
Boolean permission_assigned = (Boolean) p.get(standard_permission); | |
tempStdPermAssignedMap.put(standard_permission, permission_assigned); | |
} | |
permissionSetStdPermissionAssigned.put(p.Id, tempStdPermAssignedMap); | |
}*/ | |
String permission_text = ''; | |
String pemLabel = ''; | |
String pemVal = ''; | |
for (Profile eachProfile :profiles) { | |
pemLabel = ''; | |
pemVal = ''; | |
permission_text += 'Profile Name,'; | |
permission_text += eachProfile.Name; | |
permission_text += '\n'; | |
if(profileStdPermissionAssigned.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Standard permissions:\n'; | |
permission_text += 'Label, Name, Value\n'; //HelpText, | |
for (String key :profileStdPermissionAssigned.get(eachProfile.Id).keySet()) { | |
if (standard_permissions_profile.containsKey(key)) { | |
pemLabel += standard_permissions_profile.get(key) + ',' ; | |
} else { | |
pemLabel += ','; | |
} | |
/*if(standard_permissions_permissionset.containsKey(key)) { | |
Schema.DescribeFieldResult ps_field_describe = standard_permissions_permissionset.get(key); | |
pemLabel += ps_field_describe.getLabel() + ','; | |
//pemLabel += String.isNotBlank(ps_field_describe.getInlineHelpText()) ? ps_field_describe.getInlineHelpText() : '' + ','; | |
} else { | |
pemLabel += ','; | |
}*/ | |
pemLabel += key + ','; | |
pemLabel += profileStdPermissionAssigned.get(eachProfile.Id).get(key) + '\n'; | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(profileCustPermissionAssigned.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Custom permissions:\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
pemVal = String.join(profileCustPermissionAssigned.get(eachProfile.Id), ','); | |
permission_text += pemVal; | |
permission_text += '\n'; | |
} | |
if(userAssignedToProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Assigned users :\n'; | |
pemLabel = ''; | |
pemVal = ''; | |
Integer i = 0; | |
for (User eachUser :userAssignedToProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachUser.Name; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(profileAssigendObjectPermission.containsKey(eachProfile.Id)) { | |
permission_text += '\n'; | |
permission_text += 'Object Permission :'; | |
permission_text += '\n'; | |
permission_text += 'SobjectType,' + String.join(objectPermissions_list, ',') + '\n'; | |
for (ObjectPermissions eachPerm :profileAssigendObjectPermission.get(eachProfile.Id)) { | |
String tempText = eachPerm.SobjectType + ','; | |
Integer i = 0; | |
for (String eachStr :objectPermissions_list) { | |
i++; | |
tempText += eachPerm.get(eachStr); | |
if(i >= objectPermissions_list.size()) { | |
tempText += '\n'; | |
} else { | |
tempText += ','; | |
} | |
} | |
permission_text += tempText; | |
} | |
permission_text += '\n'; | |
} | |
if(enabledClassesForProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += 'Apex classes :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachClass :enabledClassesForProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachClass; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
if(enabledPagesForProfileMap.containsKey(eachProfile.Id)) { | |
permission_text += 'Apex pages :'; | |
permission_text += '\n'; | |
Integer i = 0; | |
pemLabel = ''; | |
for (String eachPage :enabledPagesForProfileMap.get(eachProfile.Id)) { | |
i++; | |
pemLabel += eachPage; | |
if(Math.mod(i, 2) == 0) { | |
pemLabel += '\n'; | |
} else { | |
pemLabel += ','; | |
} | |
} | |
permission_text += pemLabel; | |
permission_text += '\n'; | |
} | |
permission_text += '\n'; | |
permission_text += '=================================================================='; | |
permission_text += '\n'; | |
} | |
sendEmail (permission_text); | |
} | |
private void sendEmail(String permission_text) { | |
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
efa.setFileName('permissions.csv'); | |
Blob permissions_content_blob = Blob.valueof(permission_text); | |
efa.setBody(permissions_content_blob); | |
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); | |
email.setSubject('Permissions Export'); | |
email.setToAddresses(new List<String>{ email_address }); | |
email.setPlainTextBody('Your permissions export'); | |
email.setFileAttachments(new Messaging.EmailFileAttachment [] { | |
efa}); | |
Messaging.sendEmail(new Messaging.SingleEmailMessage [] { | |
email}); | |
} | |
} | |
//pemLabel = String.join( new List<String>(profileStdPermissionAssigned.get (eachProfile.Id).keySet ()), ','); | |
//pemVal = String.join( profileStdPermissionAssigned.get (eachProfile.Id).values (), ','); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment