Skip to content

Commit

Permalink
Abstraction of permissions check for access to the platform cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 10, 2022
1 parent 5d00e7f commit 3658177
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ public class OrgCache implements ICacheAdaptor
public class AccessViolationException extends Exceptions.DeveloperException {} // this looks like a config exception, but actually the system should be built
// in such a way that it's never possible to get this exception

@testVisible
private final static String CAN_ACCESS_CACHE_PERMISSION = 'ProcessesCanAccessCache';

private Boolean hasAccessToCache
{
get
{
if ( hasAccessToCache == null )
{
hasAccessToCache = PermissionsService.hasPermission( CAN_ACCESS_CACHE_PERMISSION );
hasAccessToCache = PermissionsService.hasAccessToCorePlatformCache();
}
return hasAccessToCache;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: defunt
public with sharing class SobjectCache
{
public class CacheAccessViolationException extends Exceptions.DeveloperException {} // this looks like a config exception, but actually the system should be built
Expand All @@ -9,16 +10,13 @@ public with sharing class SobjectCache
private final static String PARTITION_NAME = 'soql'; // TODO: same partition?
private final static Integer CACHE_LIFESPAN_SECONDS = 28800; // TODO: soft setting / option

@testVisible
private final static String CAN_ACCESS_SOQL_CACHE_PERMISSION = 'ProcessesCanAccessSOQLCache'; // TODO: same permission?

private Boolean hasAccessToCache
{
get
{
if ( hasAccessToCache == null )
{
hasAccessToCache = PermissionsService.hasPermission( CAN_ACCESS_SOQL_CACHE_PERMISSION );
hasAccessToCache = PermissionsService.hasAccessToCorePlatformCache();
}
return hasAccessToCache;
}
Expand Down Expand Up @@ -112,7 +110,7 @@ public with sharing class SobjectCache

private String createKeyPrefix( String subKey )
{
return cacheWrapper.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, PARTITION_NAME, subkey ) + 'x';
return cacheWrapper.createFullyQualifiedKey( PARTITION_NAME, subkey ) + 'x';
}

public class CacheRetrieval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ private without sharing class CachedSoqlExecutorTest
private static void setupAccessToSoqlCache( Boolean accessToCache )
{
ApplicationMockRegistrar.registerMockService( IPermissionsService.class )
.when( 'hasPermission' )
.withParameter( OrgCache.CAN_ACCESS_CACHE_PERMISSION )
.when( 'hasAccessToCorePlatformCache' )
.returns( accessToCache );
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public interface IPermissionsService
{
Boolean hasPermission( String customPermissionName );
Boolean hasAccessToCorePlatformCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
public with sharing class PermissionsService {

/**
* States if the user has the custom permission with the given API name.
* States if the user has rights to access the core platform cache
*
* @param String The API name of the custom permission to check the assignment of.
* @return Boolean Does the current user have the stated permission.
*/
public static Boolean hasPermission( String customPermissionName )
public static Boolean hasAccessToCorePlatformCache()
{
return service().hasPermission( customPermissionName );
return service().hasAccessToCorePlatformCache();
}

private static IPermissionsService service()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
public with sharing class PermissionsServiceImpl implements IPermissionsService
{
public Boolean hasPermission( String customPermissionName )
public Boolean hasAccessToCorePlatformCache()
{
return hasCustomPermission( 'customPermissionName' );
}

/**
* 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 )
{
return FeatureManagement.checkPermission( customPermissionName );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@
private with sharing class PermissionsServiceImplTest
{
@isTest
private static void hasPermission_givenACustomPermissionThatExists_doesNotThrowAnException() // NOPMD: test method format
private static void hasAccessToCorePlatformCache_doesNotThrowAnException() // NOPMD: test method format
{
Boolean hasPermission = PermissionsService.hasPermission( 'ProcessesCanAccessSOQLCache' );
Boolean hasPermission = PermissionsService.hasAccessToCorePlatformCache();

System.assert( true, 'hasPermission, when given a custom permission that exists, does not throw an exception' );
}

@isTest
private static void hasPermission_givenACustomPermissionThatDoesNotExists_returnsFalse() // NOPMD: test method format
{
Boolean hasPermission = PermissionsService.hasPermission( 'invalid-permission-that-does-not-exist' );

System.assertEquals( false, hasPermission, 'hasPermission, when given a custom permission that does not exist, returns false' );
System.assertNotEquals( null, hasPermission, 'hasAccessToCorePlatformCache, does not throw an exception, and returns a value' );
}
}

0 comments on commit 3658177

Please sign in to comment.