Skip to content

Commit 2740129

Browse files
author
albert
committed
first commit
1 parent 5fb934a commit 2740129

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "rabbit/casbin",
3+
"description": "rabbit casbin base on casbin/casbin",
4+
"type": "library",
5+
"require": {
6+
"rabbit/framework": "dev-master",
7+
"casbin/casbin": "^0.2.1"
8+
},
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "albert",
13+
"email": "63851587@qq.com"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"rabbit\\casbin\\": "src/"
19+
}
20+
}
21+
}

src/Adapter.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2019/3/16
6+
* Time: 18:09
7+
*/
8+
9+
namespace rabbit\casbin;
10+
11+
12+
use Casbin\Exceptions\CasbinException;
13+
use Casbin\Persist\Adapter as AdapterContract;
14+
use Casbin\Persist\AdapterHelper;
15+
use rabbit\casbin\Model\CasbinRule;
16+
17+
/**
18+
* Class Adapter
19+
* @package rabbit\casbin
20+
*/
21+
class Adapter implements AdapterContract
22+
{
23+
use AdapterHelper;
24+
/** @var CasbinRule */
25+
protected $casbinRule;
26+
27+
public function __construct(CasbinRule $casbinRule)
28+
{
29+
$this->casbinRule = $casbinRule;
30+
}
31+
32+
/**
33+
* @param array $ptype
34+
* @param array $rule
35+
*/
36+
public function savePolicyLine(array $ptype, array $rule)
37+
{
38+
$col['ptype'] = $ptype;
39+
foreach ($rule as $key => $value) {
40+
$col['v' . strval($key) . ''] = $value;
41+
}
42+
$ar = clone $this->casbinRule;
43+
$ar->setAttributes($col);
44+
$ar->save();
45+
}
46+
47+
/**
48+
* @param \Casbin\Model\Model $model
49+
* @return mixed|void
50+
*/
51+
public function loadPolicy($model)
52+
{
53+
$ar = clone $this->casbinRule;
54+
$rows = $ar->find()->all();
55+
foreach ($rows as $row) {
56+
$line = implode(', ', array_slice(array_values($row->toArray()), 1));
57+
$this->loadPolicyLine(trim($line), $model);
58+
}
59+
}
60+
61+
/**
62+
* @param \Casbin\Model\Model $model
63+
* @return bool
64+
*/
65+
public function savePolicy($model)
66+
{
67+
foreach ($model->model['p'] as $ptype => $ast) {
68+
foreach ($ast->policy as $rule) {
69+
$this->savePolicyLine($ptype, $rule);
70+
}
71+
}
72+
foreach ($model->model['g'] as $ptype => $ast) {
73+
foreach ($ast->policy as $rule) {
74+
$this->savePolicyLine($ptype, $rule);
75+
}
76+
}
77+
return true;
78+
}
79+
80+
/**
81+
* @param string $sec
82+
* @param string $ptype
83+
* @param array $rule
84+
* @return mixed|void
85+
*/
86+
public function addPolicy($sec, $ptype, $rule)
87+
{
88+
return $this->savePolicyLine($ptype, $rule);
89+
}
90+
91+
/**
92+
* @param string $sec
93+
* @param string $ptype
94+
* @param array $rule
95+
* @return mixed
96+
*/
97+
public function removePolicy($sec, $ptype, $rule)
98+
{
99+
$result = $this->casbinRule->where('ptype', $ptype);
100+
foreach ($rule as $key => $value) {
101+
$result->where('v' . strval($key), $value);
102+
}
103+
return $result->delete();
104+
}
105+
106+
/**
107+
* @param string $sec
108+
* @param string $ptype
109+
* @param int $fieldIndex
110+
* @param mixed ...$fieldValues
111+
* @return mixed|void
112+
* @throws CasbinException
113+
*/
114+
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
115+
{
116+
throw new CasbinException('not implemented');
117+
}
118+
}

