-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfieldPermissionSettings.apex
112 lines (75 loc) · 4.37 KB
/
fieldPermissionSettings.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
/** SET FIELDS TO TRUE/FALSE **/
/**
* If Object and field combo is plugged in and settings for both read and write are false you will receive a misleading error message along the lines of, "...permission set read is required.."
* This doesn't make sense because we are trying to set the permissions for both write and read to false. From the inital trial and error in reproducing this error I believe it shows up when there isn't any read or write permission on the field to begin with
* Below is a SOQL query to confirm whether or not there are any current permissions on the custom field that can be run before and after to see what the results were and if a false/false permission setting is necessary
* SELECT SobjectType,
* Field, ParentId,
* Parent.Label,
* Parent.Name,
* PermissionsRead,
* PermissionsEdit
* FROM FieldPermissions
* WHERE Field = 'Account.Custom_Field__c'
* AND Parent.Name = 'OFFICE_MANAGER'
**/
List<String> permissionSetNamesToAdjustFieldsFor = new List<String>{
'OFFICE_MANAGER',
'REGIONAL_DIRECTOR',
'CLIENT_SERVICES',
'SALES'
};
Map<String, Map<String, Boolean>> fieldsToBeAdjustedMap = new Map<String, Map<String, Boolean>> {
'Account.Custom_Field__c' => new Map<String, Boolean> {
'READ' => true,
'WRITE' => true
}
};
List<PermissionSet> permissionSetsToGetUpdated = [SELECT Label, PermissionsTransferAnyLead, Id,
(SELECT SobjectType, PermissionsRead FROM ObjectPerms),
(SELECT SobjectType, Field, ParentId, PermissionsRead, PermissionsEdit FROM FieldPerms WHERE Field IN: fieldsToBeAdjustedMap.keySet()),
(SELECT AssigneeId,Assignee.Name FROM Assignments)
FROM PermissionSet WHERE Name in : permissionSetNamesToAdjustFieldsFor];
List<FieldPermissions> fieldPermissionsToUpdate = new List<FieldPermissions>();
for (PermissionSet perm : permissionSetsToGetUpdated) {
fieldPermissionsToUpdate.addAll(adjustPermissionByFlag(perm.FieldPerms, fieldsToBeAdjustedMap, perm));
}
upsert fieldPermissionsToUpdate;
/////////////////////////////
private List<FieldPermissions> adjustPermissionByFlag(List<FieldPermissions> fieldPermsToClone, Map<String, Map<String, Boolean>> fieldsToBeAdjustedMap, PermissionSet permSet) {
List<FieldPermissions> fieldPermissionsToUpdate = new List<FieldPermissions>();
for (String newFieldToReceivePerm : fieldsToBeAdjustedMap.keySet()) {
List<String> existingPermissionsModified = new List<String>();
if (fieldPermsToClone.size() > 0 ) {
for (FieldPermissions fieldPermission : fieldPermsToClone) {
Boolean readValue = fieldsToBeAdjustedMap.get(fieldPermission.Field).get('READ');
Boolean writeValue = fieldsToBeAdjustedMap.get(fieldPermission.Field).get('WRITE');
if (fieldsToBeAdjustedMap.get(fieldPermission.Field) != NULL) {
fieldPermission.PermissionsRead = readValue;
fieldPermission.PermissionsEdit = writeValue;
existingPermissionsModified.add(newFieldToReceivePerm);
fieldPermissionsToUpdate.add(fieldPermission);
}
}
}
for (String fieldToUpsertPermission : fieldsToBeAdjustedMap.keySet()) {
if (!existingPermissionsModified.contains(fieldToUpsertPermission) ) {
fieldPermissionsToUpdate.add(createNewFieldPermissionByPermissionSetAndField(permSet, fieldToUpsertPermission, fieldsToBeAdjustedMap.get(fieldToUpsertPermission) ));
}
}
}
return fieldPermissionsToUpdate;
}
public FieldPermissions createNewFieldPermissionByPermissionSetAndField(PermissionSet permSet, String fieldToCreateNewPermissionsFor, Map<String, Boolean> readWriteSettingToValueMap) {
Boolean readValue = readWriteSettingToValueMap.get('READ');
Boolean writeValue = readWriteSettingToValueMap.get('WRITE');
String expectedDelimiter = '.';
String newsobjectTypeOfField = fieldToCreateNewPermissionsFor.substringBefore(expectedDelimiter);
FieldPermissions newfp = new FieldPermissions();
newfp.ParentId = permSet.Id;
newfp.Field = fieldToCreateNewPermissionsFor;
newfp.SobjectType = newsobjectTypeOfField;
newfp.PermissionsRead = readValue;
newfp.PermissionsEdit = writeValue;
return newfp;
}