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:
fast_finish: true
include:
# Laravel 5.1
- php: 5.6
env: LARAVEL=5.1.* PHPUNIT=^5.7
- php: 7.0
env: LARAVEL=5.1.* PHPUNIT=^5.7
- php: 7.1
env: LARAVEL=5.1.* PHPUNIT=^5.7
# Laravel 5.5
- php: 7.1
env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.2
env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.3
env: LARAVEL=5.5.* PHPUNIT=~6.0
# Laravel 5.5
- php: 7.0
env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.1
env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.2
env: LARAVEL=5.5.* PHPUNIT=~6.0
- php: 7.3
env: LARAVEL=5.5.* PHPUNIT=~6.0
# Laravel 5.6
- php: 7.1
env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.2
env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.3
env: LARAVEL=5.6.* PHPUNIT=~7.0
# Laravel 5.6
- php: 7.1
env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.2
env: LARAVEL=5.6.* PHPUNIT=~7.0
- php: 7.3
env: LARAVEL=5.6.* PHPUNIT=~7.0
# Laravel 5.7
- php: 7.1
env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.2
env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.3
env: LARAVEL=5.7.* PHPUNIT=~7.5
# Laravel 5.7
- php: 7.1
env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.2
env: LARAVEL=5.7.* PHPUNIT=~7.5
- php: 7.3
env: LARAVEL=5.7.* PHPUNIT=~7.5
# Laravel 5.8
- php: 7.1
env: LARAVEL=5.8.* PHPUNIT=~7.5
- php: 7.2
env: LARAVEL=5.8.* PHPUNIT=~8.0
- php: 7.3
env: LARAVEL=5.8.* PHPUNIT=~8.0
# Laravel 5.8
- php: 7.1
env: LARAVEL=5.8.* PHPUNIT=~7.5
- php: 7.2
env: LARAVEL=5.8.* 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
# 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:
- travis_retry composer self-update
@ -70,9 +60,9 @@ install:
- travis_retry composer install --no-suggest --no-interaction
script:
- vendor/bin/phpunit --version
- mkdir -p build/logs
- vendor/bin/phpunit
- vendor/bin/phpunit --version
- mkdir -p build/logs
- vendor/bin/phpunit
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",
"require": {
"laravel/framework": "~5.1|~6.0",
"casbin/casbin": "~1.0",
"casbin/psr3-bridge": "^1.0"
"php": ">=7.1.0",
"laravel/framework": "~5.5|~6.0",
"casbin/casbin": "~2.0",
"casbin/psr3-bridge": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0",
"phpunit/phpunit": "~7.0|~8.0",
"php-coveralls/php-coveralls": "^2.1",
"mockery/mockery": "^1.0",
"laravel/laravel": "~5.1|~6.0"
"laravel/laravel": "~5.5|~6.0"
},
"autoload": {
"psr-4": {

View File

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

View File

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