Skip to content

Commit ca0acf8

Browse files
committed
feat: test
1 parent 259a389 commit ca0acf8

File tree

6 files changed

+130
-56
lines changed

6 files changed

+130
-56
lines changed

src/EnforcerManager.php

Lines changed: 3 additions & 2 deletions
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\LoaderManager;
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(LoaderManager::class);
91+
$loader->initFromConfig($config);
9192
$loader->loadModel($model);
9293

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

src/LauthzServiceProvider.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Lauthz;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\ServiceProvider;
6-
use Lauthz\Contracts\ModelLoader;
7-
use Lauthz\Loaders\ModelLoaderFactory;
7+
use Lauthz\Facades\Enforcer;
8+
use Lauthz\Loaders\LoaderManager;
89
use Lauthz\Models\Rule;
910
use Lauthz\Observers\RuleObserver;
1011

@@ -51,10 +52,30 @@ public function register()
5152
{
5253
$this->app->singleton('enforcer', function ($app) {
5354
return new EnforcerManager($app);
55+
}) ;
56+
57+
$this->app->singleton(LoaderManager::class, function($app){
58+
return new LoaderManager($app);
5459
});
5560

56-
$this->app->bind(ModelLoader::class, function($app, $config) {
57-
return ModelLoaderFactory::createFromConfig($config);
61+
$this->registerGate();
62+
}
63+
64+
/**
65+
* Register a gate that allows users to use Laravel's built-in Gate to call Enforcer.
66+
*
67+
* @return void
68+
*/
69+
protected function registerGate()
70+
{
71+
Gate::define('enforcer', function ($user, ...$args) {
72+
$identifier = $user->getAuthIdentifier();
73+
if (method_exists($user, 'getAuthzIdentifier')) {
74+
$identifier = $user->getAuthzIdentifier();
75+
}
76+
$identifier = strval($identifier);
77+
78+
return Enforcer::enforce($identifier, ...$args);
5879
});
5980
}
6081
}

src/Loaders/LoaderManager.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
class LoaderManager extends Manager
11+
{
12+
13+
/**
14+
* The array of the lauthz driver configuration.
15+
*
16+
* @var array
17+
*/
18+
protected $config;
19+
20+
/**
21+
* Initialize configuration for the loader manager instance.
22+
*
23+
* @param array $config the lauthz driver configuration.
24+
*/
25+
public function initFromConfig(array $config)
26+
{
27+
$this->config = $config;
28+
}
29+
30+
/**
31+
* Get the default driver from the configuration.
32+
*
33+
* @return string The default driver name.
34+
*/
35+
public function getDefaultDriver(){
36+
return Arr::get($this->config, 'model.config_type', '');
37+
}
38+
39+
/**
40+
* Create a new TextLoader instance.
41+
*
42+
* @return TextLoader
43+
*/
44+
public function createTextDriver(){
45+
return new TextLoader($this->config);
46+
}
47+
48+
/**
49+
* Create a new UrlLoader instance.
50+
*
51+
* @return UrlLoader
52+
*/
53+
public function createUrlDriver(){
54+
return new UrlLoader($this->config);
55+
}
56+
57+
/**
58+
* Create a new FileLoader instance.
59+
*
60+
* @return FileLoader
61+
*/
62+
public function createFileDriver(){
63+
return new FileLoader($this->config);
64+
}
65+
66+
/**
67+
* Create a new driver instance.
68+
*
69+
* @param string $driver
70+
* @return mixed
71+
*
72+
* @throws \InvalidArgumentException
73+
*/
74+
protected function createDriver($driver)
75+
{
76+
if (empty($driver)) {
77+
throw new InvalidArgumentException("Unsupported empty model loader type.");
78+
}
79+
80+
if (isset($this->customCreators[$driver])) {
81+
return $this->callCustomCreator($driver);
82+
}
83+
84+
$method = 'create'.Str::studly($driver).'Driver';
85+
86+
if (method_exists($this, $method)) {
87+
return $this->$method();
88+
}
89+
90+
throw new InvalidArgumentException("Unsupported model loader type: {$driver}.");
91+
}
92+
}

src/Loaders/ModelLoaderFactory.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/DatabaseAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\DatabaseMigrations;
77
use Casbin\Persist\Adapters\Filter;
88
use Casbin\Exceptions\InvalidFilterTypeException;
9+
use Illuminate\Support\Facades\Gate;
910

1011
class DatabaseAdapterTest extends TestCase
1112
{

tests/ModelLoaderTest.php

Lines changed: 9 additions & 2 deletions
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\LoaderManager;
67
use InvalidArgumentException;
78
use RuntimeException;
89

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

70-
public function testBadUlrConnection(): void
71+
public function testBadUrlConnection(): void
7172
{
7273
$this->initUrlConfig();
7374
$this->app['config']->set('lauthz.basic.model.config_url', 'http://filenoexists');
@@ -95,11 +96,17 @@ protected function initTextConfig(): void
9596
}
9697

9798
protected function initCustomConfig(): void {
98-
$this->app['config']->set('lauthz.second.model.config_loader_class', '\Lauthz\Loaders\TextLoader');
99+
$this->app['config']->set('lauthz.second.model.config_type', 'custom');
99100
$this->app['config']->set(
100101
'lauthz.second.model.config_text',
101102
$this->getModelText()
102103
);
104+
105+
$config = $this->app['config']->get('lauthz.second');
106+
$loader = $this->app->make(LoaderManager::class);
107+
$loader->extend('custom', function () use ($config) {
108+
return new \Lauthz\Loaders\TextLoader($config);
109+
});
103110
}
104111

105112
protected function getModelText(): string

0 commit comments

Comments
 (0)