-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/php-casbin/laravel-authz …
…into feature-use-gates-1.0
- Loading branch information
Showing
14 changed files
with
330 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Lauthz; | ||
|
||
use Illuminate\Contracts\Auth\Access\Authorizable; | ||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Lauthz\Facades\Enforcer; | ||
|
||
class EnforcerLocalizer | ||
{ | ||
public function registerAtGates(Gate $gate) | ||
{ | ||
$gate->before(function (Authorizable $user, string $ability, array $guards) { | ||
/** @var \Illuminate\Contracts\Auth\Authenticatable $user */ | ||
$identifier = $user->getAuthIdentifier(); | ||
if (method_exists($user, 'getAuthzIdentifier')) { | ||
/** @var \Lauthz\Tests\Models\User $user */ | ||
$identifier = $user->getAuthzIdentifier(); | ||
} | ||
$identifier = strval($identifier); | ||
$ability = explode(',', $ability); | ||
if (empty($guards)) { | ||
return Enforcer::enforce($identifier, ...$ability); | ||
} | ||
|
||
foreach ($guards as $guard) { | ||
return Enforcer::guard($guard)->enforce($identifier, ...$ability); | ||
} | ||
}); | ||
} | ||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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 the custom loader 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 ModelLoaderManager 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 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,52 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Lauthz\Tests\TestCase; | ||
|
||
class EnforcerCustomLocalizerTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
public function testCustomRegisterAtGatesBefore() | ||
{ | ||
$user = $this->user("alice"); | ||
$this->assertFalse($user->can('data3,read')); | ||
|
||
app(Gate::class)->before(function () { | ||
return true; | ||
}); | ||
|
||
$this->assertTrue($user->can('data3,read')); | ||
} | ||
|
||
public function testCustomRegisterAtGatesAfter() | ||
{ | ||
$user = $this->user("alice"); | ||
$this->assertFalse($user->can('data3,read')); | ||
|
||
app(Gate::class)->after(function () { | ||
return true; | ||
}); | ||
|
||
$this->assertTrue($user->can('data3,read')); | ||
} | ||
|
||
public function testCustomRegisterAtGatesDefine() | ||
{ | ||
$user = $this->user("alice"); | ||
$this->assertFalse($user->can('data3,read')); | ||
|
||
app(Gate::class)->define('data3,read', function () { | ||
return true; | ||
}); | ||
|
||
$this->assertTrue($user->can('data3,read')); | ||
} | ||
|
||
public function initConfig() | ||
{ | ||
parent::initConfig(); | ||
$this->app['config']->set('lauthz.localizer.enabled_register_at_gates', false); | ||
} | ||
} |
Oops, something went wrong.