Skip to content

Commit

Permalink
Added back the permissions service
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Apr 21, 2022
1 parent 20bface commit 118f8d6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface IPermissionsService
{
Boolean hasAccessToCorePlatformCache();
}
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>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Provides the ability to check if the current user has particular permissions.
*/
public with sharing class PermissionsService {

/**
* States if the user has rights to access the core platform cache
*
* @return Boolean Does the current user have the stated permission.
*/
public static Boolean hasAccessToCorePlatformCache()
{
return service().hasAccessToCorePlatformCache();
}

private static IPermissionsService service()
{
return (IPermissionsService)Application.SERVICE.newInstance( IPermissionsService.class );
}
}
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>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public with sharing class PermissionsServiceImpl implements IPermissionsService
{
/**
* States if the user has the custom permission required to acces the core platform cache
*
* @return Boolean Does the current user have the platform cache
*/
public Boolean hasAccessToCorePlatformCache()
{
Boolean hasAccessToCorePlatformCache = false;

try
{
hasAccessToCorePlatformCache = hasCustomPermission( 'ProcessesCanAccessCache' );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return hasAccessToCorePlatformCache;
}

/**
* States if the user has the custom permission with the given API name.
*
* @param String The API name of the custom permission to check the assignment of.
* @return Boolean Does the current user have the stated permission.
*/
private Boolean hasCustomPermission( String customPermissionName )
{
Boolean hasCustomPermission = false;

try
{
hasCustomPermission = FeatureManagement.checkPermission( customPermissionName );
}
catch ( Exception e )
{
ServiceUtils.logAndRethrow( e );
}

return hasCustomPermission;
}
}
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>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@isTest
private with sharing class PermissionsServiceImplTest
{
@isTest
private static void hasAccessToCorePlatformCache_doesNotThrowAnException() // NOPMD: test method format
{
PermissionsServiceImpl permissionsService = new PermissionsServiceImpl();

Boolean hasPermission = permissionsService.hasAccessToCorePlatformCache();

System.assertNotEquals( null, hasPermission, 'hasAccessToCorePlatformCache, does not throw an exception, and returns a value' );
}
}
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>52.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 118f8d6

Please sign in to comment.