99use Casbin \Persist \FilteredAdapter ;
1010use Casbin \Persist \Adapters \Filter ;
1111use Casbin \Exceptions \InvalidFilterTypeException ;
12+ use Closure ;
1213
1314/**
1415 * DatabaseAdapter.
@@ -27,6 +28,8 @@ class Adapter implements AdapterContract, FilteredAdapter
2728
2829 public $ casbinRuleTableName = 'casbin_rule ' ;
2930
31+ public $ rows = [];
32+
3033 public function __construct (array $ config )
3134 {
3235 $ this ->config = $ config ;
@@ -45,6 +48,16 @@ public function isFiltered(): bool
4548 return $ this ->filtered ;
4649 }
4750
51+ /**
52+ * Sets filtered parameter.
53+ *
54+ * @param bool $filtered
55+ */
56+ public function setFiltered (bool $ filtered ): void
57+ {
58+ $ this ->filtered = $ filtered ;
59+ }
60+
4861 public static function newAdapter (array $ config )
4962 {
5063 return new static ($ config );
@@ -90,55 +103,6 @@ public function loadPolicy(Model $model): void
90103 }
91104 }
92105
93- /**
94- * Loads only policy rules that match the filter from storage.
95- *
96- * @param Model $model
97- * @param mixed $filter
98- *
99- * @throws CasbinException
100- */
101- public function loadFilteredPolicy (Model $ model , $ filter ): void
102- {
103- // if $filter is empty, load all policies
104- if (is_null ($ filter )) {
105- $ this ->loadPolicy ($ model );
106- return ;
107- }
108- // validate $filter is a instance of Filter
109- if (!$ filter instanceof Filter) {
110- throw new InvalidFilterTypeException ('invalid filter type ' );
111- }
112- $ type = '' ;
113- $ filter = (array ) $ filter ;
114- // choose which ptype to use
115- foreach ($ filter as $ i => $ v ) {
116- if (!empty ($ v )) {
117- array_unshift ($ filter [$ i ], $ i );
118- $ type = $ i ;
119- break ;
120- }
121- }
122- $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
123- $ items = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
124- $ temp = [];
125- $ values = [];
126- foreach ($ items as $ i => $ item ) {
127- if (isset ($ filter [$ type ][$ i ]) && !empty ($ filter [$ type ][$ i ])) {
128- array_push ($ temp , $ item . '=: ' . $ item );
129- $ values [$ item ] = $ filter [$ type ][$ i ];
130- }
131- }
132- $ sql .= implode (' and ' , $ temp );
133- $ rows = $ this ->connection ->query ($ sql , $ values );
134- foreach ($ rows as $ row ) {
135- $ line = implode (', ' , $ row );
136- $ this ->loadPolicyLine ($ line , $ model );
137- }
138-
139- $ this ->filtered = true ;
140- }
141-
142106 /**
143107 * saves all policy rules to the storage.
144108 *
@@ -219,4 +183,61 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
219183
220184 $ this ->connection ->execute ($ sql , $ where );
221185 }
186+
187+ /**
188+ * Loads only policy rules that match the filter from storage.
189+ *
190+ * @param Model $model
191+ * @param mixed $filter
192+ *
193+ * @throws CasbinException
194+ */
195+ public function loadFilteredPolicy (Model $ model , $ filter ): void
196+ {
197+ // if $filter is empty, load all policies
198+ if (is_null ($ filter )) {
199+ $ this ->loadPolicy ($ model );
200+ return ;
201+ }
202+ // the basic sql
203+ $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
204+
205+ if ($ filter instanceof Filter) {
206+ $ type = '' ;
207+ $ filter = (array ) $ filter ;
208+ // choose which ptype to use
209+ foreach ($ filter as $ i => $ v ) {
210+ if (!empty ($ v )) {
211+ array_unshift ($ filter [$ i ], $ i );
212+ $ type = $ i ;
213+ break ;
214+ }
215+ }
216+ $ items = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
217+ $ temp = [];
218+ $ values = [];
219+ foreach ($ items as $ i => $ item ) {
220+ if (isset ($ filter [$ type ][$ i ]) && !empty ($ filter [$ type ][$ i ])) {
221+ array_push ($ temp , $ item . '=: ' . $ item );
222+ $ values [$ item ] = $ filter [$ type ][$ i ];
223+ }
224+ }
225+ $ sql .= implode (' and ' , $ temp );
226+ $ rows = $ this ->connection ->query ($ sql , $ values );
227+ } elseif (is_string ($ filter )) {
228+ $ sql .= $ filter ;
229+ $ rows = $ this ->connection ->query ($ sql );
230+ } else if ($ filter instanceof Closure) {
231+ $ filter ($ this ->connection , $ sql , $ this ->rows );
232+ } else {
233+ throw new InvalidFilterTypeException ('invalid filter type ' );
234+ }
235+
236+ $ rows = $ rows ?? $ this ->rows ;
237+ foreach ($ rows as $ row ) {
238+ $ line = implode (', ' , $ row );
239+ $ this ->loadPolicyLine ($ line , $ model );
240+ }
241+ $ this ->filtered = true ;
242+ }
222243}
0 commit comments