2017-12-04 01:12:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
use Psr\Log\InvalidArgumentException;
|
|
|
|
use SilverStripe\Core\Flushable;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-12-07 04:07:40 +01:00
|
|
|
use SilverStripe\Core\Cache\MemberCacheFlusher;
|
2017-12-04 01:12:13 +01:00
|
|
|
|
|
|
|
class InheritedPermissionFlusher extends DataExtension implements Flushable
|
|
|
|
{
|
|
|
|
/**
|
2017-12-07 04:12:05 +01:00
|
|
|
* @var MemberCacheFlusher[]
|
2017-12-04 01:12:13 +01:00
|
|
|
*/
|
|
|
|
protected $services = [];
|
|
|
|
|
|
|
|
/**
|
2017-12-06 09:59:27 +01:00
|
|
|
* Flush all MemberCacheFlusher services
|
2017-12-04 01:12:13 +01:00
|
|
|
*/
|
|
|
|
public static function flush()
|
|
|
|
{
|
|
|
|
singleton(__CLASS__)->flushCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DataObject $owner
|
|
|
|
*/
|
|
|
|
public function setOwner($owner)
|
|
|
|
{
|
|
|
|
if (!$owner instanceof Member && !$owner instanceof Group) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'%s can only be applied to %s or %s',
|
|
|
|
__CLASS__,
|
|
|
|
Member::class,
|
|
|
|
Group::class
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setOwner($owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param MemberCacheFlusher[] $services
|
2017-12-06 10:03:05 +01:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @return $this
|
2017-12-04 01:12:13 +01:00
|
|
|
*/
|
|
|
|
public function setServices($services)
|
|
|
|
{
|
|
|
|
foreach ($services as $service) {
|
2017-12-07 04:07:40 +01:00
|
|
|
if (!$service instanceof MemberCacheFlusher) {
|
2017-12-04 01:12:13 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'%s.services must contain only %s instances. %s provided.',
|
|
|
|
__CLASS__,
|
2017-12-07 04:07:40 +01:00
|
|
|
MemberCacheFlusher::class,
|
2017-12-04 01:12:13 +01:00
|
|
|
get_class($service)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->services = $services;
|
2017-12-06 10:03:05 +01:00
|
|
|
|
|
|
|
return $this;
|
2017-12-04 01:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-07 04:12:05 +01:00
|
|
|
* @return MemberCacheFlusher[]
|
2017-12-04 01:12:13 +01:00
|
|
|
*/
|
|
|
|
public function getServices()
|
|
|
|
{
|
|
|
|
return $this->services;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-07 04:12:05 +01:00
|
|
|
* Flushes all registered MemberCacheFlusher services
|
2017-12-04 01:12:13 +01:00
|
|
|
*/
|
|
|
|
public function flushCache()
|
|
|
|
{
|
|
|
|
$ids = $this->getMemberIDList();
|
2017-12-11 04:46:21 +01:00
|
|
|
foreach ($this->getServices() as $service) {
|
2017-12-07 04:07:40 +01:00
|
|
|
$service->flushMemberCache($ids);
|
2017-12-04 01:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of member IDs that need their permissions flushed
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
protected function getMemberIDList()
|
|
|
|
{
|
2017-12-06 10:03:05 +01:00
|
|
|
if (!$this->owner || !$this->owner->exists()) {
|
2017-12-04 01:12:13 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->owner instanceof Group) {
|
2017-12-11 05:49:23 +01:00
|
|
|
return $this->owner->Members()->column('ID');
|
2017-12-04 01:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$this->owner->ID];
|
|
|
|
}
|
2017-12-11 04:46:21 +01:00
|
|
|
}
|