Added Casbin 2.0 support, requires Laravel 5.5 and newer.

This commit is contained in:
root 2019-10-14 10:58:07 +08:00
parent 8f437f2c2e
commit d77f1f7e88
4 changed files with 59 additions and 85 deletions

View File

@ -12,53 +12,43 @@ services:
matrix: matrix:
fast_finish: true fast_finish: true
include: include:
# Laravel 5.1 # Laravel 5.5
- php: 5.6 - php: 7.1
env: LARAVEL=5.1.* PHPUNIT=^5.7 env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.0 - php: 7.2
env: LARAVEL=5.1.* PHPUNIT=^5.7 env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.1 - php: 7.3
env: LARAVEL=5.1.* PHPUNIT=^5.7 env: LARAVEL=5.5.* PHPUNIT=~6.0
# Laravel 5.5 # Laravel 5.6
- php: 7.0 - php: 7.1
env: LARAVEL=5.5.* PHPUNIT=~6.0 env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.1 - php: 7.2
env: LARAVEL=5.5.* PHPUNIT=~6.0 env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.2 - php: 7.3
env: LARAVEL=5.5.* PHPUNIT=~6.0 env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.3
env: LARAVEL=5.5.* PHPUNIT=~6.0
# Laravel 5.6 # Laravel 5.7
- php: 7.1 - php: 7.1
env: LARAVEL=5.6.* PHPUNIT=~7.0 env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.2 - php: 7.2
env: LARAVEL=5.6.* PHPUNIT=~7.0 env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.3 - php: 7.3
env: LARAVEL=5.6.* PHPUNIT=~7.0 env: LARAVEL=5.7.* PHPUNIT=~7.5
# Laravel 5.7 # Laravel 5.8
- php: 7.1 - php: 7.1
env: LARAVEL=5.7.* PHPUNIT=~7.5 env: LARAVEL=5.8.* PHPUNIT=~7.5
- php: 7.2 - php: 7.2
env: LARAVEL=5.7.* PHPUNIT=~7.5 env: LARAVEL=5.8.* PHPUNIT=~8.0
- php: 7.3 - php: 7.3
env: LARAVEL=5.7.* PHPUNIT=~7.5 env: LARAVEL=5.8.* PHPUNIT=~8.0
# Laravel 5.8 # Laravel 6.0
- php: 7.1 - php: 7.2
env: LARAVEL=5.8.* PHPUNIT=~7.5 env: LARAVEL=6.0.* PHPUNIT=~8.0
- php: 7.2 - php: 7.3
env: LARAVEL=5.8.* PHPUNIT=~8.0 env: LARAVEL=6.0.* PHPUNIT=~8.0
- php: 7.3
env: LARAVEL=5.8.* PHPUNIT=~8.0
# Laravel 6.0
- php: 7.2
env: LARAVEL=6.0.* PHPUNIT=~8.0
- php: 7.3
env: LARAVEL=6.0.* PHPUNIT=~8.0
before_install: before_install:
- travis_retry composer self-update - travis_retry composer self-update
@ -70,9 +60,9 @@ install:
- travis_retry composer install --no-suggest --no-interaction - travis_retry composer install --no-suggest --no-interaction
script: script:
- vendor/bin/phpunit --version - vendor/bin/phpunit --version
- mkdir -p build/logs - mkdir -p build/logs
- vendor/bin/phpunit - vendor/bin/phpunit
after_script: after_script:
- travis_retry vendor/bin/php-coveralls -v - travis_retry vendor/bin/php-coveralls -v

View File

