Skip to content

Commit fe9fd1a

Browse files
committed
feat: Integrate Laravel's built-in authorization Gates
- Integrate Laravel's built-in authorization Gates (#70) - Added guidance for Gates in README.md
1 parent 259a389 commit fe9fd1a

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ Route::group(['middleware' => ['http_request']], function () {
277277
});
278278
```
279279

280+
### Using Gates
281+
282+
You can use Laravel Gates to check if a user has a permission, provided that you have set an existing user instance as the currently authenticated user using `Auth::login`. See [Gates](https://laravel.com/docs/11.x/authorization#gates) for more details.
283+
284+
```php
285+
if(Gate::allows('enforcer', ['articles', 'read'])) {
286+
// The user can read articles
287+
};
288+
```
289+
280290
### Multiple enforcers
281291

282292
If you need multiple permission controls in your project, you can configure multiple enforcers.

src/LauthzServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Lauthz;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\ServiceProvider;
67
use Lauthz\Contracts\ModelLoader;
8+
use Lauthz\Facades\Enforcer;
79
use Lauthz\Loaders\ModelLoaderFactory;
810
use Lauthz\Models\Rule;
911
use Lauthz\Observers\RuleObserver;
@@ -56,5 +58,25 @@ public function register()
5658
$this->app->bind(ModelLoader::class, function($app, $config) {
5759
return ModelLoaderFactory::createFromConfig($config);
5860
});
61+
62+
$this->registerGates();
63+
}
64+
65+
/**
66+
* Register a gate that allows users to use Laravel's built-in Gate to call Enforcer.
67+
*
68+
* @return void
69+
*/
70+
protected function registerGates()
71+
{
72+
Gate::define('enforcer', function ($user, ...$args) {
73+
$identifier = $user->getAuthIdentifier();
74+
if (method_exists($user, 'getAuthzIdentifier')) {
75+
$identifier = $user->getAuthzIdentifier();
76+
}
77+
$identifier = strval($identifier);
78+
79+
return Enforcer::enforce($identifier, ...$args);
80+
});
5981
}
6082
}

tests/GatesAuthorizationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Lauthz\Tests;
4+
5+
use Illuminate\Foundation\Testing\DatabaseMigrations;
6+
use Illuminate\Support\Facades\Gate;
7+
8+
class GatesAuthorizationTest extends TestCase
9+
{
10+
use DatabaseMigrations;
11+
12+
public function testNotLogin()
13+
{
14+
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read']));
15+
}
16+
17+
public function testAfterLogin()
18+
{
19+
$this->login('alice');
20+
$this->assertTrue(Gate::allows('enforcer', ['data1', 'read']));
21+
$this->assertTrue(Gate::allows('enforcer', ['data2', 'read']));
22+
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write']));
23+
24+
$this->login('bob');
25+
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read']));
26+
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write']));
27+
}
28+
}

0 commit comments

Comments
 (0)