Sub topic 8
Permission sets: additive access on top of a profile
This chapter assumes you know what a profile is, how object permissions and field-level security work, and how to find a user record in Setup. It covers permission sets: what they are, how they combine with a profile, and the mistakes people make with them.
What a permission set is
A permission set is a collection of permissions that you assign to users in addition to their profile. Each user has exactly one profile. A user can have many permission sets.
Permission sets only add access. They never remove it. A user's effective access is the union of the profile and every assigned permission set. If the profile grants Read on Account and a permission set grants Edit, the user can edit Accounts.
A permission set can hold:
- Object permissions (Create, Read, Edit, Delete, View All, Modify All)
- Field permissions (Read and Edit)
- System and user permissions
- Custom permissions
- App, tab, Apex class, and Visualforce page access
- Record type assignments (but not defaults or page layout assignments)
Settings such as login hours, login IP ranges, and the default app stay on the profile.
A permission set in metadata form
This permission set grants read access to Account and read-only access to one field. The <hasActivationRequired> element is false, so the permission set takes effect as soon as it is assigned.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Account Viewer</label>
<description>Read-only access to Accounts and the Annual Revenue field</description>
<hasActivationRequired>false</hasActivationRequired>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>false</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
<fieldPermissions>
<editable>false</editable>
<field>Account.AnnualRevenue</field>
<readable>true</readable>
</fieldPermissions>
</PermissionSet>The <readable> and <editable> values describe the field's access. Field access is not granted by object access. You must grant both.
Assigning a permission set
In Setup, open the permission set and choose Manage Assignments. You can also assign it from the user's detail page. In Apex, insert a PermissionSetAssignment record that links the user to the permission set.
PermissionSet ps = [
SELECT Id
FROM PermissionSet
WHERE Name = 'Account_Viewer'
LIMIT 1
];
User u = [SELECT Id FROM User WHERE Username = '[email protected]' LIMIT 1];
insert new PermissionSetAssignment(
AssigneeId = u.Id,
PermissionSetId = ps.Id
);Note that the query uses the API name (Account_Viewer), not the label (Account Viewer).
Common errors
- Expecting a permission set to restrict access. It cannot. If a user has too much access, look at the profile and the other permission sets. To subtract permissions inside a group, you need a muting permission set, which a later chapter covers.
- Granting object access without field access. A user with Read on Account still cannot see a field unless a profile or permission set grants Read on that field.
- Confusing permissions with sharing. A permission set grants access to an object. It does not grant access to individual records. Record access comes from sharing, unless View All or Modify All is granted.
- Assigning the same permission set twice. A user can hold a given permission set only once, so the second insert fails with a duplicate error. Check existing assignments first.
- Missing license. A permission set can only be assigned to users whose license supports its permissions. Otherwise the assignment fails.
Exercise
A user's profile grants Read on Opportunity. You assign a permission set that grants Edit on Opportunity. A second permission set grants nothing on Opportunity. What can the user do with Opportunities, and why?
Show answer
The user can read and edit Opportunities. Effective access is the union of the profile and all assigned permission sets, so the Edit permission is added to the profile's Read. The second permission set adds nothing, and it removes nothing, because permission sets are only additive.