2
2
namespace Tqdev \PhpCrudApi \Database ;
3
3
4
4
use Tqdev \PhpCrudApi \Column \Reflection \ReflectedTable ;
5
+ use Tqdev \PhpCrudApi \Middleware \Communication \VariableStore ;
6
+ use Tqdev \PhpCrudApi \Record \Condition \AndCondition ;
5
7
use Tqdev \PhpCrudApi \Record \Condition \ColumnCondition ;
6
8
use Tqdev \PhpCrudApi \Record \Condition \Condition ;
7
9
@@ -95,6 +97,12 @@ public function definition(): GenericDefinition
95
97
return $ this ->definition ;
96
98
}
97
99
100
+ private function addAuthorizationCondition (Condition $ condition2 ): Condition
101
+ {
102
+ $ condition1 = VariableStore::get ('authorization.condition ' );
103
+ return $ condition1 ? AndCondition::fromArray ([$ condition1 , $ condition2 ]) : $ condition2 ;
104
+ }
105
+
98
106
public function createSingle (ReflectedTable $ table , array $ columnValues ) /*: ?String*/
99
107
{
100
108
$ this ->converter ->convertColumnValues ($ table , $ columnValues );
@@ -122,6 +130,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
122
130
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
123
131
$ tableName = $ table ->getName ();
124
132
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
133
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
125
134
$ parameters = array ();
126
135
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
127
136
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -143,6 +152,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
143
152
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
144
153
$ tableName = $ table ->getName ();
145
154
$ condition = new ColumnCondition ($ table ->getPk (), 'in ' , implode (', ' , $ ids ));
155
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
146
156
$ parameters = array ();
147
157
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
148
158
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -155,6 +165,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
155
165
public function selectCount (ReflectedTable $ table , Condition $ condition ): int
156
166
{
157
167
$ tableName = $ table ->getName ();
168
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
158
169
$ parameters = array ();
159
170
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
160
171
$ sql = 'SELECT COUNT(*) FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -166,6 +177,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
166
177
{
167
178
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
168
179
$ tableName = $ table ->getName ();
180
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
169
181
$ parameters = array ();
170
182
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
171
183
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -182,6 +194,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
182
194
}
183
195
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
184
196
$ tableName = $ table ->getName ();
197
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
185
198
$ parameters = array ();
186
199
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
187
200
$ orderBy = $ this ->columns ->getOrderBy ($ table , $ columnOrdering );
@@ -202,6 +215,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
202
215
$ updateColumns = $ this ->columns ->getUpdate ($ table , $ columnValues );
203
216
$ tableName = $ table ->getName ();
204
217
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
218
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
205
219
$ parameters = array_values ($ columnValues );
206
220
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
207
221
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -213,6 +227,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
213
227
{
214
228
$ tableName = $ table ->getName ();
215
229
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
230
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
216
231
$ parameters = array ();
217
232
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
218
233
$ sql = 'DELETE FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -229,6 +244,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
229
244
$ updateColumns = $ this ->columns ->getIncrement ($ table , $ columnValues );
230
245
$ tableName = $ table ->getName ();
231
246
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
247
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
232
248
$ parameters = array_values ($ columnValues );
233
249
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
234
250
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
0 commit comments