1
1
<?php
2
+
2
3
namespace CasbinAdapter \Database ;
3
4
4
5
use Casbin \Exceptions \CasbinException ;
5
6
use Casbin \Persist \Adapter as AdapterContract ;
6
7
use TechOne \Database \Manager ;
7
8
use Casbin \Persist \AdapterHelper ;
9
+
8
10
/**
9
- * DatabaseAdapter
11
+ * DatabaseAdapter.
12
+ *
10
13
11
14
*/
12
15
class Adapter implements AdapterContract
@@ -21,7 +24,7 @@ class Adapter implements AdapterContract
21
24
22
25
public function __construct (array $ config )
23
26
{
24
- $ this ->config = $ config ;
27
+ $ this ->config = $ config ;
25
28
$ this ->connection = (new Manager ($ config ))->getConnection ();
26
29
$ this ->initTable ();
27
30
}
@@ -33,7 +36,48 @@ public static function newAdapter(array $config)
33
36
34
37
public function initTable ()
35
38
{
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
37
81
CREATE TABLE IF NOT EXISTS ` $ this ->casbinRuleTableName ` (
38
82
`id` int(11) NOT NULL AUTO_INCREMENT,
39
83
`ptype` varchar(255) NOT NULL,
@@ -46,29 +90,29 @@ public function initTable()
46
90
PRIMARY KEY (`id`)
47
91
);
48
92
EOT ;
93
+ }
49
94
$ this ->connection ->execute ($ sql , []);
50
95
}
51
96
52
97
public function savePolicyLine ($ ptype , array $ rule )
53
98
{
54
-
55
- $ col ['`ptype` ' ] = $ ptype ;
99
+ $ col ['ptype ' ] = $ ptype ;
56
100
foreach ($ rule as $ key => $ value ) {
57
- $ col ['`v ' . strval ($ key ) . ' ` ' ] = $ value ;
101
+ $ col ['v ' . strval ($ key ). ' ' ] = $ value ;
58
102
}
59
103
60
104
$ colStr = implode (', ' , array_keys ($ col ));
61
105
62
106
$ name = rtrim (str_repeat ('?, ' , count ($ col )), ', ' );
63
107
64
- $ sql = 'INSERT INTO ` ' . $ this ->casbinRuleTableName . ' `( ' . $ colStr . ') VALUES ( ' . $ name . ') ' ;
108
+ $ sql = 'INSERT INTO ' . $ this ->casbinRuleTableName . ' ( ' . $ colStr. ') VALUES ( ' . $ name. ') ' ;
65
109
66
110
$ this ->connection ->execute ($ sql , array_values ($ col ));
67
111
}
68
112
69
113
public function loadPolicy ($ model )
70
114
{
71
- $ rows = $ this ->connection ->query ('SELECT * FROM ` ' . $ this ->casbinRuleTableName . ' ` ' );
115
+ $ rows = $ this ->connection ->query ('SELECT * FROM ' . $ this ->casbinRuleTableName . ' ' );
72
116
73
117
foreach ($ rows as $ row ) {
74
118
$ line = implode (', ' , array_slice (array_values ($ row ), 1 ));
@@ -89,6 +133,7 @@ public function savePolicy($model)
89
133
$ this ->savePolicyLine ($ ptype , $ rule );
90
134
}
91
135
}
136
+
92
137
return true ;
93
138
}
94
139
@@ -100,22 +145,22 @@ public function addPolicy($sec, $ptype, $rule)
100
145
public function removePolicy ($ sec , $ ptype , $ rule )
101
146
{
102
147
$ where ['ptype ' ] = $ ptype ;
103
- $ condition [] = 'ptype = :ptype ' ;
148
+ $ condition [] = 'ptype = :ptype ' ;
104
149
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 );
107
152
}
108
153
if (empty ($ condition )) {
109
154
return ;
110
155
}
111
156
112
- $ sql = 'DELETE FROM ` ' . $ this ->casbinRuleTableName . ' ` WHERE ' . implode (' AND ' , $ condition );
157
+ $ sql = 'DELETE FROM ' . $ this ->casbinRuleTableName . ' WHERE '. implode (' AND ' , $ condition );
113
158
114
159
return $ this ->connection ->execute ($ sql , $ where );
115
160
}
116
161
117
162
public function removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues )
118
163
{
119
- throw new CasbinException (" not implemented " );
164
+ throw new CasbinException (' not implemented ' );
120
165
}
121
166
}
0 commit comments