Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Created August 29, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlebkowski/63b8cb5c4b40c6778186f947b140575a to your computer and use it in GitHub Desktop.
Save mlebkowski/63b8cb5c4b40c6778186f947b140575a to your computer and use it in GitHub Desktop.
<?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