Skip to content

Commit f3f9abf

Browse files
committed
Fix: #1
1 parent cebbcf5 commit f3f9abf

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor/
2+
3+
composer.lock
4+
5+
.idea/
6+
*.iml
7+
8+
# coverage report
9+
/build

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ the current supported databases are:
66

77
| type | database |
88
| ------ | ------ |
9-
| mysql | PostgreSQL |
10-
| pgsql | MySQL |
9+
| mysql | MySQL |
10+
| pgsql | PostgreSQL |
1111
| sqlite | SQLite |
1212
| sqlsrv | SqlServer |
1313

@@ -57,4 +57,10 @@ if ($e->enforce($sub, $obj, $act) === true) {
5757

5858
### Getting Help
5959

60-
- [php-casbin](https://github.com/php-casbin/php-casbin)
60+
- [php-casbin](https://github.com/php-casbin/php-casbin)
61+
62+
### License
63+
64+
This project is licensed under the [Apache 2.0 license](LICENSE).## License
65+
66+
This project is licensed under the [Apache 2.0 license](LICENSE).

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
],
1111
"license": "Apache-2.0",
1212
"require": {
13-
"casbin/casbin": "^0.1.0",
14-
"techone/database": "^0.1.0"
13+
"casbin/casbin": ">=0.1.4",
14+
"techone/database": ">=0.2.0"
1515
},
1616
"autoload": {
1717
"psr-4": {

src/Adapter.php

+58-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?php
2+
23
namespace CasbinAdapter\Database;
34

45
use Casbin\Exceptions\CasbinException;
56
use Casbin\Persist\Adapter as AdapterContract;
67
use TechOne\Database\Manager;
78
use Casbin\Persist\AdapterHelper;
9+
810
/**
9-
* DatabaseAdapter
11+
* DatabaseAdapter.
12+
*
1013
1114
*/
1215
class Adapter implements AdapterContract
@@ -21,7 +24,7 @@ class Adapter implements AdapterContract
2124

2225
public function __construct(array $config)
2326
{
24-
$this->config = $config;
27+
$this->config = $config;
2528
$this->connection = (new Manager($config))->getConnection();
2629
$this->initTable();
2730
}
@@ -33,7 +36,48 @@ public static function newAdapter(array $config)
3336

3437
public function initTable()
3538
{
36-
$sql = <<<EOT
39+
if ('pgsql' == $this->config['type']) {
40+
$sql = <<<EOT
41+
CREATE TABLE IF NOT EXISTS $this->casbinRuleTableName (
42+
id bigserial NOT NULL,
43+
ptype varchar(255) NOT NULL,
44+
v0 varchar(255) DEFAULT NULL,
45+
v1 varchar(255) DEFAULT NULL,
46+
v2 varchar(255) DEFAULT NULL,
47+
v3 varchar(255) DEFAULT NULL,
48+
v4 varchar(255) DEFAULT NULL,
49+
v5 varchar(255) DEFAULT NULL,
50+
PRIMARY KEY (id)
51+
);
52+
EOT;
53+
} elseif ('sqlite' == $this->config['type']) {
54+
$sql = <<<EOT
55+
CREATE TABLE IF NOT EXISTS `$this->casbinRuleTableName` (
56+
`id` INTEGER PRIMARY KEY AUTOINCREMENT ,
57+
`ptype` varchar(255) NOT NULL,
58+
`v0` varchar(255) DEFAULT NULL,
59+
`v1` varchar(255) DEFAULT NULL,
60+
`v2` varchar(255) DEFAULT NULL,
61+
`v3` varchar(255) DEFAULT NULL,
62+
`v4` varchar(255) DEFAULT NULL,
63+
`v5` varchar(255) DEFAULT NULL
64+
);
65+
EOT;
66+
} elseif ('sqlsrv' == $this->config['type']) {
67+
$sql = <<<EOT
68+
CREATE TABLE IF NOT EXISTS `$this->casbinRuleTableName` (
69+
id int IDENTITY(1,1),
70+
ptype varchar(255) NOT NULL,
71+
v0 varchar(255) DEFAULT NULL,
72+
v1 varchar(255) DEFAULT NULL,
73+
v2 varchar(255) DEFAULT NULL,
74+
v3 varchar(255) DEFAULT NULL,
75+
v4 varchar(255) DEFAULT NULL,
76+
v5 varchar(255) DEFAULT NULL
77+
);
78+
EOT;
79+
} else {
80+
$sql = <<<EOT
3781
CREATE TABLE IF NOT EXISTS `$this->casbinRuleTableName` (
3882
`id` int(11) NOT NULL AUTO_INCREMENT,
3983
`ptype` varchar(255) NOT NULL,
@@ -46,29 +90,29 @@ public function initTable()
4690
PRIMARY KEY (`id`)
4791
);
4892
EOT;
93+
}
4994
$this->connection->execute($sql, []);
5095
}
5196

5297
public function savePolicyLine($ptype, array $rule)
5398
{
54-
55-
$col['`ptype`'] = $ptype;
99+
$col['ptype'] = $ptype;
56100
foreach ($rule as $key => $value) {
57-
$col['`v' . strval($key) . '`'] = $value;
101+
$col['v'.strval($key).''] = $value;
58102
}
59103

60104
$colStr = implode(', ', array_keys($col));
61105

62106
$name = rtrim(str_repeat('?, ', count($col)), ', ');
63107

64-
$sql = 'INSERT INTO `' . $this->casbinRuleTableName . '`(' . $colStr . ') VALUES (' . $name . ') ';
108+
$sql = 'INSERT INTO '.$this->casbinRuleTableName.'('.$colStr.') VALUES ('.$name.') ';
65109

66110
$this->connection->execute($sql, array_values($col));
67111
}
68112

69113
public function loadPolicy($model)
70114
{
71-
$rows = $this->connection->query('SELECT * FROM `' . $this->casbinRuleTableName . '`');
115+
$rows = $this->connection->query('SELECT * FROM '.$this->casbinRuleTableName.'');
72116

73117
foreach ($rows as $row) {
74118
$line = implode(', ', array_slice(array_values($row), 1));
@@ -89,6 +133,7 @@ public function savePolicy($model)
89133
$this->savePolicyLine($ptype, $rule);
90134
}
91135
}
136+
92137
return true;
93138
}
94139

@@ -100,22 +145,22 @@ public function addPolicy($sec, $ptype, $rule)
100145
public function removePolicy($sec, $ptype, $rule)
101146
{
102147
$where['ptype'] = $ptype;
103-
$condition[] = 'ptype = :ptype';
148+
$condition[] = 'ptype = :ptype';
104149
foreach ($rule as $key => $value) {
105-
$where['v' . strval($key)] = $value;
106-
$condition[] = 'v' . strval($key) . ' = :' . 'v' . strval($key);
150+
$where['v'.strval($key)] = $value;
151+
$condition[] = 'v'.strval($key).' = :'.'v'.strval($key);
107152
}
108153
if (empty($condition)) {
109154
return;
110155
}
111156

112-
$sql = 'DELETE FROM `' . $this->casbinRuleTableName . '` WHERE ' . implode(' AND ', $condition);
157+
$sql = 'DELETE FROM '.$this->casbinRuleTableName.' WHERE '.implode(' AND ', $condition);
113158

114159
return $this->connection->execute($sql, $where);
115160
}
116161

117162
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
118163
{
119-
throw new CasbinException("not implemented");
164+
throw new CasbinException('not implemented');
120165
}
121166
}

0 commit comments

Comments
 (0)