22namespace Tqdev \PhpCrudApi \Database ;
33
44use Tqdev \PhpCrudApi \Column \Reflection \ReflectedTable ;
5+ use Tqdev \PhpCrudApi \Middleware \Communication \VariableStore ;
6+ use Tqdev \PhpCrudApi \Record \Condition \AndCondition ;
57use Tqdev \PhpCrudApi \Record \Condition \ColumnCondition ;
68use Tqdev \PhpCrudApi \Record \Condition \Condition ;
79
@@ -95,6 +97,12 @@ public function definition(): GenericDefinition
9597 return $ this ->definition ;
9698 }
9799
100+ private function addAuthorizationCondition (Condition $ condition2 ): Condition
101+ {
102+ $ condition1 = VariableStore::get ('authorization.condition ' );
103+ return $ condition1 ? AndCondition::fromArray ([$ condition1 , $ condition2 ]) : $ condition2 ;
104+ }
105+
98106 public function createSingle (ReflectedTable $ table , array $ columnValues ) /*: ?String*/
99107 {
100108 $ this ->converter ->convertColumnValues ($ table , $ columnValues );
@@ -122,6 +130,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
122130 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
123131 $ tableName = $ table ->getName ();
124132 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
133+ $ condition = $ this ->addAuthorizationCondition ($ condition );
125134 $ parameters = array ();
126135 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
127136 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -143,6 +152,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
143152 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
144153 $ tableName = $ table ->getName ();
145154 $ condition = new ColumnCondition ($ table ->getPk (), 'in ' , implode (', ' , $ ids ));
155+ $ condition = $ this ->addAuthorizationCondition ($ condition );
146156 $ parameters = array ();
147157 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
148158 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -155,6 +165,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
155165 public function selectCount (ReflectedTable $ table , Condition $ condition ): int
156166 {
157167 $ tableName = $ table ->getName ();
168+ $ condition = $ this ->addAuthorizationCondition ($ condition );
158169 $ parameters = array ();
159170 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
160171 $ sql = 'SELECT COUNT(*) FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -166,6 +177,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
166177 {
167178 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
168179 $ tableName = $ table ->getName ();
180+ $ condition = $ this ->addAuthorizationCondition ($ condition );
169181 $ parameters = array ();
170182 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
171183 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -182,6 +194,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
182194 }
183195 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
184196 $ tableName = $ table ->getName ();
197+ $ condition = $ this ->addAuthorizationCondition ($ condition );
185198 $ parameters = array ();
186199 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
187200 $ orderBy = $ this ->columns ->getOrderBy ($ table , $ columnOrdering );
@@ -202,6 +215,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
202215 $ updateColumns = $ this ->columns ->getUpdate ($ table , $ columnValues );
203216 $ tableName = $ table ->getName ();
204217 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
218+ $ condition = $ this ->addAuthorizationCondition ($ condition );
205219 $ parameters = array_values ($ columnValues );
206220 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
207221 $ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -213,6 +227,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
213227 {
214228 $ tableName = $ table ->getName ();
215229 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
230+ $ condition = $ this ->addAuthorizationCondition ($ condition );
216231 $ parameters = array ();
217232 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
218233 $ sql = 'DELETE FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -229,6 +244,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
229244 $ updateColumns = $ this ->columns ->getIncrement ($ table , $ columnValues );
230245 $ tableName = $ table ->getName ();
231246 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
247+ $ condition = $ this ->addAuthorizationCondition ($ condition );
232248 $ parameters = array_values ($ columnValues );
233249 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
234250 $ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
0 commit comments