88namespace OCA \User_LDAP \Mapping ;
99
1010use Doctrine \DBAL \Exception ;
11- use OCP \DB \IPreparedStatement ;
1211use OCP \DB \QueryBuilder \IQueryBuilder ;
1312use OCP \IAppConfig ;
1413use OCP \ICache ;
@@ -154,23 +153,6 @@ protected function getXbyY($fetchCol, $compareCol, $search) {
154153 }
155154 }
156155
157- /**
158- * Performs a DELETE or UPDATE query to the database.
159- *
160- * @param IPreparedStatement $statement
161- * @param array $parameters
162- * @return bool true if at least one row was modified, false otherwise
163- */
164- protected function modify (IPreparedStatement $ statement , $ parameters ) {
165- try {
166- $ result = $ statement ->execute ($ parameters );
167- $ updated = $ result ->rowCount () > 0 ;
168- $ result ->closeCursor ();
169- return $ updated ;
170- } catch (Exception $ e ) {
171- return false ;
172- }
173- }
174156
175157 /**
176158 * Gets the LDAP DN based on the provided name.
@@ -199,13 +181,16 @@ public function getDNByName(string $name): string|false {
199181 */
200182 public function setDNbyUUID ($ fdn , $ uuid ) {
201183 $ oldDn = $ this ->getDnByUUID ($ uuid );
202- $ statement = $ this ->dbc ->prepare ('
203- UPDATE ` ' . $ this ->getTableName () . '`
204- SET `ldap_dn_hash` = ?, `ldap_dn` = ?
205- WHERE `directory_uuid` = ?
206- ' );
207-
208- $ r = $ this ->modify ($ statement , [$ this ->getDNHash ($ fdn ), $ fdn , $ uuid ]);
184+ $ qb = $ this ->dbc ->getQueryBuilder ();
185+ try {
186+ $ r = $ qb ->update ($ this ->getTableName (false ))
187+ ->set ('ldap_dn_hash ' , $ qb ->createNamedParameter ($ this ->getDNHash ($ fdn )))
188+ ->set ('ldap_dn ' , $ qb ->createNamedParameter ($ fdn ))
189+ ->where ($ qb ->expr ()->eq ('directory_uuid ' , $ qb ->createNamedParameter ($ uuid )))
190+ ->executeStatement () > 0 ;
191+ } catch (Exception $ e ) {
192+ $ r = false ;
193+ }
209194 if ($ r ) {
210195 if (is_string ($ oldDn ) && isset ($ this ->cache [$ oldDn ])) {
211196 $ userId = $ this ->cache [$ oldDn ];
@@ -231,15 +216,17 @@ public function setDNbyUUID($fdn, $uuid) {
231216 * @return bool
232217 */
233218 public function setUUIDbyDN ($ uuid , $ fdn ): bool {
234- $ statement = $ this ->dbc ->prepare ('
235- UPDATE ` ' . $ this ->getTableName () . '`
236- SET `directory_uuid` = ?
237- WHERE `ldap_dn_hash` = ?
238- ' );
239-
240219 unset($ this ->cache [$ fdn ]);
241220
242- return $ this ->modify ($ statement , [$ uuid , $ this ->getDNHash ($ fdn )]);
221+ $ qb = $ this ->dbc ->getQueryBuilder ();
222+ try {
223+ return $ qb ->update ($ this ->getTableName (false ))
224+ ->set ('directory_uuid ' , $ qb ->createNamedParameter ($ uuid ))
225+ ->where ($ qb ->expr ()->eq ('ldap_dn_hash ' , $ qb ->createNamedParameter ($ this ->getDNHash ($ fdn ))))
226+ ->executeStatement () > 0 ;
227+ } catch (Exception $ e ) {
228+ return false ;
229+ }
243230 }
244231
245232 /**
@@ -333,21 +320,22 @@ public function getListOfIdsByDn(array $fdns): array {
333320 * @return string[]
334321 */
335322 public function getNamesBySearch (string $ search , string $ prefixMatch = '' , string $ postfixMatch = '' ): array {
336- $ statement = $ this ->dbc ->prepare ('
337- SELECT `owncloud_name`
338- FROM ` ' . $ this ->getTableName () . '`
339- WHERE `owncloud_name` LIKE ?
340- ' );
341-
323+ $ qb = $ this ->dbc ->getQueryBuilder ();
342324 try {
343- $ res = $ statement ->execute ([$ prefixMatch . $ this ->dbc ->escapeLikeParameter ($ search ) . $ postfixMatch ]);
325+ $ res = $ qb ->select ('owncloud_name ' )
326+ ->from ($ this ->getTableName (false ))
327+ ->where ($ qb ->expr ()->like ('owncloud_name ' , $ qb ->createNamedParameter (
328+ $ prefixMatch . $ this ->dbc ->escapeLikeParameter ($ search ) . $ postfixMatch
329+ )))
330+ ->executeQuery ();
344331 } catch (Exception $ e ) {
345332 return [];
346333 }
347334 $ names = [];
348335 while ($ row = $ res ->fetchAssociative ()) {
349336 $ names [] = $ row ['owncloud_name ' ];
350337 }
338+ $ res ->closeCursor ();
351339 return $ names ;
352340 }
353341
@@ -443,17 +431,20 @@ public function map($fdn, $name, $uuid) {
443431 * @return bool
444432 */
445433 public function unmap ($ name ) {
446- $ statement = $ this ->dbc ->prepare ('
447- DELETE FROM ` ' . $ this ->getTableName () . '`
448- WHERE `owncloud_name` = ? ' );
449-
450434 $ dn = array_search ($ name , $ this ->cache , true );
451435 if ($ dn !== false ) {
452436 unset($ this ->cache [$ dn ]);
453437 }
454438 $ this ->localNameToDnCache ?->remove($ name );
455439
456- return $ this ->modify ($ statement , [$ name ]);
440+ $ qb = $ this ->dbc ->getQueryBuilder ();
441+ try {
442+ return $ qb ->delete ($ this ->getTableName (false ))
443+ ->where ($ qb ->expr ()->eq ('owncloud_name ' , $ qb ->createNamedParameter ($ name )))
444+ ->executeStatement () > 0 ;
445+ } catch (Exception $ e ) {
446+ return false ;
447+ }
457448 }
458449
459450 /**
0 commit comments