Skip to content

Commit 4d49aef

Browse files
authored
feat: use Laravel's built-in Manager class (#72)
- Use Laravel's built-in abstract Manager class instead of ModelLoaderFactory (#71)
1 parent 259a389 commit 4d49aef

7 files changed

+149
-61
lines changed

Diff for: src/EnforcerManager.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Casbin\Model\Model;
88
use Casbin\Log\Log;
99
use Lauthz\Contracts\Factory;
10-
use Lauthz\Contracts\ModelLoader;
1110
use Lauthz\Models\Rule;
1211
use Illuminate\Support\Arr;
1312
use InvalidArgumentException;
13+
use Lauthz\Loaders\ModelLoaderManager;
1414

1515
/**
1616
* @mixin \Casbin\Enforcer
@@ -87,7 +87,8 @@ protected function resolve($name)
8787
}
8888

8989
$model = new Model();
90-
$loader = $this->app->make(ModelLoader::class, $config);
90+
$loader = $this->app->make(ModelLoaderManager::class);
91+
$loader->initFromConfig($config);
9192
$loader->loadModel($model);
9293

9394
$adapter = Arr::get($config, 'adapter');

Diff for: src/LauthzServiceProvider.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace Lauthz;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Lauthz\Contracts\ModelLoader;
7-
use Lauthz\Loaders\ModelLoaderFactory;
6+
use Lauthz\Loaders\ModelLoaderManager;
87
use Lauthz\Models\Rule;
98
use Lauthz\Observers\RuleObserver;
109

@@ -53,8 +52,8 @@ public function register()
5352
return new EnforcerManager($app);
5453
});
5554

56-
$this->app->bind(ModelLoader::class, function($app, $config) {
57-
return ModelLoaderFactory::createFromConfig($config);
55+
$this->app->singleton(ModelLoaderManager::class, function ($app) {
56+
return new ModelLoaderManager($app);
5857
});
5958
}
6059
}

Diff for: src/Loaders/ModelLoaderFactory.php

-48
This file was deleted.

Diff for: src/Loaders/ModelLoaderManager.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Lauthz\Loaders;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
7+
use Illuminate\Support\Manager;
8+
use InvalidArgumentException;
9+
10+
/**
11+
* The model loader manager.
12+
*
13+
* A model loader is responsible for a loading model from an arbitrary source.
14+
* Developers can customize loading behavior by implementing
15+
* and register the custom loader in AppServiceProvider through `app(LoaderManager::class)->extend()`.
16+
*
17+
* Built-in loader implementations include:
18+
* - FileLoader: For loading model from file.
19+
* - TextLoader: Suitable for model defined as a multi-line string.
20+
* - UrlLoader: Handles model loading from URL.
21+
*
22+
* To utilize a built-in or custom loader, set 'model.config_type' in the configuration to match one of the above types.
23+
*/
24+
class ModelLoaderManager extends Manager
25+
{
26+
27+
/**
28+
* The array of the lauthz driver configuration.
29+
*
30+
* @var array
31+
*/
32+
protected $config;
33+
34+
/**
35+
* Initialize configuration for the loader manager instance.
36+
*
37+
* @param array $config the lauthz driver configuration.
38+
*/
39+
public function initFromConfig(array $config)
40+
{
41+
$this->config = $config;
42+
}
43+
44+
/**
45+
* Get the default driver from the configuration.
46+
*
47+
* @return string The default driver name.
48+
*/
49+
public function getDefaultDriver()
50+
{
51+
return Arr::get($this->config, 'model.config_type', '');
52+
}
53+
54+
/**
55+
* Create a new TextLoader instance.
56+
*
57+
* @return TextLoader
58+
*/
59+
public function createTextDriver()
60+
{
61+
return new TextLoader($this->config);
62+
}
63+
64+
/**
65+
* Create a new UrlLoader instance.
66+
*
67+
* @return UrlLoader
68+
*/
69+
public function createUrlDriver()
70+
{
71+
return new UrlLoader($this->config);
72+
}
73+
74+
/**
75+
* Create a new FileLoader instance.
76+
*
77+
* @return FileLoader
78+
*/
79+
public function createFileDriver()
80+
{
81+
return new FileLoader($this->config);
82+
}
83+
84+
/**
85+
* Create a new driver instance.
86+
*
87+
* @param string $driver
88+
* @return mixed
89+
*
90+
* @throws \InvalidArgumentException
91+
*/
92+
protected function createDriver($driver)
93+
{
94+
if(empty($driver)) {
95+
throw new InvalidArgumentException('Unsupported empty model loader type.');
96+
}
97+
98+
if (isset($this->customCreators[$driver])) {
99+
return $this->callCustomCreator($driver);
100+
}
101+
$method = 'create' . Str::studly($driver) . 'Driver';
102+
if (method_exists($this, $method)) {
103+
return $this->$method();
104+
}
105+
106+
throw new InvalidArgumentException("Unsupported model loader type: {$driver}.");
107+
}
108+
}

