Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions Classes/Cache/PermissionCache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace JBartels\BeAcl\Cache;

/***************************************************************
Expand All @@ -24,18 +25,18 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\SingletonInterface;

/**
* A cache for storing permission data for a given Backend user
* A cache for storing permission data for a given Backend user.
*/
class PermissionCache implements SingletonInterface
{

const CACHE_IDENTIFIER_PERMISSIONS = 'tx_be_acl_permission_cache';

/**
* The Backend user for which this cache stores the permissions
* The Backend user for which this cache stores the permissions.
*
* @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
*/
Expand All @@ -51,7 +52,7 @@ class PermissionCache implements SingletonInterface
*
* @var array
*/
protected $permissionCacheFirstLevel = array();
protected $permissionCacheFirstLevel = [];

/**
* This cache will be used during single requests.
Expand Down Expand Up @@ -84,24 +85,22 @@ public function disableFirstLevelCache()
/**
* Updates the last permission update timestamp which makes all
* previously stored caches invalid.
*
* @return void
*/
public function flushCache()
{
$this->permissionCacheFirstLevel = array();
$this->permissionCacheFirstLevel = [];
$this->timestampUtility->updateTimestamp();
}

/**
* Returns the stored permissions clause cache entry if one is found
* Returns the stored permissions clause cache entry if one is found.
*
* @param string $requestedPermissions
*
* @return string|null If a cache entry is found the permissions clause will be returned, otherwise NULL
*/
public function getPermissionsClause($requestedPermissions)
{

if (!isset($this->backendUser)) {
return null;
}
Expand All @@ -117,16 +116,15 @@ public function getPermissionsClause($requestedPermissions)

if ($this->isValidCacheData($cacheData)) {
return $cacheData->getPermissionClause($requestedPermissions);
} else {
return null;
}
return null;
}

/**
* Sets the Backend user for which the cache entries will be managed.
*
* @param \TYPO3\CMS\Core\Authentication\BackendUserAuthentication $backendUser The Backend user for which the
* cache is managed
* cache is managed
*/
public function setBackendUser($backendUser)
{
Expand All @@ -147,11 +145,9 @@ public function setPermissionCache($permissionCache)
*
* @param string $requestedPermissions
* @param string $permissionsClause
* @return void
*/
public function setPermissionsClause($requestedPermissions, $permissionsClause)
{

if (!isset($this->backendUser)) {
return;
}
Expand All @@ -164,7 +160,7 @@ public function setPermissionsClause($requestedPermissions, $permissionsClause)
$cacheData = $this->getCacheDataForCurrentUser();

if (!$this->isValidCacheData($cacheData)) {
$cacheData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('JBartels\\BeAcl\\Cache\\PermissionCacheData');
$cacheData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(PermissionCacheData::class);
}

$cacheData->setPermissionClause($requestedPermissions, $permissionsClause);
Expand All @@ -188,7 +184,6 @@ public function setTimestampUtility($timestampUtility)
*/
protected function getCacheDataForCurrentUser()
{

$cacheIdentifier = $this->getCacheIdentifier();
if ($this->permissionCacheSecondLevel->has($cacheIdentifier)) {
return $this->permissionCacheSecondLevel->get($cacheIdentifier);
Expand All @@ -202,21 +197,22 @@ protected function getCacheDataForCurrentUser()
* cache in the users session data.
*
* @param string $requestedPermissions
*
* @return string
*
* @throws \JBartels\BeAcl\Exception\RuntimeException
*/
protected function getCacheIdentifier($requestedPermissions = '')
{

if (!isset($this->backendUser)) {
throw new \JBartels\BeAcl\Exception\RuntimeException('The Backend user needs to be initializes before the cache identifier can be generated.');
}

$usergroup_cached_list = str_replace( ',', '_', $this->backendUser->user['usergroup_cached_list'] );
$usergroup_cached_list = str_replace(',', '_', $this->backendUser->user['usergroup_cached_list']);
$identifier = static::CACHE_IDENTIFIER_PERMISSIONS . '_' . $this->backendUser->user['uid'] . '_' . $usergroup_cached_list;

$requestedPermissions = trim($requestedPermissions);
if ($requestedPermissions !== '') {
if ('' !== $requestedPermissions) {
$identifier .= '_' . $requestedPermissions;
}

Expand All @@ -229,22 +225,22 @@ protected function getCacheIdentifier($requestedPermissions = '')
protected function initializeRequiredClasses()
{
/** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */
$cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
$cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(CacheManager::class);
$this->setPermissionCache($cacheManager->getCache('tx_be_acl_permissions'));
/** @var TimestampUtility $timestampUtility */
$timestampUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('JBartels\\BeAcl\\Cache\\TimestampUtility');
$timestampUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(TimestampUtility::class);
$this->setTimestampUtility($timestampUtility);
}

/**
* Returns TRUE if the given cache data is valid
* Returns TRUE if the given cache data is valid.
*
* @param PermissionCacheData $cacheData
*
* @return bool
*/
protected function isValidCacheData($cacheData)
{

if (!isset($cacheData)) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions Classes/Cache/PermissionCacheData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace JBartels\BeAcl\Cache;

/***************************************************************
Expand Down Expand Up @@ -29,7 +30,6 @@
*/
class PermissionCacheData
{

/**
* Array containing the results of the different permission clauses.
*
Expand All @@ -50,18 +50,18 @@ class PermissionCacheData
public function __construct()
{
$this->timestamp = time();
$this->permissionClauseCache = array();
$this->permissionClauseCache = [];
}

/**
* Returns the matching permissions clause or NULL if none is stored.
*
* @param $requestedPermissions
*
* @return string|null
*/
public function getPermissionClause($requestedPermissions)
{

if (array_key_exists($requestedPermissions, $this->permissionClauseCache)) {
$permissionsClause = $this->permissionClauseCache[$requestedPermissions];
if (!empty($permissionsClause)) {
Expand Down
19 changes: 8 additions & 11 deletions Classes/Cache/TimestampUtility.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace JBartels\BeAcl\Cache;

/***************************************************************
Expand All @@ -24,6 +25,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\SingletonInterface;

/**
Expand All @@ -32,7 +34,6 @@
*/
class TimestampUtility implements SingletonInterface
{

const CACHE_IDENTIFIER_TIMESTAMP = 'last_permission_change_timestamp';

/**
Expand All @@ -55,17 +56,17 @@ class TimestampUtility implements SingletonInterface
* timestamp.
*
* @param int $timestamp
*
* @return bool
*/
public function permissionTimestampIsValid($timestamp)
{

$this->initializeCache();

$lastPermissionChangeTimestamp = $this->getLastPermissionChangeTimestampFromCache();
$timestamp = intval($timestamp);

return ($lastPermissionChangeTimestamp < $timestamp);
return $lastPermissionChangeTimestamp < $timestamp;
}

/**
Expand All @@ -77,10 +78,9 @@ public function setTimestampCache($timestampCache)
}

/**
* Updates the timestamp in the cache with the current timestamp
* Updates the timestamp in the cache with the current timestamp.
*
* @param int $offset
* @return void
*/
public function updateTimestamp($offset = 0)
{
Expand All @@ -97,7 +97,6 @@ public function updateTimestamp($offset = 0)
*/
protected function getLastPermissionChangeTimestampFromCache()
{

// Return directly if timestamp is found in the first level cache.
if (isset($this->timestampCacheFirstLevel)) {
return $this->timestampCacheFirstLevel;
Expand All @@ -107,26 +106,24 @@ protected function getLastPermissionChangeTimestampFromCache()
if ($this->timestampCache->has(static::CACHE_IDENTIFIER_TIMESTAMP)) {
$timestamp = (int)$this->timestampCache->get(static::CACHE_IDENTIFIER_TIMESTAMP);
$this->timestampCacheFirstLevel = $timestamp;

return $timestamp;
}

return 0;
}

/**
* Initializes the timestamp cache
*
* @return void
* Initializes the timestamp cache.
*/
protected function initializeCache()
{

if (isset($this->timestampCache)) {
return;
}

/** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */
$cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\Core\\Cache\\CacheManager');
$cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(CacheManager::class);
$this->setTimestampCache($cacheManager->getCache('tx_be_acl_timestamp'));
}
}
Loading