mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Cache warming in InheritedPermissions::getCachePermissions()
Simplify Group::Members() code Remove cms-only config
This commit is contained in:
parent
2be902ef2f
commit
33b2d50d59
@ -38,7 +38,3 @@ SilverStripe\Core\Injector\Injector:
|
|||||||
Authenticators:
|
Authenticators:
|
||||||
cms: '%$SilverStripe\Security\MemberAuthenticator\CMSMemberAuthenticator'
|
cms: '%$SilverStripe\Security\MemberAuthenticator\CMSMemberAuthenticator'
|
||||||
SilverStripe\Security\IdentityStore: '%$SilverStripe\Security\AuthenticationHandler'
|
SilverStripe\Security\IdentityStore: '%$SilverStripe\Security\AuthenticationHandler'
|
||||||
SilverStripe\Security\InheritedPermissionFlusher:
|
|
||||||
properties:
|
|
||||||
Services:
|
|
||||||
- '%$SilverStripe\Security\PermissionChecker.sitetree'
|
|
@ -305,7 +305,7 @@ class Group extends DataObject
|
|||||||
* including all members which are "inherited" from children groups of this record.
|
* including all members which are "inherited" from children groups of this record.
|
||||||
* See {@link DirectMembers()} for retrieving members without any inheritance.
|
* See {@link DirectMembers()} for retrieving members without any inheritance.
|
||||||
*
|
*
|
||||||
* @param String $filter
|
* @param string $filter
|
||||||
* @return ManyManyList
|
* @return ManyManyList
|
||||||
*/
|
*/
|
||||||
public function Members($filter = '')
|
public function Members($filter = '')
|
||||||
@ -330,15 +330,9 @@ class Group extends DataObject
|
|||||||
|
|
||||||
// Now set all children groups as a new foreign key
|
// Now set all children groups as a new foreign key
|
||||||
$familyIDs = $this->collateFamilyIDs();
|
$familyIDs = $this->collateFamilyIDs();
|
||||||
if (!empty($familyIDs)) {
|
$result = $result->forForeignID($familyIDs);
|
||||||
$groups = Group::get()->byIDs($familyIDs);
|
|
||||||
$groupIDs = $groups->column('ID');
|
return $result->where($filter);
|
||||||
if (!empty($groupIDs)) {
|
|
||||||
$result = $result->forForeignID($groupIDs)->where($filter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -450,7 +444,7 @@ class Group extends DataObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This isn't a decendant of SiteTree, but needs this in case
|
* This isn't a descendant of SiteTree, but needs this in case
|
||||||
* the group is "reorganised";
|
* the group is "reorganised";
|
||||||
*/
|
*/
|
||||||
public function cmsCleanup_parentChanged()
|
public function cmsCleanup_parentChanged()
|
||||||
@ -626,6 +620,8 @@ class Group extends DataObject
|
|||||||
/**
|
/**
|
||||||
* Returns all of the children for the CMS Tree.
|
* Returns all of the children for the CMS Tree.
|
||||||
* Filters to only those groups that the current user can edit
|
* Filters to only those groups that the current user can edit
|
||||||
|
*
|
||||||
|
* @return ArrayList
|
||||||
*/
|
*/
|
||||||
public function AllChildrenIncludingDeleted()
|
public function AllChildrenIncludingDeleted()
|
||||||
{
|
{
|
||||||
@ -635,6 +631,7 @@ class Group extends DataObject
|
|||||||
|
|
||||||
if ($children) {
|
if ($children) {
|
||||||
foreach ($children as $child) {
|
foreach ($children as $child) {
|
||||||
|
/** @var DataObject $child */
|
||||||
if ($child->canView()) {
|
if ($child->canView()) {
|
||||||
$filteredChildren->push($child);
|
$filteredChildren->push($child);
|
||||||
}
|
}
|
||||||
|
@ -94,9 +94,7 @@ class InheritedPermissionFlusher extends DataExtension implements Flushable
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($this->owner instanceof Group) {
|
if ($this->owner instanceof Group) {
|
||||||
return $this->owner->Members()->exists()
|
return $this->owner->Members()->column('ID');
|
||||||
? $this->owner->Members()->column('ID')
|
|
||||||
: null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$this->owner->ID];
|
return [$this->owner->ID];
|
||||||
|
@ -10,6 +10,7 @@ use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|||||||
use SilverStripe\Versioned\Versioned;
|
use SilverStripe\Versioned\Versioned;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use SilverStripe\Core\Cache\MemberCacheFlusher;
|
use SilverStripe\Core\Cache\MemberCacheFlusher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates batch permissions for nested objects for:
|
* Calculates batch permissions for nested objects for:
|
||||||
* - canView: Supports 'Anyone' type
|
* - canView: Supports 'Anyone' type
|
||||||
@ -125,7 +126,8 @@ class InheritedPermissions implements PermissionChecker, MemberCacheFlusher
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the cache for this instance only
|
* Clear the cache for this instance only
|
||||||
* @param array $ids A list of member IDs
|
*
|
||||||
|
* @param array $memberIDs A list of member IDs
|
||||||
*/
|
*/
|
||||||
public function flushMemberCache($memberIDs = null)
|
public function flushMemberCache($memberIDs = null)
|
||||||
{
|
{
|
||||||
@ -703,17 +705,25 @@ class InheritedPermissions implements PermissionChecker, MemberCacheFlusher
|
|||||||
/**
|
/**
|
||||||
* Gets the permission from cache
|
* Gets the permission from cache
|
||||||
*
|
*
|
||||||
* @param $cacheKey
|
* @param string $cacheKey
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getCachePermissions($cacheKey)
|
protected function getCachePermissions($cacheKey)
|
||||||
{
|
{
|
||||||
|
// Check local cache
|
||||||
if (isset($this->cachePermissions[$cacheKey])) {
|
if (isset($this->cachePermissions[$cacheKey])) {
|
||||||
return $this->cachePermissions[$cacheKey];
|
return $this->cachePermissions[$cacheKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check persistent cache
|
||||||
if ($this->cacheService) {
|
if ($this->cacheService) {
|
||||||
return $this->cacheService->get($cacheKey);
|
$result = $this->cacheService->get($cacheKey);
|
||||||
|
|
||||||
|
// Warm local cache
|
||||||
|
if ($result) {
|
||||||
|
$this->cachePermissions[$cacheKey] = $result;
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -721,12 +731,13 @@ class InheritedPermissions implements PermissionChecker, MemberCacheFlusher
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a cache key for a member and type
|
* Creates a cache key for a member and type
|
||||||
* @param $type
|
*
|
||||||
* @param $memberID
|
* @param string $type
|
||||||
|
* @param int $memberID
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function generateCacheKey($type, $memberID)
|
protected function generateCacheKey($type, $memberID)
|
||||||
{
|
{
|
||||||
return "{$type}-{$memberID}";
|
return "{$type}-{$memberID}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,7 +325,7 @@ class InheritedPermissionsTest extends SapphireTest
|
|||||||
$viewKey1 = $this->generateCacheKey($permissionChecker, InheritedPermissions::VIEW, $member1->ID);
|
$viewKey1 = $this->generateCacheKey($permissionChecker, InheritedPermissions::VIEW, $member1->ID);
|
||||||
$viewKey2 = $this->generateCacheKey($permissionChecker, InheritedPermissions::VIEW, $member2->ID);
|
$viewKey2 = $this->generateCacheKey($permissionChecker, InheritedPermissions::VIEW, $member2->ID);
|
||||||
|
|
||||||
foreach([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
foreach ([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
||||||
$this->assertNull($cache->get($key));
|
$this->assertNull($cache->get($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,14 +341,14 @@ class InheritedPermissionsTest extends SapphireTest
|
|||||||
|
|
||||||
unset($permissionChecker);
|
unset($permissionChecker);
|
||||||
|
|
||||||
foreach([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
foreach ([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
||||||
$this->assertNotNull($cache->get($key));
|
$this->assertNotNull($cache->get($key));
|
||||||
}
|
}
|
||||||
$permissionChecker = new InheritedPermissions(TestPermissionNode::class, $cache);
|
$permissionChecker = new InheritedPermissions(TestPermissionNode::class, $cache);
|
||||||
|
|
||||||
// Non existent ID
|
// Non existent ID
|
||||||
$permissionChecker->flushMemberCache('dummy');
|
$permissionChecker->flushMemberCache('dummy');
|
||||||
foreach([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
foreach ([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
||||||
$this->assertNotNull($cache->get($key));
|
$this->assertNotNull($cache->get($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,10 +363,9 @@ class InheritedPermissionsTest extends SapphireTest
|
|||||||
|
|
||||||
// Nuclear
|
// Nuclear
|
||||||
$permissionChecker->flushMemberCache();
|
$permissionChecker->flushMemberCache();
|
||||||
foreach([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
foreach ([$editKey1, $editKey2, $viewKey1, $viewKey2] as $key) {
|
||||||
$this->assertNull($cache->get($key));
|
$this->assertNull($cache->get($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateCacheKey(InheritedPermissions $inst, $type, $memberID)
|
protected function generateCacheKey(InheritedPermissions $inst, $type, $memberID)
|
||||||
@ -377,5 +376,4 @@ class InheritedPermissionsTest extends SapphireTest
|
|||||||
|
|
||||||
return $method->invokeArgs($inst, [$type, $memberID]);
|
return $method->invokeArgs($inst, [$type, $memberID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user