php-casbin / laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Apache License 2.0
272 stars 46 forks source link

How to use loadFilteredPolicy without calling loadPolicy? #64

Closed sdemirov closed 2 months ago

sdemirov commented 2 months ago

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

hsluoyz commented 2 months ago

@leeqvip

leeqvip commented 2 months ago

@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;
});
sdemirov commented 2 months ago

Great! Thank you very much.