src/Casbin.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2019/3/16
6+
* Time: 17:54
7+
*/
8+
9+
namespace rabbit\casbin;
10+
11+
use Casbin\Enforcer;
12+
use Casbin\Log\Log;
13+
use Casbin\Model\Model;
14+
use rabbit\casbin\Model\CasbinRule;
15+
16+
/**
17+
* Class Casbin
18+
* @package rabbit\casbin
19+
*/
20+
class Casbin
21+
{
22+
/** @var bool */
23+
private $isInit = false;
24+
/** @var Enforcer */
25+
private $enforcer;
26+
/** @var Adapter */
27+
private $adapter;
28+
/** @var Model */
29+
private $model;
30+
/** @var array */
31+
private $config = [];
32+
33+
/**
34+
* @param array $config
35+
*/
36+
public function __construct(\Casbin\Log\Logger $logger, array $config = [])
37+
{
38+
$this->config = $config;
39+
Log::setLogger($logger);
40+
}
41+
42+
/**
43+
* @return array
44+
*/
45+
public function getConfig(): array
46+
{
47+
return $this->config;
48+
}
49+
50+
/**
51+
*
52+
*/
53+
private function init()
54+
{
55+
if (!$this->isInit) {
56+
$db = CasbinRule::getDb();
57+
$tableName = CasbinRule::tableName();
58+
$table = $db->getTableSchema($tableName);
59+
if (!$table) {
60+
$res = $db->createCommand()->createTable($tableName, [
61+
'id' => 'pk',
62+
'ptype' => 'string',
63+
'v0' => 'string',
64+
'v1' => 'string',
65+
'v2' => 'string',
66+
'v3' => 'string',
67+
'v4' => 'string',
68+
'v5' => 'string',
69+
])->execute();
70+
}
71+
}
72+
}
73+
74+
/**
75+
* @param bool $newInstance
76+
* @return Enforcer
77+
* @throws \Casbin\Exceptions\CasbinException
78+
*/
79+
public function enforcer($newInstance = false): Enforcer
80+
{
81+
if ($newInstance || is_null($this->enforcer)) {
82+
$this->init();
83+
$this->enforcer = new Enforcer($this->model, $this->adapter);
84+
}
85+
return $this->enforcer;
86+
}
87+
88+
/**
89+
* @param $name
90+
* @param $params
91+
* @return mixed
92+
* @throws \Casbin\Exceptions\CasbinException
93+
*/
94+
public function __call($name, $params)
95+
{
96+
return call_user_func_array([$this->enforcer(), $name], $params);
97+
}
98+
}

src/Logger.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2019/3/16
6+
* Time: 18:47
7+
*/
8+
9+
namespace rabbit\casbin;
10+
11+
use Casbin\Log\Logger as LoggerContract;
12+
use Psr\Log\LoggerInterface;
13+
14+
/**
15+
* Class Logger
16+
* @package rabbit\casbin
17+
*/
18+
class Logger implements LoggerContract
19+
{
20+
public $enable = false;
21+
/**
22+
* @var LoggerInterface
23+
*/
24+
protected $logger;
25+
26+
public function __construct(LoggerInterface $logger = null)
27+
{
28+
$this->logger = $logger ?? getDI('logger');
29+
}
30+
31+
/**
32+
* controls whether print the message.
33+
*
34+
* @param bool $enable
35+
*/
36+
public function enableLog($enable)
37+
{
38+
$this->enable = $enable;
39+
}
40+
41+
/**
42+
* returns if logger is enabled.
43+
*
44+
* @return bool
45+
*/
46+
public function isEnabled()
47+
{
48+
return $this->enable;
49+
}
50+
51+
/**
52+
* formats using the default formats for its operands and logs the message.
53+
*
54+
* @param mixed ...$v
55+
*
56+
* @return mixed
57+
*/
58+
public function write(...$v)
59+
{
60+
if (!$this->enable) {
61+
return;
62+
}
63+
$content = '';
64+
foreach ($v as $value) {
65+
if (\is_array($value) || \is_object($value)) {
66+
$value = json_encode($value);
67+
}
68+
$content .= $value;
69+
}
70+
$this->logger->info($content);
71+
}
72+
73+
/**
74+
* formats according to a format specifier and logs the message.
75+
*
76+
* @param $format
77+
* @param mixed ...$v
78+
*
79+
* @return mixed
80+
*/
81+
public function writef($format, ...$v)
82+
{
83+
if (!$this->enable) {
84+
return;
85+
}
86+
$this->logger->info(sprintf($format, ...$v));
87+
}
88+
89+
}

src/Model/CasbinRule.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2019/3/16
6+
* Time: 17:59
7+
*/
8+
9+
namespace rabbit\casbin\Model;
10+
11+
12+
use rabbit\activerecord\ActiveRecord;
13+
use rabbit\db\ConnectionInterface;
14+
use rabbit\db\Manager;
15+
16+
/**
17+
* Class CasbinRule
18+
* @package rabbit\casbin\Model
19+
*/
20+
class CasbinRule extends ActiveRecord
21+
{
22+
/**
23+
* @return string Active Record
24+
*/
25+
public static function tableName()
26+
{
27+
return getDI('casbin')->getConfig()['database']['casbin_rules_table'];
28+
}
29+
30+
/**
31+
* @return ConnectionInterface
32+
*/
33+
public static function getDb(): ConnectionInterface
34+
{
35+
$dbKey = getDI('casbin')->getConfig()['database']['connection'] ?: 'db';
36+
/** @var Manager $db */
37+
$db = getDI('db');
38+
return $db->getConnection($dbKey);
39+
}
40+
41+
public function rules()
42+
{
43+
return [
44+
[['ptype', 'v0'], 'required'],
45+
[['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'], 'safe'],
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)