Je možné nakešovať autorizátor postavený nad DB?

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
Čamo
Člen | 798
+
0
-

Zdravím vás,
Vie mi niekto poradiť, či a ako sa dá nakešovať autorizátor postavený nad databázou? Vyhadzuje mi to výnimku „can not serialize PDO object…“
Autorizáto vyzerá takto:

namespace ACL;

use Nette;
use App;
use Nette\Caching\Cache;
use Tracy\Debugger;

class PermissionModel extends Nette\Security\Permission
{

	/** @var AclModel $AclModel */
	protected $aclModel;

	/** @var Nette\Caching\Cache $cache */
	protected $cache;

	/** @var string */
	protected $cacheKey;

	protected $cThis;

	/**
	 * @param AclModel $aclModel
	 * @param Cache $cache
	 * @param $cacheKey
	 */
    public function __construct( AclModel $aclModel, Cache $cache, $cacheKey )
    {
		$this->aclModel = $aclModel;
		$this->cache = $cache;
		$this->cacheKey = $cacheKey;

		$this->cThis = $this->cache->load( $this->cacheKey );

    }

	/**
	 * @return $this
	 */
	public function create()
	{
		if( $this->cThis )
		{
			return $this->cThis;
		}

		$roles = $this->aclModel->getRoles();
		foreach ($roles as $role) {
			$this->addRole($role['key_name'], $role['parent_key']);
		}

		$resources = $this->aclModel->getResources();
		foreach ($resources as $resource) {
			$this->addResource($resource['key_name'], $resource['parent_key']);
		}

		$rules = $this->aclModel->getRules();
		foreach ($rules as $rule) {
			$this->{$rule->access ? 'allow' : 'deny'}($rule->role, $rule->resource, $rule->privilege);
		}

		//$this->cache->save( $this->cacheKey, $this );

		return $this;
	}
}
CZechBoY
Člen | 3608
+
0
-

Uloz si vysledek do pole nebo neceho serializovatelnyho.

Barvoj
Člen | 60
+
+2
-

Já si necachuji celý Authorizátor ale jen data z DB:

class AuthorizatorFactory
{
	...

	/**
     * @return IAuthorizator
     */
    public function create()
    {
        $acl = new Permission();

        foreach ($this->cache->call([$this, 'getResources']) as $resource) {
            $acl->addResource($resource);
        }

        foreach ($this->cache->call([$this, 'getRoles']) as $role) {
            $acl->addRole($role);
        }

        foreach ($this->cache->call([$this, 'getRoleResources']) as $roleResource) {
            $acl->allow($roleResource['role'], $roleResource['resource']);
        }

        return $acl;
    }

    /**
     * @return array
     */
    public function getResources()
    {
        $resources = [];
        foreach ($this->resourceRepository->findAll() as $resource) {
            $resources[] = $resource->code;
        }

        return $resources;
    }

	...
}

EDIT:

Pokud chceš cachovat celou instanci Permission, tak by ti možná stačilo neextendovat Permission, ale místo toho si na něj vytvořit továrničku – tak jako to mám já.

Potom ta instance Permission nebude obsahovat žádné Modely, které by bránily serializaci..

Editoval Barvoj (22. 6. 2016 14:36)

Čamo
Člen | 798
+
0
-

Barvoj:
Díky za nakopnutie. Autorizator sa podľa všetkého(zatiaľ to tak vyzerá dá kešovať). Len som sa naozaj potreboval zbaviť tých modelov. Mám to takto keby to niekoho zaujímalo:

<?php


namespace ACL;


use Nette;
use App;
use Nette\Caching\Cache;
use Tracy\Debugger;


class PermissionModel /* extends Nette\Security\Permission */
{

	/** @var AclModel $AclModel */
	protected $aclModel;

	/** @var Nette\Caching\Cache $cache */
	protected $cache;

	/** @var string */
	protected $cacheKey;

	protected $cAuthorizer;

	/**
	 * @param AclModel $aclModel
	 * @param Cache $cache
	 * @param $cacheKey
	 */
    public function __construct( AclModel $aclModel, Cache $cache, $cacheKey )
    {
		$this->aclModel = $aclModel;
		$this->cache = $cache;
		$this->cacheKey = $cacheKey;

		$this->cAuthorizer = $this->cache->load( $this->cacheKey );

    }


	/**
	 * @desc This method exists because of cache.
	 * @return $this
	 */
	public function create()
	{
		if( $this->cAuthorizer )
		{
			Debugger::barDump('Cached authorizer');
			return $this->cAuthorizer;
		}

		$this->cAuthorizer = new Nette\Security\Permission();

		$roles = $this->aclModel->getRoles();
		foreach ($roles as $role) {
			$this->cAuthorizer->addRole($role['key_name'], $role['parent_key']);
		}

		$resources = $this->aclModel->getResources();
		foreach ($resources as $resource) {
			$this->cAuthorizer->addResource($resource['key_name'], $resource['parent_key']);
		}

		$rules = $this->aclModel->getRules();
		foreach ($rules as $rule) {
			$this->cAuthorizer->{$rule->access ? 'allow' : 'deny'}($rule->role, $rule->resource, $rule->privilege);
		}

		$this->cache->save( $this->cacheKey, $this->cAuthorizer );

		Debugger::barDump('Not cached authorizer');
		return $this->cAuthorizer;

	}
}

A v BasePresentery,

$this->user->setAuthorizator($this->authorizator->create());  // Authorizator je ten PermissionModel

Asi by sa to malo volať factory, ale nemám toľko času. Ešte raz díky.