-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrate Laravel's built-in authorization Gates (#73)
- Integrate Laravel's built-in authorization Gates (#70) - Added guidance for Gates in README.md
- Loading branch information
Showing
9 changed files
with
211 additions
and
2 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,61 @@ | ||
<?php | ||
|
||
namespace Lauthz; | ||
|
||
use Illuminate\Contracts\Auth\Access\Authorizable; | ||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Lauthz\Facades\Enforcer; | ||
|
||
class EnforcerLocalizer | ||
{ | ||
/** | ||
* The application instance. | ||
* | ||
* @var \Illuminate\Foundation\Application | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* Create a new localizer instance. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
*/ | ||
public function __construct($app) | ||
{ | ||
$this->app = $app; | ||
} | ||
|
||
/** | ||
* Register the localizer based on the configuration. | ||
*/ | ||
public function register() | ||
{ | ||
if ($this->app->config->get('lauthz.localizer.enabled_register_at_gates')) { | ||
$this->registerAtGate(); | ||
} | ||
} | ||
|
||
/** | ||
* Register the localizer at the gate. | ||
*/ | ||
protected function registerAtGate() | ||
{ | ||
$this->app->make(Gate::class)->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 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,40 @@ | ||
<?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 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); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Lauthz\Facades\Enforcer; | ||
use Lauthz\Tests\TestCase; | ||
|
||
class EnforcerLocalizerTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
public function testRegisterAtGates() | ||
{ | ||
$user = $this->user('alice'); | ||
$this->assertTrue($user->can('data1,read')); | ||
$this->assertFalse($user->can('data1,write')); | ||
$this->assertFalse($user->cannot('data2,read')); | ||
|
||
Enforcer::guard('second')->addPolicy('alice', 'data1', 'read'); | ||
$this->assertTrue($user->can('data1,read', 'second')); | ||
$this->assertFalse($user->can('data3,read', 'second')); | ||
} | ||
|
||
public function testNotLogin() | ||
{ | ||
$this->assertFalse(app(Gate::class)->allows('data1,read')); | ||
$this->assertTrue(app(Gate::class)->forUser($this->user('alice'))->allows('data1,read')); | ||
$this->assertFalse(app(Gate::class)->forUser($this->user('bob'))->allows('data1,read')); | ||
} | ||
|
||
public function testAfterLogin() | ||
{ | ||
$this->login('alice'); | ||
$this->assertTrue(app(Gate::class)->allows('data1,read')); | ||
$this->assertTrue(app(Gate::class)->allows('data2,read')); | ||
$this->assertTrue(app(Gate::class)->allows('data2,write')); | ||
|
||
$this->login('bob'); | ||
$this->assertFalse(app(Gate::class)->allows('data1,read')); | ||
$this->assertTrue(app(Gate::class)->allows('data2,write')); | ||
} | ||
|
||
public function initConfig() | ||
{ | ||
parent::initConfig(); | ||
$this->app['config']->set('lauthz.second.model.config_type', 'text'); | ||
$this->app['config']->set( | ||
'lauthz.second.model.config_text', | ||
$this->getModelText() | ||
); | ||
} | ||
|
||
protected function getModelText(): string | ||
{ | ||
return <<<EOT | ||
[request_definition] | ||
r = sub, obj, act | ||
[policy_definition] | ||
p = sub, obj, act | ||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
[matchers] | ||
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act | ||
EOT; | ||
} | ||
} |
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