Skip to content

Commit 863057d

Browse files
hlhillCHELUN\haoxuPlayer626
authored
Support static usage class Enforcer, optimize test case (#14)
* Support static usage class Enforcer, optimize test case * Support static usage class Enforcer, optimize test case * Update composer.json Co-authored-by: CHELUN\haoxu <[email protected]> Co-authored-by: 史迪仔 <[email protected]>
1 parent 9e818c4 commit 863057d

13 files changed

+558
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ composer.lock
77
.subsplit
88
.php_cs.cache
99
/runtime
10+
*.log

casbin-rbac-model.conf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
],
1111
"require": {
1212
"php": "^7.2",
13-
"ext-swoole": ">=4.5",
14-
"casbin/casbin": "~3.1",
13+
"ext-swoole": ">=4.4",
14+
"casbin/casbin": "~3.0",
1515
"easyswoole/orm": "^1.4",
1616
"easyswoole/easyswoole": "~3.3|~3.4"
1717
},

dev.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@
3434
'host' => '127.0.0.1',
3535
'port' => 3306,
3636
'user' => 'root',
37-
'password' => '',
38-
'database' => 'easyswoole_permission',
37+
'password' => 'root',
38+
'database' => 'test',
3939
'timeout' => 5,
4040
'charset' => 'utf8mb4',
41+
],
42+
'Enforcer' => [
43+
'model_config_type' => 'file',
44+
'model_config_file_path' => __DIR__ . '/casbin-rbac-model.conf',
45+
'log_enable' => true,
4146
]
4247
];

phpunit.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
defined("MYSQL_HOST") ?: define('MYSQL_HOST', '127.0.0.1');
5+
defined("MYSQL_PORT") ?: define('MYSQL_PORT', 3306);
6+
defined("MYSQL_USER") ?: define('MYSQL_USER', 'root');
7+
defined("MYSQL_PASSWORD") ?: define('MYSQL_PASSWORD', '');
8+
defined("MYSQL_DATABASE") ?: define('MYSQL_DATABASE', 'easyswoole_permission');
9+
defined("MYSQL_TIMEOUT") ?: define('MYSQL_TIMEOUT', 5);
10+
defined("MYSQL_CHARSET") ?: define('MYSQL_CHARSET', 'utf8mb4');

src/Adapters/DatabaseAdapter.php

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace EasySwoole\Permission\Adapters;
64

75
use Casbin\Persist\AdapterHelper;
@@ -176,7 +174,6 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
176174
}
177175
$cols[] = $temp;
178176
}
179-
180177
RulesModel::create()->saveAll($cols);
181178
}
182179

src/Casbin.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace EasySwoole\Permission;
64

75
use Casbin\Enforcer;

src/Config.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace EasySwoole\Permission;
66

77
use Casbin\Persist\Adapter;
8+
use EasySwoole\Permission\Adapters\DatabaseAdapter;
9+
use EasySwoole\Spl\SplBean;
810

9-
class Config
11+
class Config extends SplBean
1012
{
1113
const CONFIG_TYPE_FILE = 'file';
1214

@@ -95,7 +97,7 @@ public function setModelConfigText(string $model_config_text): void
9597
*/
9698
public function getAdapter(): ?Adapter
9799
{
98-
return $this->adapter;
100+
return $this->adapter instanceof Adapter ? $this->adapter : new DatabaseAdapter();
99101
}
100102

101103
/**

src/Enforcer.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
4+
namespace EasySwoole\Permission;
5+
6+
use Casbin\Exceptions\InvalidFilePathException;
7+
use Casbin\Log\Log;
8+
use Casbin\Model\Model;
9+
use EasySwoole\Component\Context\ContextManager;
10+
use EasySwoole\Component\Context\Exception\ModifyError;
11+
use InvalidArgumentException;
12+
use Casbin\Exceptions\CasbinException;
13+
use Casbin\Enforcer as BaseEnforcer;
14+
15+
/**
16+
* Class Enforcer
17+
* @package EasySwoole\Permission
18+
* @mixin EnforcerIDE
19+
*/
20+
class Enforcer
21+
{
22+
/**
23+
* @return BaseEnforcer
24+
* @throws CasbinException
25+
*/
26+
protected static function init()
27+
{
28+
$config = \EasySwoole\EasySwoole\Config::getInstance()->getConf('Enforcer');
29+
30+
$config = new Config($config);
31+
32+
if ($config->isLogEnable()) {
33+
$loggerClass = $config->getLoggerClass();
34+
$logger = new $loggerClass();
35+
if (!$logger instanceof Log) {
36+
throw new InvalidArgumentException("Enforcer config is invalid.");
37+
}
38+
Log::setLogger($logger);
39+
}
40+
41+
$model = new Model();
42+
if (Config::CONFIG_TYPE_FILE === $config->getModelConfigType()) {
43+
$model->loadModel($config->getModelConfigFilePath());
44+
} elseif (Config::CONFIG_TYPE_TEXT === $config->getModelConfigType()) {
45+
$model->loadModelFromText($config->getModelConfigText());
46+
}
47+
48+
$adapter = $config->getAdapter();
49+
50+
return new BaseEnforcer($model, $adapter, $config->isLogEnable());
51+
}
52+
53+
/**
54+
* @return BaseEnforcer|mixed
55+
* @throws CasbinException
56+
* @throws ModifyError
57+
*/
58+
protected static function getInstance()
59+
{
60+
$enforcer = ContextManager::getInstance()->get(\Casbin\Enforcer::class);
61+
if (is_null($enforcer)) {
62+
$enforcer = self::init();
63+
ContextManager::getInstance()->set(\Casbin\Enforcer::class, $enforcer);
64+
}
65+
66+
return $enforcer;
67+
}
68+
69+
/**
70+
* @param $name
71+
* @param $arguments
72+
* @return mixed
73+
* @throws CasbinException
74+
* @throws ModifyError
75+
*/
76+
public static function __callStatic($name, $arguments)
77+
{
78+
return self::getInstance()->{$name}(...$arguments);
79+
}
80+
}

0 commit comments

Comments
 (0)