Skip to content

Commit

Permalink
Merge pull request #2 from php-casbin/analysis-zYp1Wo
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
leeqvip committed Nov 3, 2018
2 parents e1fec81 + 020eeb0 commit 13fe7e7
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 35 deletions.
13 changes: 7 additions & 6 deletions examples/demo.php
@@ -1,21 +1,22 @@
<?php

require_once './vendor/autoload.php';

use Casbin\Enforcer;
use Casbin\Util\Log;

Log::$enableLog = true;

$e = new Enforcer(__DIR__ . '/examples/modelandpolicy/basic_model.conf', __DIR__ . "/examples/modelandpolicy/basic_policy.csv");
$e = new Enforcer(__DIR__.'/examples/modelandpolicy/basic_model.conf', __DIR__.'/examples/modelandpolicy/basic_policy.csv');

$sub = "alice"; // 想要访问资源的用户。$cfg
$obj = "data1"; // 将被访问的资源。buildRoleLinks
$act = "read"; // 用户对资源执行的操作。
$sub = 'alice'; // 想要访问资源的用户。$cfg
$obj = 'data1'; // 将被访问的资源。buildRoleLinks
$act = 'read'; // 用户对资源执行的操作。

if ($e->enforce($sub, $obj, $act) === true) {
// 允许 alice 读取 data1
echo "允许 alice 读取 data1";
echo '允许 alice 读取 data1';
} else {
// 拒绝请求, 显示错误
echo "拒绝请求, 显示错误";
echo '拒绝请求, 显示错误';
}
6 changes: 2 additions & 4 deletions src/Config/Config.php
Expand Up @@ -71,13 +71,13 @@ private function parseBuffer($buf)
$buf = preg_replace('/[\r\n]+/', PHP_EOL, $buf);
$buf = explode(PHP_EOL, $buf);

