|
| 1 | +/** |
| 2 | + * @description Helper class for checking custom permissions. |
| 3 | + * The easiest way to check if the running user has permsissions is to use the `FeatureManagement.checkPermission` method. |
| 4 | + * This class is intended to be used when you need to check: |
| 5 | + * - Multiple custom permissions. |
| 6 | + * - Custom permissions in a test context. |
| 7 | + * - Custom permissions for a different user than the running user. |
| 8 | + * |
| 9 | + * @author Tor Håkon Sigurdsen |
| 10 | + * @since 0.1.0, March 2025 |
| 11 | + * @group Custom Permission Helper |
| 12 | + * @example |
| 13 | + * Eksempel på bruk: |
| 14 | + * ``` |
| 15 | + * Set<String> myCustomPermissionsSet = new CustomPermissionHelper().setCustomPermissionMaps().getAssignedCustomPermissions(); |
| 16 | + * ``` |
| 17 | + */ |
| 18 | +public inherited sharing class CustomPermissionHelper { |
| 19 | + @TestVisible |
| 20 | + private Map<Id, Set<String>> usersCustomPermissionsMap; |
| 21 | + @TestVisible |
| 22 | + private Map<Id, CustomPermission> customPermissionMapById; |
| 23 | + @TestVisible |
| 24 | + private Map<String, CustomPermission> customPermissionMapByDeveloperName; |
| 25 | + |
| 26 | + /** |
| 27 | + * @description Load all custom permissions into maps. |
| 28 | + * |
| 29 | + * @author Tor Håkon Sigurdsen |
| 30 | + * @since 0.1.0, March 2025 |
| 31 | + * @return `CustomPermissionHelper` instance |
| 32 | + */ |
| 33 | + public CustomPermissionHelper setCustomPermissionMaps() { |
| 34 | + return this.setCustomPermissionMaps( |
| 35 | + [SELECT Id, DeveloperName FROM CustomPermission LIMIT 1000] |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @description Load all custom permissions into maps. |
| 41 | + * |
| 42 | + * @author Tor Håkon Sigurdsen |
| 43 | + * @since 0.1.0, March 2025 |
| 44 | + * @param customPermissions List of custom permissions to load into the maps |
| 45 | + * @return `CustomPermissionHelper` instance |
| 46 | + */ |
| 47 | + public CustomPermissionHelper setCustomPermissionMaps( |
| 48 | + List<CustomPermission> customPermissions |
| 49 | + ) { |
| 50 | + customPermissionMapById = new Map<Id, CustomPermission>(); |
| 51 | + customPermissionMapByDeveloperName = new Map<String, CustomPermission>(); |
| 52 | + for (CustomPermission customPermission : customPermissions) { |
| 53 | + customPermissionMapById.put(customPermission.Id, customPermission); |
| 54 | + customPermissionMapByDeveloperName.put( |
| 55 | + customPermission.DeveloperName, |
| 56 | + customPermission |
| 57 | + ); |
| 58 | + } |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @description Validate that the custom permission exists. If not, throw an exception. |
| 64 | + * |
| 65 | + * @author Tor Håkon Sigurdsen |
| 66 | + * @since 0.1.0, March 2025 |
| 67 | + * @param customPermissionDeveloperName Developer name of the custom permission to validate |
| 68 | + * @return self for chaining |
| 69 | + * @exception Throws `CustomPermissionHelperException` if custom permission with developer name is not found |
| 70 | + */ |
| 71 | + public CustomPermissionHelper validateCustomPermission( |
| 72 | + String customPermissionDeveloperName |
| 73 | + ) { |
| 74 | + if ( |
| 75 | + !customPermissionMapByDeveloperName.containsKey( |
| 76 | + customPermissionDeveloperName |
| 77 | + ) |
| 78 | + ) { |
| 79 | + throw new CustomPermissionHelperException( |
| 80 | + 'Custom Permission with Developer Name ' + |
| 81 | + customPermissionDeveloperName + |
| 82 | + ' not found' |
| 83 | + ); |
| 84 | + } |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @description Get the running users assigned custom permissions |
| 90 | + * |
| 91 | + * @author Tor Håkon Sigurdsen |
| 92 | + * @since 0.1.0, March 2025 |
| 93 | + * @return `Set<String>` containing the developer name of the assigned custom permissions |
| 94 | + */ |
| 95 | + public Set<String> getAssignedCustomPermissions() { |
| 96 | + return getAssignedCustomPermissions(UserInfo.getUserId()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @description get the assigned custom permissions for a user |
| 101 | + * |
| 102 | + * @author Tor Håkon Sigurdsen |
| 103 | + * @since 0.1.0, March 2025 |
| 104 | + * @param userId of the user to get the assigned custom permissions for |
| 105 | + * @return `Set<String>` containing the developer name of the assigned custom permissions |
| 106 | + */ |
| 107 | + public Set<String> getAssignedCustomPermissions(Id userId) { |
| 108 | + if (!this.usersCustomPermissionsMap.containsKey(userId)) { |
| 109 | + setAssignedCustomPermissionsByUser( |
| 110 | + userId, |
| 111 | + this.getSetupEntityAccesses(userId) |
| 112 | + ); |
| 113 | + } |
| 114 | + return this.usersCustomPermissionsMap.get(userId); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @description Check if the running user has a custom permission |
| 119 | + * |
| 120 | + * @author Tor Håkon Sigurdsen |
| 121 | + * @since 0.1.0, March 2025 |
| 122 | + * @param customPermissionDeveloperName Developer name of the custom permission to check |
| 123 | + * @return `Boolean` indicating if the running user has the custom permission |
| 124 | + */ |
| 125 | + public Boolean hasCustomPermission(String customPermissionDeveloperName) { |
| 126 | + return FeatureManagement.checkPermission(customPermissionDeveloperName); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @description Check if a user has a custom permission |
| 131 | + * |
| 132 | + * @author Tor Håkon Sigurdsen |
| 133 | + * @since 0.1.0, March 2025 |
| 134 | + * @param userId of the user to check the custom permission for |
| 135 | + * @param customPermissionDeveloperName Developer name of the custom permission to check |
| 136 | + * @return `Boolean` indicating if the user has the custom permission |
| 137 | + */ |
| 138 | + public Boolean hasCustomPermission( |
| 139 | + Id userId, |
| 140 | + String customPermissionDeveloperName |
| 141 | + ) { |
| 142 | + return getAssignedCustomPermissions(userId) |
| 143 | + .contains(customPermissionDeveloperName); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @description Load the assigned custom permissions for users |
| 148 | + * |
| 149 | + * @author Tor Håkon Sigurdsen |
| 150 | + * @since 0.1.0, March 2025 |
| 151 | + * @param userId of the user to load the assigned custom permissions for |
| 152 | + * @param setupEntityAccessesList List of SetupEntityAccess objects for the users |
| 153 | + * @return `CustomPermissionHelper` instance |
| 154 | + */ |
| 155 | + @TestVisible |
| 156 | + private CustomPermissionHelper setAssignedCustomPermissionsByUser( |
| 157 | + Id userId, |
| 158 | + List<SetupEntityAccess> setupEntityAccessesList |
| 159 | + ) { |
| 160 | + if (usersCustomPermissionsMap == null) { |
| 161 | + usersCustomPermissionsMap = new Map<Id, Set<String>>(); |
| 162 | + } |
| 163 | + |
| 164 | + usersCustomPermissionsMap.put(userId, new Set<String>()); |
| 165 | + for (SetupEntityAccess setupEntityAccess : setupEntityAccessesList) { |
| 166 | + if ( |
| 167 | + customPermissionMapById == null || |
| 168 | + !customPermissionMapById.containsKey( |
| 169 | + setupEntityAccess.SetupEntityId |
| 170 | + ) |
| 171 | + ) { |
| 172 | + throw new CustomPermissionHelperException( |
| 173 | + 'Could not map Custom Permissions to user. Custom Permission with Id ' + |
| 174 | + setupEntityAccess.SetupEntityId + |
| 175 | + ' not found. Try calling setCustomPermissionMaps() first.' |
| 176 | + ); |
| 177 | + } |
| 178 | + usersCustomPermissionsMap.get(userId) |
| 179 | + .add( |
| 180 | + customPermissionMapById.get(setupEntityAccess.SetupEntityId) |
| 181 | + .DeveloperName |
| 182 | + ); |
| 183 | + } |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @description Get the SetupEntityAccess objects for the provided user |
| 189 | + * |
| 190 | + * @author Tor Håkon Sigurdsen |
| 191 | + * @since 0.1.0, March 2025 |
| 192 | + * @param userId of the user to get the SetupEntityAccess objects for |
| 193 | + * @return `List<SetupEntityAccess>` containing the SetupEntityAccess objects for the provided users |
| 194 | + */ |
| 195 | + @TestVisible |
| 196 | + private List<SetupEntityAccess> getSetupEntityAccesses(Id userId) { |
| 197 | + return [ |
| 198 | + SELECT SetupEntityId |
| 199 | + FROM SetupEntityAccess |
| 200 | + WHERE |
| 201 | + SetupEntityId IN :customPermissionMapById.keySet() |
| 202 | + AND ParentId IN ( |
| 203 | + SELECT PermissionSetId |
| 204 | + FROM PermissionSetAssignment |
| 205 | + WHERE AssigneeId = :userId |
| 206 | + ) |
| 207 | + WITH SECURITY_ENFORCED |
| 208 | + ]; |
| 209 | + } |
| 210 | +} |
0 commit comments