Requirement
In Salesforce, you can check if the current user is an admin by checking their profile or permissions. The easiest way to do this is by checking if the user has the “Modify All Data” permission, which is a standard permission given to system administrators.
Here’s how you can check if the current user is an admin in Apex:
Apex Sample Code
public class UserAccessController {
// Check if the user has "Modify All Data" permission via profile or permission sets
public static Boolean isCurrentUserAdmin() {
String userId = UserInfo.getUserId();
// Check if the user’s profile has "Modify All Data" permission
Boolean isAdminProfile = [SELECT Profile.PermissionsModifyAllData
FROM User
WHERE Id = :userId].Profile.PermissionsModifyAllData;
// Check if the user has any permission set that grants "Modify All Data"
Boolean isAdminViaPermissionSet = [
SELECT PermissionsModifyAllData
FROM PermissionSetAssignment
WHERE AssigneeId = :userId
AND PermissionSet.PermissionsModifyAllData = true
LIMIT 1
] != null;
return isAdminProfile || isAdminViaPermissionSet;
}
}
