Created
August 29, 2017 13:30
-
-
Save mlebkowski/63b8cb5c4b40c6778186f947b140575a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AclModeratorsUpdater | |
{ | |
/** | |
* @var MutableAclProviderInterface | |
*/ | |
private $provider; | |
public function __construct(MutableAclProviderInterface $provider) | |
{ | |
$this->provider = $provider; | |
} | |
public function updateAcl(HasModeratorsInterface $entity) | |
{ | |
$modified = false; | |
try { | |
$acl = $this->provider->findAcl($entity); | |
} catch (AclNotFoundException $e) { | |
$acl = $this->provider->createAcl($entity); | |
$modified = true; | |
} | |
$aces = $acl->getObjectAces(); | |
$permissions = MaskBuilder::MASK_DELETE | MaskBuilder::MASK_EDIT | MaskBuilder::MASK_VIEW; | |
foreach ($entity->getModerators() as $user) { | |
$userIdentity = UserSecurityIdentity::fromAccount($user); | |
foreach ($aces as $idx => $ace) { | |
if ($ace->getSecurityIdentity()->equals($userIdentity)) { | |
if ($ace->getMask() !== $permissions) { | |
$acl->updateObjectAce($idx, $permissions); | |
$modified = true; | |
} | |
unset($aces[$idx]); | |
break 2; // break out of the user loop! | |
} | |
} | |
$acl->insertObjectAce($userIdentity, $permissions); | |
$modified = true; | |
} | |
foreach ($aces as $idx => $ace) { | |
$acl->deleteObjectAce($idx); | |
$modified = true; | |
} | |
if ($modified) { | |
$this->provider->updateAcl($acl); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment