Skip to content

Commit

Permalink
Add AclManager to make it easier to create/update ACLs
Browse files Browse the repository at this point in the history
  • Loading branch information
curiosity26 committed Nov 5, 2018
1 parent f1477ea commit be58ebe
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ composer.phar

/phpunit.xml
/Tests/test_db.sqlite
/Tests/cache/
24 changes: 21 additions & 3 deletions Helper/AclHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Curiosity26\AclHelperBundle\QueryBuilder\AclHelperQueryBuilder;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;

class AclHelper
{
Expand All @@ -23,10 +24,19 @@ class AclHelper
*/
private $registry;

public function __construct(RegistryInterface $registry, AclHelperQueryBuilder $queryBuilder)
{
$this->registry = $registry;
/**
* @var MutableAclProviderInterface
*/
private $aclProvider;

public function __construct(
RegistryInterface $registry,
AclHelperQueryBuilder $queryBuilder,
MutableAclProviderInterface $provider
) {
$this->registry = $registry;
$this->queryBuilder = $queryBuilder;
$this->aclProvider = $provider;
}

/**
Expand All @@ -40,4 +50,12 @@ public function createAgent(string $className): AclHelperAgent

return new AclHelperAgent($className, $manager, $this->queryBuilder);
}

/**
* @return AclManager
*/
public function createAclManager()
{
return new AclManager($this->aclProvider);
}
}
347 changes: 347 additions & 0 deletions Helper/AclManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
<?php
/**
* Created by PhpStorm.
* User: alex.boyce
* Date: 11/5/18
* Time: 12:30 PM
*/

namespace Curiosity26\AclHelperBundle\Helper;

use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;

/**
* Class AclManager
*
* @package Curiosity26\AclHelperBundle\Helper
*/
class AclManager
{
/**
* @var MutableAclProviderInterface
*/
private $aclProvider;

/**
* @var MutableAclInterface
*/
private $acl;

/**
* AclManager constructor.
*
* @param MutableAclProviderInterface $provider
*/
public function __construct(MutableAclProviderInterface $provider)
{
$this->aclProvider = $provider;
}

/**
* @param $object
*
* @return $this
*/
public function aclFor($object)
{
if (!$object instanceof ObjectIdentity) {
$object = ObjectIdentity::fromDomainObject($object);
}

try {
$this->acl = $this->aclProvider->findAcl($object);
} catch (AclNotFoundException $e) {
$this->acl = $this->aclProvider->createAcl($object);
}

return $this;
}

/**
* @param SecurityIdentityInterface $identity
* @param int $mask
* @param int $index
* @param bool $granting
* @param null|string $strategy
*
* @return $this
*/
public function insertClassAce(
SecurityIdentityInterface $identity,
int $mask,
$index = 0,
bool $granting = true,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->insertClassAce($identity, $mask, $index, $granting, $strategy);

return $this;
}

/**
* @param SecurityIdentityInterface $identity
* @param int $mask
* @param int $index
* @param bool $granting
* @param null|string $strategy
*
* @return $this
*/
public function insertObjectAce(
SecurityIdentityInterface $identity,
int $mask,
$index = 0,
bool $granting = true,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->insertObjectAce($identity, $mask, $index, $granting, $strategy);

return $this;
}

/**
* @param string $field
* @param SecurityIdentityInterface $identity
* @param int $mask
* @param int $index
* @param bool $granting
* @param null|string $strategy
*
* @return $this
*/
public function insertClassFieldAce(
string $field,
SecurityIdentityInterface $identity,
int $mask,
$index = 0,
bool $granting = true,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->insertClassFieldAce($field, $identity, $mask, $index, $granting, $strategy);

return $this;
}

/**
* @param string $field
* @param SecurityIdentityInterface $identity
* @param int $mask
* @param int $index
* @param bool $granting
* @param null|string $strategy
*
* @return $this
*/
public function insertObjectFieldAce(
string $field,
SecurityIdentityInterface $identity,
int $mask,
$index = 0,
bool $granting = true,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->insertObjectFieldAce($field, $identity, $mask, $index, $granting, $strategy);

return $this;
}

/**
* @param $index
* @param int $mask
* @param null|string $strategy
*
* @return $this
*/
public function updateClassAce(
$index,
int $mask,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->updateClassAce($index, $mask, $strategy);

return $this;
}

/**
* @param $index
* @param int $mask
* @param null|string $strategy
*
* @return $this
*/
public function updateObjectAce(
$index,
int $mask,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->updateObjectAce($index, $mask, $strategy);

return $this;
}

/**
* @param $index
* @param string $field
* @param int $mask
* @param null|string $strategy
*
* @return $this
*/
public function updateClassFieldAce(
$index,
string $field,
int $mask,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->updateClassFieldAce($index, $field, $mask, $strategy);

return $this;
}

/**
* @param $index
* @param string $field
* @param int $mask
* @param null|string $strategy
*
* @return $this
*/
public function updateObjectFieldAce(
$index,
string $field,
int $mask,
?string $strategy = null
) {
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->updateObjectFieldAce($index, $field, $mask, $strategy);

return $this;
}

/**
* @param $index
*
* @return $this
*/
public function deleteClassAce($index)
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->deleteClassAce($index);

return $this;
}

/**
* @param $index
*
* @return $this
*/
public function deleteObjectAce($index)
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->deleteObjectAce($index);

return $this;
}

/**
* @param $index
* @param $field
*
* @return $this
*/
public function deleteClassFieldAce($index, $field)
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->deleteClassFieldAce($index, $field);

return $this;
}

/**
* @param $index
* @param $field
*
* @return $this
*/
public function deleteObjectFieldAce($index, $field)
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->acl->deleteObjectFieldAce($index, $field);

return $this;
}

/**
* @return $this
*/
public function save()
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

$this->aclProvider->updateAcl($this->acl);

return $this;
}

/**
* @return null|MutableAclInterface
*/
public function getAcl(): ?MutableAclInterface
{
if (null === $this->acl) {
throw new \RuntimeException("Find or create an ACL using aclFor() first.");
}

return $this->acl;
}
}
Loading

0 comments on commit be58ebe

Please sign in to comment.