@ -10,15 +10,16 @@
], ],
"license": "Apache-2.0", "license": "Apache-2.0",
"require": { "require": {
"laravel/framework": "~5.1|~6.0", "php": ">=7.1.0",
"casbin/casbin": "~1.0", "laravel/framework": "~5.5|~6.0",
"casbin/psr3-bridge": "^1.0" "casbin/casbin": "~2.0",
"casbin/psr3-bridge": "^1.1"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0", "phpunit/phpunit": "~7.0|~8.0",
"php-coveralls/php-coveralls": "^2.1", "php-coveralls/php-coveralls": "^2.1",
"mockery/mockery": "^1.0", "mockery/mockery": "^1.0",
"laravel/laravel": "~5.1|~6.0" "laravel/laravel": "~5.5|~6.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -1,9 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace Lauthz\Adapters; namespace Lauthz\Adapters;
use Lauthz\Models\Rule; use Lauthz\Models\Rule;
use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract; use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;
use Casbin\Model\Model;
use Casbin\Persist\AdapterHelper; use Casbin\Persist\AdapterHelper;
/** /**
@ -38,7 +41,7 @@ class DatabaseAdapter implements DatabaseAdapterContract
* @param string $ptype * @param string $ptype
* @param array $rule * @param array $rule
*/ */
public function savePolicyLine($ptype, array $rule) public function savePolicyLine(string $ptype, array $rule): void
{ {
$col['ptype'] = $ptype; $col['ptype'] = $ptype;
foreach ($rule as $key => $value) { foreach ($rule as $key => $value) {
@ -52,10 +55,8 @@ class DatabaseAdapter implements DatabaseAdapterContract
* loads all policy rules from the storage. * loads all policy rules from the storage.
* *
* @param Model $model * @param Model $model
*
* @return mixed
*/ */
public function loadPolicy($model) public function loadPolicy(Model $model): void
{ {
$rows = $this->eloquent->getAllFromCache(); $rows = $this->eloquent->getAllFromCache();
@ -71,39 +72,33 @@ class DatabaseAdapter implements DatabaseAdapterContract
* saves all policy rules to the storage. * saves all policy rules to the storage.
* *
* @param Model $model * @param Model $model
*
* @return bool
*/ */
public function savePolicy($model) public function savePolicy(Model $model): void
{ {
foreach ($model->model['p'] as $ptype => $ast) { foreach ($model['p'] as $ptype => $ast) {
foreach ($ast->policy as $rule) { foreach ($ast->policy as $rule) {
$this->savePolicyLine($ptype, $rule); $this->savePolicyLine($ptype, $rule);
} }
} }
foreach ($model->model['g'] as $ptype => $ast) { foreach ($model['g'] as $ptype => $ast) {
foreach ($ast->policy as $rule) { foreach ($ast->policy as $rule) {
$this->savePolicyLine($ptype, $rule); $this->savePolicyLine($ptype, $rule);
} }
} }
return true;
} }
/** /**
* Adds a policy rule to the storage. * adds a policy rule to the storage.
* This is part of the Auto-Save feature. * This is part of the Auto-Save feature.
* *
* @param string $sec * @param string $sec
* @param string $ptype * @param string $ptype
* @param array $rule * @param array $rule
*
* @return mixed
*/ */
public function addPolicy($sec, $ptype, $rule) public function addPolicy(string $sec, string $ptype, array $rule): void
{ {
return $this->savePolicyLine($ptype, $rule); $this->savePolicyLine($ptype, $rule);
} }
/** /**
@ -112,10 +107,8 @@ class DatabaseAdapter implements DatabaseAdapterContract
* @param string $sec * @param string $sec
* @param string $ptype * @param string $ptype
* @param array $rule * @param array $rule
*
* @return mixed
*/ */
public function removePolicy($sec, $ptype, $rule) public function removePolicy(string $sec, string $ptype, array $rule): void
{ {
$count = 0; $count = 0;
@ -130,8 +123,6 @@ class DatabaseAdapter implements DatabaseAdapterContract
++$count; ++$count;
} }
} }
return $count;
} }
/** /**
@ -141,11 +132,9 @@ class DatabaseAdapter implements DatabaseAdapterContract
* @param string $sec * @param string $sec
* @param string $ptype * @param string $ptype
* @param int $fieldIndex * @param int $fieldIndex
* @param mixed ...$fieldValues * @param string ...$fieldValues
*
* @return mixed
*/ */
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues) public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
{ {
$count = 0; $count = 0;
@ -163,7 +152,5 @@ class DatabaseAdapter implements DatabaseAdapterContract
++$count; ++$count;
} }
} }
return $count;
} }
} }

View File

@ -34,13 +34,9 @@ abstract class TestCase extends BaseTestCase
$this->artisan('vendor:publish', ['--provider' => 'Lauthz\LauthzServiceProvider']); $this->artisan('vendor:publish', ['--provider' => 'Lauthz\LauthzServiceProvider']);
$this->artisan('migrate', ['--force' => true]); $this->artisan('migrate', ['--force' => true]);
if (method_exists($this, 'afterApplicationCreated')) { $this->afterApplicationCreated(function () {
$this->afterApplicationCreated(function () {
$this->initTable();
});
} else {
$this->initTable(); $this->initTable();
} });
return $this->app; return $this->app;
} }