CustomPermissionsReader Class

virtual

This class is designed to help with caching the results of querying (via SOQL) Custom Permissions for the current user, it will load all defined Custom Permissions in one go for a given default or specified namespace (so not all defined in the org). This is done on the basis the caller will make 2 or more calls to the hasPermission method, thus benefiting from the bulkification approach used. Note that the query to the database is demand loaded only on the first call to the hasPermission method thus constructing the object carries no SOQL / database overhead.

Copyright (c), Andrew Fawcett All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of the Andrew Fawcett, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

IMPORTANT UPDATE: Since API 41 (Winter’18) there is now a native way to read Custom Permissions. The following may still be useful if you have requirements not met by the native method. See Apex Developer Guide for FeatureManagement.checkPermission. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_FeatureManagement.htm

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Modified by: Peter Friberg, Metaforce AB

Namespace

mfwe

Constructors

CustomPermissionsReader()

This default constructor will seek out all unmanaged/default namespace Custom Permissions

Signature

global CustomPermissionsReader()

CustomPermissionsReader(managedObject)

This constructor will load Custom Permissions associated with the namespace of the object passed in, this is the best constructor to use if you are developing a managed AppExchange package! The object passed in does not matter so long as its one from the package itself.

If the object is running in a managed context (e.g. packaging org or installed package) namespace is used to constrain the query If the object is not running in a managed context (e.g. developer org not namespaced) the default namespace is used to query

Signature

global CustomPermissionsReader(SObjectType managedObject)

Parameters

NameTypeDescription
managedObjectSObjectTypeThe object passed in does not matter so long as its one from the package itself.

Methods

hasPermission(customPermissionName)

This method will return true if the current user has the given custom permission assigned to them.

Signature

global Boolean hasPermission(String customPermissionName)

Parameters

NameTypeDescription
customPermissionNameStringThe name of the custom permission to check for

Return Type

Boolean

True if the current user has the given custom permission assigned to them

Classes

CustomPermissionsException Class

Custom exception to throw when a custom permission does not exist