Zend: using EntityManager in assertion

143 Views Asked by At

I'm using Zend Framework 2.4 with BjyAuthorize module and trying to make an Assertion class for ACL rule. The problem is that I need to make a query to database to make a decision on user's permission (I'm using Doctrine ORM). I tried to pass the ObjectManager to Assertion, but got this error:

Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php:48 Stack trace: #0 [internal function]: PDO->__sleep() #1 /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php(48): serialize(Object(Zend\Permissions\Acl\Acl)) #2 /var/www/vendor/zendframework/zendframework/library/Zend/Cache/Storage/Plugin/Serializer.php(102): Zend\Serializer\Adapter\PhpSerialize->serialize(Object(Zend\Permissions\Acl\Acl)) #3 [internal function]: Zend\Cache\Storage\Plugin\Serializer->onWriteItemPre(Object(Zend\Cache\Storage\Event)) #4 /var/www/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Cache\Storage\Event)) #5 /var/www/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('setItem.pre', Object(Zend\Cache\Stor in /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php on line 48

OK, it is what it says. My question is: how can I workaround this?

Here's code example:

Factory:

<?php
namespace Foo\Permissions\Acl\Assertion;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Foo\Permissions\Acl\Assertion\IsUserDrunkAssertion;

class IsUserDrunkAssertionFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $em = $serviceLocator->get('doctrine.entitymanager.orm_default');
        $assertion = new IsUserDrunkAssertion();
        $assertion->setObjectManager($em);
        return $assertion;
    }
}

Assertion:

<?php
namespace Foo\Permissions\Acl\Assertion;

use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Doctrine\Common\Persistence\ObjectManager;
// ...

class IsUserDrunkAssertion implements AssertionInterface
{
    protected $objectManager;

    public function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
        return $this;
    }

    public function getObjectManager()
    {
        return $this->objectManager;
    }

    public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
    {
        $decision = // ... make a query
        return $decision;
    }
}

So far I've been able to come up with only two ideas: either pass ServiceLocator to Assertion or make all queries in Factory and then pass it's result as aggregated values. Is there a better way?

0

There are 0 best solutions below