forked from php-casbin/laravel-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Laravel's built-in Manager class and integrate authorizatio…
…n Gates - Use Laravel's built-in abstract Manager class instead of ModelLoaderFactory (php-casbin#71) - Integrate Laravel's built-in authorization Gates (php-casbin#70)
- Loading branch information
Showing
8 changed files
with
190 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Lauthz\Loaders; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Manager; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* The model loader manager. | ||
* | ||
* A model loader is responsible for a loading model from an arbitrary source. | ||
* Developers can customize loading behavior by implementing | ||
* and register it in AppServiceProvider through `app(LoaderManager::class)->extend()`. | ||
* | ||
* Built-in loader implementations include: | ||
* - FileLoader: For loading model from file. | ||
* - TextLoader: Suitable for model defined as a multi-line string. | ||
* - UrlLoader: Handles model loading from URL. | ||
* | ||
* To utilize a built-in or custom loader, set 'model.config_type' in the configuration to match one of the above types. | ||
*/ | ||
class LoaderManager extends Manager | ||
{ | ||
|
||
/** | ||
* The array of the lauthz driver configuration. | ||
* | ||
* @var array | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* Initialize configuration for the loader manager instance. | ||
* | ||
* @param array $config the lauthz driver configuration. | ||
*/ | ||
public function initFromConfig(array $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Get the default driver from the configuration. | ||
* | ||
* @return string The default driver name. | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return Arr::get($this->config, 'model.config_type', ''); | ||
} | ||
|
||
/** | ||
* Create a new TextLoader instance. | ||
* | ||
* @return TextLoader | ||
*/ | ||
public function createTextDriver() | ||
{ | ||
return new TextLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new UrlLoader instance. | ||
* | ||
* @return UrlLoader | ||
*/ | ||
public function createUrlDriver() | ||
{ | ||
return new UrlLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new FileLoader instance. | ||
* | ||
* @return FileLoader | ||
*/ | ||
public function createFileDriver() | ||
{ | ||
return new FileLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new driver instance. | ||
* | ||
* @param string $driver | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function createDriver($driver) | ||
{ | ||
if (empty($driver)) { | ||
throw new InvalidArgumentException("Unsupported empty model loader type."); | ||
} | ||
|
||
if (isset($this->customCreators[$driver])) { | ||
return $this->callCustomCreator($driver); | ||
} | ||
|
||
$method = 'create' . Str::studly($driver) . 'Driver'; | ||
|
||
if (method_exists($this, $method)) { | ||
return $this->$method(); | ||
} | ||
|
||
throw new InvalidArgumentException("Unsupported model loader type: {$driver}."); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Lauthz\Tests; | ||
|
||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Illuminate\Support\Facades\Gate; | ||
|
||
class GatesAuthorizationTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
public function testNotLogin() | ||
{ | ||
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read'])); | ||
} | ||
|
||
public function testAfterLogin() | ||
{ | ||
$this->login('alice'); | ||
$this->assertTrue(Gate::allows('enforcer', ['data1', 'read'])); | ||
$this->assertTrue(Gate::allows('enforcer', ['data2', 'read'])); | ||
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write'])); | ||
|
||
|
||
$this->login('bob'); | ||
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read'])); | ||
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write'])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters