Skip to content

feat: Integrate Laravel's built-in authorization Gates #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ Route::group(['middleware' => ['http_request']], function () {
});
```

### Using Gates

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.

```php
if(Gate::allows('enforcer', ['articles', 'read'])) {
// The user can read articles
};
```

### Multiple enforcers

If you need multiple permission controls in your project, you can configure multiple enforcers.
Expand Down
22 changes: 22 additions & 0 deletions src/LauthzServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Lauthz;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Lauthz\Contracts\ModelLoader;
use Lauthz\Facades\Enforcer;
use Lauthz\Loaders\ModelLoaderFactory;
use Lauthz\Models\Rule;
use Lauthz\Observers\RuleObserver;
Expand Down Expand Up @@ -56,5 +58,25 @@ public function register()
$this->app->bind(ModelLoader::class, function($app, $config) {
return ModelLoaderFactory::createFromConfig($config);
});

$this->registerGates();
}

/**
* Register a gate that allows users to use Laravel's built-in Gate to call Enforcer.
*
* @return void
*/
protected function registerGates()
{
Gate::define('enforcer', function ($user, ...$args) {
$identifier = $user->getAuthIdentifier();
if (method_exists($user, 'getAuthzIdentifier')) {
$identifier = $user->getAuthzIdentifier();
}
$identifier = strval($identifier);

return Enforcer::enforce($identifier, ...$args);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make full use of the Gate. Here the policy should be registered to the Gate.
We can check if a user has a permission with Laravel's default can function: $user->can('edit');

Therefore, the Gate check should be intercepted here:

Gate::before(function (User $user, string $act, $obj...) {});

Gate::after(function (User $user, string $act, $obj...) {});

See: https://laravel.com/docs/11.x/authorization#intercepting-gate-checks

And then

  1. Whether to use Gate is a configurable item
  2. Use after or before interception is also a configurable item

}
}
28 changes: 28 additions & 0 deletions tests/GatesAuthorizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Lauthz\Tests;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Gate;

class GatesAuthorizationTest extends TestCase
{
use DatabaseMigrations;

public function testNotLogin()
{
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read']));
}

public function testAfterLogin()
{
$this->login('alice');
$this->assertTrue(Gate::allows('enforcer', ['data1', 'read']));
$this->assertTrue(Gate::allows('enforcer', ['data2', 'read']));
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write']));

$this->login('bob');
$this->assertFalse(Gate::allows('enforcer', ['data1', 'read']));
$this->assertTrue(Gate::allows('enforcer', ['data2', 'write']));
}
}
Loading