-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
558 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ composer.lock | |
.subsplit | ||
.php_cs.cache | ||
/runtime | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.