Skip to content

Commit

Permalink
Merge d834905 into 9e818c4
Browse files Browse the repository at this point in the history
  • Loading branch information
hlhill committed Apr 29, 2021
2 parents 9e818c4 + d834905 commit 87697be
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ composer.lock
.subsplit
.php_cs.cache
/runtime
*.log
14 changes: 14 additions & 0 deletions casbin-rbac-model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
],
"require": {
"php": "^7.2",
"ext-swoole": ">=4.5",
"casbin/casbin": "~3.1",
"ext-swoole": ">=4.4",
"casbin/casbin": "~3.0",
"easyswoole/orm": "^1.4",
"easyswoole/easyswoole": "~3.3|~3.4"
},
Expand Down
9 changes: 7 additions & 2 deletions dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => '',
'database' => 'easyswoole_permission',
'password' => 'root',
'database' => 'test',
'timeout' => 5,
'charset' => 'utf8mb4',
],
'Enforcer' => [
'model_config_type' => 'file',
'model_config_file_path' => __DIR__ . '/casbin-rbac-model.conf',
'log_enable' => true,
]
];
10 changes: 10 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


defined("MYSQL_HOST") ?: define('MYSQL_HOST', '127.0.0.1');
defined("MYSQL_PORT") ?: define('MYSQL_PORT', 3306);
defined("MYSQL_USER") ?: define('MYSQL_USER', 'root');
defined("MYSQL_PASSWORD") ?: define('MYSQL_PASSWORD', '');
defined("MYSQL_DATABASE") ?: define('MYSQL_DATABASE', 'easyswoole_permission');
defined("MYSQL_TIMEOUT") ?: define('MYSQL_TIMEOUT', 5);
defined("MYSQL_CHARSET") ?: define('MYSQL_CHARSET', 'utf8mb4');
3 changes: 0 additions & 3 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace EasySwoole\Permission\Adapters;

use Casbin\Persist\AdapterHelper;
Expand Down Expand Up @@ -176,7 +174,6 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
}
$cols[] = $temp;
}

RulesModel::create()->saveAll($cols);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Casbin.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace EasySwoole\Permission;

use Casbin\Enforcer;
Expand Down
6 changes: 4 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace EasySwoole\Permission;

use Casbin\Persist\Adapter;
use EasySwoole\Permission\Adapters\DatabaseAdapter;
use EasySwoole\Spl\SplBean;

class Config
class Config extends SplBean
{
const CONFIG_TYPE_FILE = 'file';

Expand Down Expand Up @@ -95,7 +97,7 @@ public function setModelConfigText(string $model_config_text): void
*/
public function getAdapter(): ?Adapter
{
return $this->adapter;
return $this->adapter instanceof Adapter ? $this->adapter : new DatabaseAdapter();
}

/**
Expand Down
80 changes: 80 additions & 0 deletions src/Enforcer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php


namespace EasySwoole\Permission;

use Casbin\Exceptions\InvalidFilePathException;
use Casbin\Log\Log;
use Casbin\Model\Model;
use EasySwoole\Component\Context\ContextManager;
use EasySwoole\Component\Context\Exception\ModifyError;
use InvalidArgumentException;
use Casbin\Exceptions\CasbinException;
use Casbin\Enforcer as BaseEnforcer;

/**
* Class Enforcer
* @package EasySwoole\Permission
* @mixin EnforcerIDE
*/
class Enforcer
{
/**
* @return BaseEnforcer
* @throws CasbinException
*/
protected static function init()
{
$config = \EasySwoole\EasySwoole\Config::getInstance()->getConf('Enforcer');

$config = new Config($config);

if ($config->isLogEnable()) {
$loggerClass = $config->getLoggerClass();
$logger = new $loggerClass();
if (!$logger instanceof Log) {
throw new InvalidArgumentException("Enforcer config is invalid.");
}
Log::setLogger($logger);
}

$model = new Model();
if (Config::CONFIG_TYPE_FILE === $config->getModelConfigType()) {
$model->loadModel($config->getModelConfigFilePath());
} elseif (Config::CONFIG_TYPE_TEXT === $config->getModelConfigType()) {
$model->loadModelFromText($config->getModelConfigText());
}

$adapter = $config->getAdapter();

return new BaseEnforcer($model, $adapter, $config->isLogEnable());
}

/**
* @return BaseEnforcer|mixed
* @throws CasbinException
* @throws ModifyError
*/
protected static function getInstance()
{
$enforcer = ContextManager::getInstance()->get(\Casbin\Enforcer::class);
if (is_null($enforcer)) {
$enforcer = self::init();
ContextManager::getInstance()->set(\Casbin\Enforcer::class, $enforcer);
}

return $enforcer;
}

/**
* @param $name
* @param $arguments
* @return mixed
* @throws CasbinException
* @throws ModifyError
*/
public static function __callStatic($name, $arguments)
{
return self::getInstance()->{$name}(...$arguments);
}
}
Loading

0 comments on commit 87697be

Please sign in to comment.