Skip to content

Instantly share code, notes, and snippets.

@tsalb
Last active August 26, 2020 20:35
Show Gist options
  • Save tsalb/7deca097f4d963e0923d27c097c2a340 to your computer and use it in GitHub Desktop.
Save tsalb/7deca097f4d963e0923d27c097c2a340 to your computer and use it in GitHub Desktop.
Sanity check your org's CRUD
String DEBUG_TEMPLATE = 'C:[C] R:[R] U:[U] D:[D] MA:[MA] RA:[RA] | [PROFILE_NAME] | [OBJECT_NAME]';
String TRUE_TEMPLATE = '[X]'; // do not use space, horizontal char alignment will be off
String FALSE_TEMPLATE = '[_]'; // same
Integer PROFILE_NAME_MAX_LENGTH;
// Grab the max chars of each, so we can get an evenly formatted / spaced output later
List<Integer> nameLengths = new List<Integer>();
for (Profile profile : [SELECT Name FROM Profile]) {
nameLengths.add(profile.Name.length());
}
nameLengths.sort();
PROFILE_NAME_MAX_LENGTH = nameLengths[nameLengths.size() -1];
System.debug('CRUD SCRIPT: PROFILE_NAME_MAX_LENGTH: '+PROFILE_NAME_MAX_LENGTH);
List<ObjectPermissions> perms = [
SELECT
Parent.Profile.Name,
SObjectType,
PermissionsCreate,
PermissionsRead,
PermissionsEdit,
PermissionsDelete,
PermissionsModifyAllRecords,
PermissionsViewAllRecords
FROM
ObjectPermissions
WHERE
Parent.IsOwnedByProfile = true
ORDER BY
Parent.Profile.Name ASC,
SObjectType ASC
];
for (ObjectPermissions perm : perms) {
// Deal with name and padding first
String profileName = perm.Parent.Profile.Name;
while (profileName.length() < PROFILE_NAME_MAX_LENGTH) {
// FYI, it's hard to find a character that will "smooth" out
// and "right justify" due to chars left of it being different horizontal lengths
profileName += '_';
}
String THIS_TEMPLATE = DEBUG_TEMPLATE.replace('[PROFILE_NAME]', profileName);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[OBJECT_NAME]', perm.SObjectType);
// Then go back to the beginning
THIS_TEMPLATE = THIS_TEMPLATE.replace('[C]', perm.PermissionsCreate ? TRUE_TEMPLATE : FALSE_TEMPLATE);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[R]', perm.PermissionsRead ? TRUE_TEMPLATE : FALSE_TEMPLATE);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[U]', perm.PermissionsEdit ? TRUE_TEMPLATE : FALSE_TEMPLATE);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[D]', perm.PermissionsDelete ? TRUE_TEMPLATE : FALSE_TEMPLATE);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[MA]', perm.PermissionsModifyAllRecords ? TRUE_TEMPLATE : FALSE_TEMPLATE);
THIS_TEMPLATE = THIS_TEMPLATE.replace('[RA]', perm.PermissionsViewAllRecords ? TRUE_TEMPLATE : FALSE_TEMPLATE);
// And here we go
System.debug('CRUD SCRIPT: '+THIS_TEMPLATE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment