-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapexClassPermissionSettings.apex
123 lines (98 loc) · 3.88 KB
/
apexClassPermissionSettings.apex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// If using Manual List:
// search org for "@AuraEnabled"
// make note of all classes that belong to your app that compe up in the results
// add to String list varialbe "apiNamesOfClassesToAddAccessTo"
// to test:
// turn on critical update, login as user with targeted permission set and try to initiate call to AuraEnabled method. Should receive error modal with red background colored header immediately.
// run script
// retest method call
String classNameAppPrefix = 'APPNAME';
Boolean useManualList = true;
List<String> apiNamesOfClassesToAddAccessTo = new List<String>{
'Error_Logger',
'SearchController'
};
List<String> permissionSetsNeedingAccess = new List<String>{
'Tier_One_User',
'Tier_Two_User'
};
Map<Id, PermissionSet> mapPermissionSets = new Map<Id, PermissionSet> (
[
Select Id, Name from PermissionSet where Name in:permissionSetsNeedingAccess
]
);
Map<Id, ApexClass> mapApexClasses;
if (useManualList) {
mapApexClasses = new Map<Id, Apexclass>(
[
SELECT Id, Name
FROM ApexClass
where Name in:apiNamesOfClassesToAddAccessTo
]
);
} else {
String wildCardOfAppPrefix = '%' + classNameAppPrefix + '%';
mapApexClasses = new Map<Id, Apexclass>(
[
SELECT Id, Name
FROM ApexClass
where Name LIKE :wildCardOfAppPrefix
]
);
}
List<SetupEntityAccess> setupEntityAccesses = new List<SetupEntityAccess>();
for (PermissionSet permSet : mapPermissionSets.values()) {
for (ApexClass apxClass : mapApexClasses.values()) {
SetupEntityAccess accessSetup = new SetupEntityAccess(
ParentId = permSet.Id,
SetupEntityId = apxClass.Id
);
setupEntityAccesses.add(accessSetup);
}
}
List<Database.SaveResult> saveResults = Database.insert(setupEntityAccesses, false);
System.debug(LoggingLevel.ERROR, 'HERE IS saveResults amount: ' + saveResults.size() );
for (Integer i = 0; i < setupEntityAccesses.size(); i++) {
Database.SaveResult saveResult = saveResults[i];
SetupEntityAccess supAccess = setupEntityAccesses[i];
if (!saveResult.isSuccess()) {
System.debug(LoggingLevel.ERROR, 'HERE IS the Class and PermSet combo that failed:' + 'Apex Class - ' + mapApexClasses.get(supAccess.SetupEntityId).Name + ' : PermSet - ' + mapPermissionSets.get(supAccess.ParentId).Name );
for(Database.Error err : saveResult.getErrors()) {
System.debug('Status Code: ' + err.getStatusCode() + ': Error Message' + err.getMessage());
}
}
}
///////////////////////////////////////////////
// for testing purposes //
/// TO DELETE ////
List<String> apiNamesOfClassesToAddAccessTo = new List<String>{
'Error_Logger',
'SearchController'
};
Map<Id, Apexclass> mapApexClasses = new Map<Id, Apexclass>(
[
Select Id, Name from Apexclass where Name in:apiNamesOfClassesToAddAccessTo
]
);
List<String> permissionSetsNeedingAccess = new List<String>{
'Tier_One_User',
'Tier_Two_User'
};
Map<Id, PermissionSet> mapPermissionSets = new Map<Id, PermissionSet> (
[
Select Id, Name from PermissionSet where Name in:permissionSetsNeedingAccess
]
);
List<SetupEntityAccess> setupEntityAccessesToDelete = [
SELECT Id, Parent.Name, SetupEntityId
FROM SetupEntityAccess
WHERE ParentId IN :mapPermissionSets.keySet()
AND SetupEntityId IN: mapApexClasses.keySet()
];
for (SetupEntityAccess access : setupEntityAccessesToDelete) {
System.debug(LoggingLevel.ERROR, 'HERE BE access name: ' + access.Parent.Name);
System.debug(LoggingLevel.ERROR, 'HERE BE the clas: ' + access.SetupEntityId);
}
delete setupEntityAccessesToDelete;
///// END DELETE ///
////////////////////////////////////////////////////////////////////////