-
Notifications
You must be signed in to change notification settings - Fork 179
Make permission checks namespace safe #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yippie
wants to merge
1
commit into
mitchspano:main
Choose a base branch
from
yippie:permission-check-namespace-safe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
trigger-actions-framework/main/default/classes/ActionUtility.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Utilities for managing actions. | ||
* | ||
* Allows for the retrieval of the current user's permissions only once during the entire transaction | ||
* Consolidates code that is shared between MetadataTriggerHandler and FinalizerHandler classes | ||
*/ | ||
|
||
public with sharing class ActionUtility { | ||
@TestVisible | ||
public static Set<String> userPermissions { | ||
get { | ||
if (userPermissions == null) { | ||
userPermissions = new Set<String>(); | ||
|
||
List<PermissionSetAssignment> permSets = [ | ||
SELECT AssigneeId, Assignee.FirstName, PermissionSetId, PermissionSet.Name | ||
FROM PermissionSetAssignment | ||
WHERE AssigneeId = :UserInfo.getUserId() | ||
]; | ||
Set<Id> permSetIds = new Set<Id>(); | ||
for (PermissionSetAssignment psa : permSets) { | ||
permSetIds.add(psa.PermissionSetId); | ||
} | ||
List<CustomPermission> customPerms = [ | ||
SELECT DeveloperName, MasterLabel, NamespacePrefix | ||
FROM CustomPermission | ||
WHERE Id IN (SELECT SetupEntityId FROM SetupEntityAccess WHERE ParentId IN :permSetIds) | ||
]; | ||
for (CustomPermission cp : customPerms) { | ||
String qualifiedPerm = String.isBlank(cp.NamespacePrefix) | ||
? cp.DeveloperName | ||
: cp.NamespacePrefix + '__' + cp.DeveloperName; | ||
userPermissions.add(qualifiedPerm); | ||
} | ||
} | ||
return userPermissions; | ||
} | ||
private set; | ||
} | ||
|
||
public static boolean isNotBypassed(String requiredPermission, String bypassPermission) { | ||
return !((requiredPermission != null && userPermissions.contains(requiredPermission)) || | ||
(bypassPermission != null && !userPermissions.contains(bypassPermission))); | ||
} | ||
|
||
public static Object getActionInstance(String className) { | ||
String namespace = ''; | ||
System.Type actionType = Type.forName(className); | ||
/** Type.forName(fullyQualifiedName) allowed some messyness and ambiguity in dealing with namespace | ||
* If config does not provide the correct namespace (likely if upgrading from older versions of this framework) we need to fallback in two scenarios | ||
* - package and class namespaced but namespace wasn't specified | ||
* - namespace is actually in the class field in the form namespace.classname | ||
*/ | ||
// try shared Namespace | ||
if (actionType == null) { | ||
// Get the namespace of the current class. | ||
String[] parts = String.valueOf(ActionUtility.class).split('\\.', 2); | ||
namespace = parts.size() == 2 ? parts[0] : ''; | ||
|
||
// try again with the new namespace | ||
actionType = Type.forName(namespace, className); | ||
} | ||
// try namespace in Class_Name field | ||
if (actionType == null) { | ||
String[] parts = className.split('\\.', 2); | ||
if (parts.size() == 2) { | ||
namespace = parts[0]; | ||
className = parts[1]; | ||
actionType = Type.forName(namespace, className); | ||
} | ||
} | ||
Object dynamicInstance = actionType.newInstance(); | ||
return dynamicInstance; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
trigger-actions-framework/main/default/classes/ActionUtility.cls-meta.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why using isNOTBypassed - it can be confusing to the user/dev if Booleans are expressed negatively.
Gentle suggestion: Update to
isBypassed
and return the negative of the output (which is already a negative, I see).