Diff for: tests/DatabaseAdapterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function testLoadFilteredPolicy()
309309
$this->assertEquals([
310310
['bob', 'data2', 'write']
311311
], Enforcer::getPolicy());
312-
312+
313313
// Filter
314314
$filter = new Filter(['v2'], ['read']);
315315
Enforcer::loadFilteredPolicy($filter);

Diff for: tests/ModelLoaderTest.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lauthz\Tests;
44

55
use Lauthz\Facades\Enforcer;
6+
use Lauthz\Loaders\ModelLoaderManager;
67
use InvalidArgumentException;
78
use RuntimeException;
89

@@ -67,7 +68,15 @@ public function testEmptyLoaderType(): void
6768
$this->assertFalse(Enforcer::enforce('alice', 'data', 'read'));
6869
}
6970

70-
public function testBadUlrConnection(): void
71+
public function testNotExistLoaderType(): void
72+
{
73+
$this->app['config']->set('lauthz.basic.model.config_type', 'not_exist');
74+
$this->expectException(InvalidArgumentException::class);
75+
76+
$this->assertFalse(Enforcer::enforce('alice', 'data', 'read'));
77+
}
78+
79+
public function testBadUrlConnection(): void
7180
{
7281
$this->initUrlConfig();
7382
$this->app['config']->set('lauthz.basic.model.config_url', 'http://filenoexists');
@@ -94,12 +103,20 @@ protected function initTextConfig(): void
94103
);
95104
}
96105

97-
protected function initCustomConfig(): void {
98-
$this->app['config']->set('lauthz.second.model.config_loader_class', '\Lauthz\Loaders\TextLoader');
106+
protected function initCustomConfig(): void
107+
{
108+
$this->app['config']->set('lauthz.second.model.config_type', 'custom');
99109
$this->app['config']->set(
100110
'lauthz.second.model.config_text',
101111
$this->getModelText()
102112
);
113+
114+
$config = $this->app['config']->get('lauthz.second');
115+
$loader = $this->app->make(ModelLoaderManager::class);
116+
117+
$loader->extend('custom', function () use ($config) {
118+
return new \Lauthz\Loaders\TextLoader($config);
119+
});
103120
}
104121

105122
protected function getModelText(): string
@@ -118,4 +135,4 @@ protected function getModelText(): string
118135
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
119136
EOT;
120137
}
121-
}
138+
}

Diff for: tests/RequestMiddlewareTest.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Lauthz\Middlewares\RequestMiddleware;
66
use Illuminate\Foundation\Testing\DatabaseMigrations;
77
use Illuminate\Http\Request;
8+
use Lauthz\Facades\Enforcer;
89
use Lauthz\Models\Rule;
910

1011
class RequestMiddlewareTest extends TestCase
@@ -34,11 +35,19 @@ public function testAfterLogin()
3435
$this->assertEquals($this->middleware(Request::create('/foo1/123', 'PUT')), 'Unauthorized Exception');
3536

3637
$this->assertEquals($this->middleware(Request::create('/proxy', 'GET')), 'Unauthorized Exception');
38+
39+
Enforcer::guard('second')->addPolicy('alice', '/foo1/*', '(GET|POST)');
40+
41+
$this->assertEquals($this->middleware(Request::create('/foo1/123', 'GET'), 'second'), 200);
42+
$this->assertEquals($this->middleware(Request::create('/foo1/123', 'POST'), 'second'), 200);
43+
$this->assertEquals($this->middleware(Request::create('/foo1/123', 'PUT'), 'second'), 'Unauthorized Exception');
44+
45+
$this->assertEquals($this->middleware(Request::create('/proxy', 'GET'), 'second'), 'Unauthorized Exception');
3746
}
3847

39-
protected function middleware($request)
48+
protected function middleware($request, ...$guards)
4049
{
41-
return parent::runMiddleware(RequestMiddleware::class, $request);
50+
return parent::runMiddleware(RequestMiddleware::class, $request, ...$guards);
4251
}
4352

4453
protected function initConfig()
@@ -62,6 +71,8 @@ protected function initConfig()
6271
m = g(r.sub, p.sub) && r.sub == p.sub && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act)
6372
EOT;
6473
$this->app['config']->set('lauthz.basic.model.config_text', $text);
74+
$this->app['config']->set('lauthz.second.model.config_type', 'text');
75+
$this->app['config']->set('lauthz.second.model.config_text', $text);
6576
}
6677

6778
protected function initTable()

0 commit comments

Comments
 (0)