Skip to content

Commit

Permalink
Added Casbin 2.0 support, requires Laravel 5.5 and newer.
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Oct 14, 2019
1 parent 8f437f2 commit d77f1f7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 85 deletions.
84 changes: 37 additions & 47 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
41 changes: 14 additions & 27 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -38,7 +41,7 @@ public function __construct(Rule $eloquent)
* @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) {
Expand All @@ -52,10 +55,8 @@ public function savePolicyLine($ptype, array $rule)
* 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();

Expand All @@ -71,39 +72,33 @@ public function loadPolicy($model)
* 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);
}

/**
Expand All @@ -112,10 +107,8 @@ public function addPolicy($sec, $ptype, $rule)
* @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;

Expand All @@ -130,8 +123,6 @@ public function removePolicy($sec, $ptype, $rule)
++$count;
}
}

return $count;
}

/**
Expand All @@ -141,11 +132,9 @@ public function removePolicy($sec, $ptype, $rule)
* @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;

Expand All @@ -163,7 +152,5 @@ public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
++$count;
}
}

return $count;
}
}
8 changes: 2 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ public function createApplication()
$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;
}
Expand Down

0 comments on commit d77f1f7

Please sign in to comment.