Skip to content
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

fix: Improve some coding #79

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: PHPUnit

on:
push:
Expand Down Expand Up @@ -46,6 +46,9 @@ jobs:
- php: 8.3
laravel: 11.*
phpunit: ~10.5
- php: 8.4
laravel: 11.*
phpunit: ~10.5

name: Laravel${{ matrix.laravel }}-PHP${{ matrix.php }}

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p align="center">
<a href="https://github.com/php-casbin/laravel-authz/actions">
<img src="https://github.com/php-casbin/laravel-authz/actions/workflows/build.yml/badge.svg?branch=master" alt="Build Status">
<img src="https://github.com/php-casbin/laravel-authz/actions/workflows/phpunit.yml/badge.svg" alt="PHPUnit Status">
</a>
<a href="https://coveralls.io/github/php-casbin/laravel-authz">
<img src="https://coveralls.io/repos/github/php-casbin/laravel-authz/badge.svg" alt="Coverage Status">
Expand Down Expand Up @@ -123,6 +123,13 @@ if (Enforcer::enforce("eve", "articles", "edit")) {

```

By default, [Gate](https://laravel.com/docs/11.x/authorization#gates) checks will be automatically intercepted
. You can check if a user has a permission with Laravel's default `can` function:

```php
$user->can('articles,read');
```

### Using Enforcer Api

It provides a very rich api to facilitate various operations on the Policy:
Expand Down
11 changes: 4 additions & 7 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Casbin\Persist\AdapterHelper;
use DateTime;
use Casbin\Exceptions\InvalidFilterTypeException;
use Illuminate\Support\Facades\DB;

/**
* DatabaseAdapter.
Expand All @@ -28,14 +27,14 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
/**
* @var bool
*/
private $filtered = false;
private bool $filtered = false;

/**
* Rules eloquent model.
*
* @var Rule
*/
protected $eloquent;
protected Rule $eloquent;

/**
* the DatabaseAdapter constructor.
Expand Down Expand Up @@ -331,10 +330,8 @@ public function loadFilteredPolicy(Model $model, $filter): void
}
$rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();
foreach ($rows as $row) {
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
}));
$row = array_filter($row, static fn($value): bool => !is_null($value) && $value !== '');
$line = implode(', ', array_filter($row, static fn ($val): bool => '' != $val && !is_null($val)));
$this->loadPolicyLine(trim($line), $model);
}
$this->setFiltered(true);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/GroupAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GroupAdd extends Command
public function handle()
{
$params = explode(',', $this->argument('policy'));
array_walk($params, function (&$value) {
array_walk($params, static function (&$value): void {
$value = trim($value);
});
$ret = Enforcer::addGroupingPolicy(...$params);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PolicyAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PolicyAdd extends Command
public function handle()
{
$params = explode(',', $this->argument('policy'));
array_walk($params, function (&$value) {
array_walk($params, static function (&$value): void {
$value = trim($value);
});
$ret = Enforcer::addPolicy(...$params);
Expand Down
9 changes: 5 additions & 4 deletions src/EnforcerLocalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Foundation\Application;
use Lauthz\Facades\Enforcer;

class EnforcerLocalizer
{
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
* @var Application
*/
protected $app;
protected Application $app;

/**
* Create a new localizer instance.
*
* @param \Illuminate\Foundation\Application $app
* @param Application $app
*/
public function __construct($app)
public function __construct(Application $app)
{
$this->app = $app;
}
Expand Down
11 changes: 6 additions & 5 deletions src/EnforcerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Casbin\Log\Logger\DefaultLogger;
use Lauthz\Contracts\Factory;
use Lauthz\Models\Rule;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Lauthz\Loaders\ModelLoaderManager;
Expand All @@ -20,23 +21,23 @@ class EnforcerManager implements Factory
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
* @var \
*/
protected $app;
protected Application $app;

/**
* The array of created "guards".
*
* @var array
*/
protected $guards = [];
protected array $guards = [];

/**
* Create a new manager instance.
*
* @param \Illuminate\Foundation\Application $app
* @param Application $app
*/
public function __construct($app)
public function __construct(Application $app)
{
$this->app = $app;
}
Expand Down
12 changes: 3 additions & 9 deletions src/LauthzServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,11 @@ protected function bootObserver()
*/
public function register()
{
$this->app->singleton('enforcer', function ($app) {
return new EnforcerManager($app);
});
$this->app->singleton('enforcer', fn ($app) => new EnforcerManager($app));

$this->app->singleton(ModelLoaderManager::class, function ($app) {
return new ModelLoaderManager($app);
});
$this->app->singleton(ModelLoaderManager::class, fn ($app) => new ModelLoaderManager($app));

$this->app->singleton(EnforcerLocalizer::class, function ($app) {
return new EnforcerLocalizer($app);
});
$this->app->singleton(EnforcerLocalizer::class, fn ($app) => new EnforcerLocalizer($app));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileLoader implements ModelLoader
*
* @var string
*/
private $filePath;
private string $filePath;

/**
* Constructor to initialize the file path.
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/TextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TextLoader implements ModelLoader
*
* @var string
*/
private $text;
private string $text;

/**
* Constructor to initialize the model text.
Expand Down
2 changes: 1 addition & 1 deletion src/Loaders/UrlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UrlLoader implements ModelLoader
*
* @var string
*/
private $url;
private string $url;

/**
* Constructor to initialize the url path.
Expand Down
4 changes: 1 addition & 3 deletions src/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public function __construct(array $attributes = [], $guard = '')
*/
public function getAllFromCache()
{
$get = function () {
return $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
};
$get = fn () => $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
if (!$this->config('cache.enabled', false)) {
return $get();
}
Expand Down
Loading