for ($i = 0; $i <= \count($buf); ++$i) {
for ($i = 0; $i <= \count($buf); $i++) {
if ($canWrite) {
$this->write($section, $lineNum, $buffer);
$canWrite = false;
}

++$lineNum;
$lineNum++;
$line = isset($buf[$i]) ? $buf[$i] : '';
if ($i == \count($buf)) {
if (\strlen($buffer) > 0) {
Expand Down Expand Up @@ -128,8 +128,6 @@ private function write($section, $lineNum, &$b)
$this->addConfig($section, $option, $value);

$b = '';

return;
}

public function getString($key)
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Assertion.php
Expand Up @@ -3,8 +3,8 @@
namespace Casbin\Model;

use Casbin\Exceptions\CasbinException;
use Casbin\Util\Log;
use Casbin\Rbac\RoleManager;
use Casbin\Util\Log;

/**
* Assertion.
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Model.php
Expand Up @@ -71,11 +71,11 @@ private function getKeySuffix($i)
private function loadSection($cfg, $sec)
{
$i = 1;
for (;;) {
for (; ;) {
if (!$this->loadAssertion($cfg, $sec, $sec.$this->getKeySuffix($i))) {
break;
} else {
++$i;
$i++;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util/BuiltinOperations.php
Expand Up @@ -63,7 +63,7 @@ public function keyMatch3($key1, $key2)
$key2 = str_replace(['/', '/*'], ['\/', '/.*'], $key2);

$pattern = '/(.*)\{[^/]+\}(.*)/g';
for (;;) {
for (; ;) {
if (!strstr($key2, '/{')) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Util.php
Expand Up @@ -37,7 +37,7 @@ public static function arrayRemoveDuplicates(&$s)
if (!isset($found[$x])) {
$found[$x] = true;
$s[$j] = $s[$i];
++$j;
$j++;
}
}
$s = \array_slice($s, 0, $j);
Expand Down
34 changes: 18 additions & 16 deletions tests/EnforcerTest.php
@@ -1,20 +1,22 @@
<?php

namespace Casbin\Tests;

use Casbin\Enforcer;
use PHPUnit\Framework\TestCase;

/**
* EnforcerTest
* EnforcerTest.
*
* @author techlee@qq.com
*/
class EnforcerTest extends TestCase
{
private $modelAndPolicyPath = __DIR__ . '/../examples/modelandpolicy';
private $modelAndPolicyPath = __DIR__.'/../examples/modelandpolicy';

public function testEnforceBasic()
{
$e = new Enforcer($this->modelAndPolicyPath . '/basic_model.conf', $this->modelAndPolicyPath . '/basic_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/basic_model.conf', $this->modelAndPolicyPath.'/basic_policy.csv');

$this->assertEquals($e->enforce('alice', 'data1', 'read'), true);
$this->assertEquals($e->enforce('alice', 'data2', 'read'), false);
Expand All @@ -24,14 +26,14 @@ public function testEnforceBasic()

public function testEnforceBasicWithRoot()
{
$e = new Enforcer($this->modelAndPolicyPath . '/basic_with_root_model.conf', $this->modelAndPolicyPath . '/basic_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/basic_with_root_model.conf', $this->modelAndPolicyPath.'/basic_policy.csv');

$this->assertEquals($e->enforce('root', 'any', 'any'), true);
}

public function testEnforceBasicWithoutResources()
{
$e = new Enforcer($this->modelAndPolicyPath . '/basic_without_resources_model.conf', $this->modelAndPolicyPath . '/basic_without_resources_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/basic_without_resources_model.conf', $this->modelAndPolicyPath.'/basic_without_resources_policy.csv');

$this->assertEquals($e->enforce('alice', 'read'), true);
$this->assertEquals($e->enforce('alice', 'write'), false);
Expand All @@ -41,7 +43,7 @@ public function testEnforceBasicWithoutResources()

public function testEnforceBasicWithoutUsers()
{
$e = new Enforcer($this->modelAndPolicyPath . '/basic_without_users_model.conf', $this->modelAndPolicyPath . '/basic_without_users_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/basic_without_users_model.conf', $this->modelAndPolicyPath.'/basic_without_users_policy.csv');

$this->assertEquals($e->enforce('data1', 'read'), true);
$this->assertEquals($e->enforce('data1', 'write'), false);
Expand All @@ -51,15 +53,15 @@ public function testEnforceBasicWithoutUsers()

public function testEnforceIpMatch()
{
$e = new Enforcer($this->modelAndPolicyPath . '/ipmatch_model.conf', $this->modelAndPolicyPath . '/ipmatch_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/ipmatch_model.conf', $this->modelAndPolicyPath.'/ipmatch_policy.csv');

$this->assertEquals($e->enforce('192.168.2.1', 'data1', 'read'), true);
$this->assertEquals($e->enforce('192.168.3.1', 'data1', 'read'), false);
}

public function testEnforceKeyMatch()
{
$e = new Enforcer($this->modelAndPolicyPath . '/keymatch_model.conf', $this->modelAndPolicyPath . '/keymatch_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/keymatch_model.conf', $this->modelAndPolicyPath.'/keymatch_policy.csv');

$this->assertEquals($e->enforce('alice', '/alice_data/test', 'GET'), true);
$this->assertEquals($e->enforce('alice', '/bob_data/test', 'GET'), false);
Expand All @@ -70,15 +72,15 @@ public function testEnforceKeyMatch()

public function testEnforceKeyMatch2()
{
$e = new Enforcer($this->modelAndPolicyPath . '/keymatch2_model.conf', $this->modelAndPolicyPath . '/keymatch2_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/keymatch2_model.conf', $this->modelAndPolicyPath.'/keymatch2_policy.csv');

$this->assertEquals($e->enforce('alice', '/alice_data/resource', 'GET'), true);
$this->assertEquals($e->enforce('alice', '/alice_data2/123/using/456', 'GET'), true);
}

public function testEnforcePriority()
{
$e = new Enforcer($this->modelAndPolicyPath . '/priority_model.conf', $this->modelAndPolicyPath . '/priority_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/priority_model.conf', $this->modelAndPolicyPath.'/priority_policy.csv');

$this->assertEquals($e->enforce('alice', 'data1', 'read'), true);
$this->assertEquals($e->enforce('alice', 'data1', 'write'), false);
Expand All @@ -93,14 +95,14 @@ public function testEnforcePriority()

public function testEnforcePriorityIndeterminate()
{
$e = new Enforcer($this->modelAndPolicyPath . '/priority_model.conf', $this->modelAndPolicyPath . '/priority_indeterminate_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/priority_model.conf', $this->modelAndPolicyPath.'/priority_indeterminate_policy.csv');

$this->assertEquals($e->enforce('alice', 'data1', 'read'), false);
}

public function testEnforceRbac()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_model.conf', $this->modelAndPolicyPath . '/rbac_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/rbac_model.conf', $this->modelAndPolicyPath.'/rbac_policy.csv');
$this->assertEquals($e->enforce('alice', 'data1', 'read'), true);
$this->assertEquals($e->enforce('bob', 'data2', 'write'), true);
$this->assertEquals($e->enforce('alice', 'data2', 'read'), true);
Expand All @@ -109,7 +111,7 @@ public function testEnforceRbac()

public function testEnforceRbacWithDeny()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_with_deny_model.conf', $this->modelAndPolicyPath . '/rbac_with_deny_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/rbac_with_deny_model.conf', $this->modelAndPolicyPath.'/rbac_with_deny_policy.csv');
$this->assertEquals($e->enforce('alice', 'data1', 'read'), true);
$this->assertEquals($e->enforce('bob', 'data2', 'write'), true);
$this->assertEquals($e->enforce('alice', 'data2', 'read'), true);
Expand All @@ -118,7 +120,7 @@ public function testEnforceRbacWithDeny()

public function testEnforceRbacWithDomains()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_with_domains_model.conf', $this->modelAndPolicyPath . '/rbac_with_domains_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/rbac_with_domains_model.conf', $this->modelAndPolicyPath.'/rbac_with_domains_policy.csv');

$this->assertEquals($e->enforce('alice', 'domain1', 'data1', 'read'), true);
$this->assertEquals($e->enforce('alice', 'domain1', 'data1', 'write'), true);
Expand All @@ -132,14 +134,14 @@ public function testEnforceRbacWithDomains()

public function testEnforceRbacWithNotDeny()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_with_not_deny_model.conf', $this->modelAndPolicyPath . '/rbac_with_deny_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/rbac_with_not_deny_model.conf', $this->modelAndPolicyPath.'/rbac_with_deny_policy.csv');

$this->assertEquals($e->enforce('alice', 'data2', 'write'), false);
}

public function testEnforceRbacWithResourceRoles()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_with_resource_roles_model.conf', $this->modelAndPolicyPath . '/rbac_with_resource_roles_policy.csv');
$e = new Enforcer($this->modelAndPolicyPath.'/rbac_with_resource_roles_model.conf', $this->modelAndPolicyPath.'/rbac_with_resource_roles_policy.csv');

$this->assertEquals($e->enforce('alice', 'data1', 'read'), true);
$this->assertEquals($e->enforce('alice', 'data1', 'write'), true);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Config/ConfigTest.php
Expand Up @@ -2,8 +2,8 @@

namespace Casbin\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Casbin\Config\Config;
use PHPUnit\Framework\TestCase;

/**
* EnforcerTest.
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Model/PolicyTest.php
Expand Up @@ -2,8 +2,8 @@

namespace Casbin\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Casbin\Model\Model;
use PHPUnit\Framework\TestCase;

/**
* EnforcerTest.
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Persist/Adapters/FileAdapterTest.php
Expand Up @@ -2,9 +2,9 @@

namespace Casbin\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Casbin\Persist\Adapters\FileAdapter;
use Casbin\Model\Model;
use Casbin\Persist\Adapters\FileAdapter;
use PHPUnit\Framework\TestCase;

/**
* EnforcerTest.
Expand Down

0 comments on commit 13fe7e7

Please sign in to comment.