php-casbin/laravel-authz

How to use loadFilteredPolicy without calling loadPolicy?

Closed this issue · 3 comments

Is there any way to call Enforcer::loadFilteredPolicy($filter); without calling loadPolicy on Enforcer creation?

In CoreEnforcer.php there is such logic:

// Do not initialize the full policy when using a filtered adapter
$ok = $this->adapter instanceof FilteredAdapter ? $this->adapter->isFiltered() : false;
if (!\is_null($this->adapter) && !$ok) {
$this->loadPolicy();
}

but $this->adapter->isFiltered() is false, because is called on Enforcer creation and $this->setFiltered(true) is called in loadFilteredPolicy

@sdemirov You can initialize the adapter before the Enforcer is created

$this->app->singleton(DatabaseAdapter.class, function($app){
    $adapter = new DatabaseAdapter(new Rule([], $name));
    $adapter->setFiltered(true);
    return  $adapter;
});

Great! Thank you very much.