Getting all Users and their Custom Permissions
You can get Object
, Field
, and SetupEntity
permissions with the query outlined below. It gets you a lot of information...
static List<PermissionSet> getPermissions(Id userId)
{
return [
SELECT Name,
(
SELECT
SObjectType,
Field,
PermissionsRead,
PermissionsEdit
FROM FieldPerms
),
(
SELECT
SObjectType,
PermissionsRead,
PermissionsCreate,
PermissionsEdit,
PermissionsDelete,
PermissionsViewAllRecords,
PermissionsModifyAllRecords
FROM ObjectPerms
),
(
SELECT SetupEntityId, SetupEntityType
FROM SetupEntityAccessItems
)
FROM PermissionSet WHERE Id IN (
SELECT PermissionSetId
FROM PermissionSetAssignment
WHERE AssigneeId = :userId
)
];
}
If you just care about CustomPermission
, you could do something like:
static List<CustomPermission> getCustomPermissions(Id userId)
{
Set<Id> assigned = new Map<Id, PermissionSet>([
SELECT Id FROM PermissionSet
WHERE Id IN (
SELECT PermissionSetId
FROM PermissionSetAssignment
WHERE AssigneeId = :userId
)
]).keySet();
return [
SELECT DeveloperName FROM CustomPermission
WHERE Id IN (
SELECT SetupEntityId
FROM SetupEntityAccess
WHERE SetupEntityType = 'CustomPermission'
AND ParentId IN :assigned
)
];
}
Or for all users:
static List<CustomPermission> getCustomPermissions()
{
return [
SELECT DeveloperName FROM CustomPermission
WHERE Id IN (
SELECT SetupEntityId
FROM SetupEntityAccess
WHERE SetupEntityType = 'CustomPermission'
)
];
}