vendor/blackbit/data-director/EventListener/QuickSearchListener.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright Blackbit digital Commerce GmbH <info@blackbit.de>
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
  6.  *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7.  *
  8.  *  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  9.  */
  10. namespace Blackbit\DataDirectorBundle\EventListener;
  11. use Blackbit\DataDirectorBundle\lib\Pim\Helper;
  12. use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
  13. use Blackbit\DataDirectorBundle\model\Dataport;
  14. use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
  15. use Pimcore\Bundle\SimpleBackendSearchBundle\Model\Search\Backend\Data\Id;
  16. use Pimcore\Db;
  17. use Pimcore\Model\Asset;
  18. use Pimcore\Model\DataObject\AbstractObject;
  19. use Pimcore\Model\DataObject\ClassDefinition;
  20. use Pimcore\Model\DataObject\ClassDefinition\Data;
  21. use Pimcore\Model\DataObject\ClassDefinition\Listing;
  22. use Pimcore\Model\DataObject\Concrete;
  23. use Pimcore\Model\Document;
  24. use Pimcore\Model\Element\ElementInterface;
  25. use Pimcore\Model\Element\Service;
  26. use Pimcore\Model\Listing\AbstractListing;
  27. use Pimcore\Translation\Translator;
  28. use Psr\Log\LoggerInterface;
  29. class QuickSearchListener
  30. {
  31.     public function loadByKeyFields($event) {
  32.         $db \Pimcore\Db::get();
  33.         $query $event->getArgument('query');
  34.         $searcherList = new \Pimcore\Bundle\SimpleBackendSearchBundle\Model\Search\Backend\Data\Listing();
  35.         $conditionParts = [];
  36.         $conditionParts[] = $this->getPermittedPaths();
  37.         $queryParts preg_split('/(\s+|,|\.)/'$query);
  38.         foreach ($queryParts as &$queryPart) {
  39.             if (mb_substr($queryPart01) !== '-' && strlen($queryPart) > 2) {
  40.                 $queryPart '+'.$queryPart;
  41.             }
  42.         }
  43.         unset($queryPart);
  44.         $matchCondition 'MATCH (`data`,`properties`) AGAINST ('.$db->quote(implode(' '$queryParts)).' IN BOOLEAN MODE)';
  45.         $conditionParts[] = $matchCondition." AND `type` != 'folder'";
  46.         $archiveFolders = [];
  47.         foreach(Dataport::getInstance()->find() as $dataport) {
  48.             if(!empty($dataport['sourceconfig']['archiveFolder'])) {
  49.                 $archiveFolders[] = rtrim($dataport['sourceconfig']['archiveFolder'], '/').'/';
  50.             }
  51.         }
  52.         $assetArchiveFolders PimcoreDbRepository::getInstance()->findColumnInSql('SELECT path FROM assets WHERE path IN (?)', [$archiveFolders]);
  53.         foreach($assetArchiveFolders as $assetArchiveFolder) {
  54.             $conditionParts[] = 'fullpath NOT LIKE '.$db->quote($assetArchiveFolder.'%');
  55.         }
  56.         $queryCondition '('.implode(') AND ('array_unique($conditionParts)).')';
  57.         $searcherList->setCondition($queryCondition);
  58.         $searcherList->setLimit(50);
  59.         $searcherList->setOrderKey($matchConditionfalse);
  60.         $searcherList->setOrder('DESC');
  61.         $event->setArgument('list'$searcherList);
  62.     }
  63.     protected function getPermittedPaths($types = ['asset''document''object'])
  64.     {
  65.         $user Helper::getUser();
  66.         if($user->isAdmin()) {
  67.             return '1';
  68.         }
  69.         $db \Pimcore\Db::get();
  70.         $allowedTypes = [];
  71.         foreach ($types as $type) {
  72.             if ($user->isAllowed($type.'s')) { //the permissions are just plural
  73.                 $elementPaths Service::findForbiddenPaths($type$user);
  74.                 $forbiddenPathSql = [];
  75.                 $allowedPathSql = [];
  76.                 foreach ($elementPaths['forbidden'] as $forbiddenPath => $allowedPaths) {
  77.                     $exceptions '';
  78.                     $folderSuffix '';
  79.                     if ($allowedPaths) {
  80.                         $exceptionsConcat implode("%' OR fullpath LIKE '"$allowedPaths);
  81.                         $exceptions " OR (fullpath LIKE '".$exceptionsConcat."%')";
  82.                         $folderSuffix '/'//if allowed children are found, the current folder is listable but its content is still blocked, can easily done by adding a trailing slash
  83.                     }
  84.                     $forbiddenPathSql[] = ' (fullpath NOT LIKE '.$db->quote($forbiddenPath.$folderSuffix.'%').$exceptions.') ';
  85.                 }
  86.                 foreach ($elementPaths['allowed'] as $allowedPaths) {
  87.                     $allowedPathSql[] = ' fullpath LIKE '.$db->quote($allowedPaths.'%');
  88.                 }
  89.                 // this is to avoid query error when implode is empty.
  90.                 // the result would be like `(maintype = type AND ((path1 OR path2) AND (not_path3 AND not_path4)))`
  91.                 $forbiddenAndAllowedSql '(maintype = \''.$type.'\'';
  92.                 if ($allowedPathSql || $forbiddenPathSql) {
  93.                     $forbiddenAndAllowedSql .= ' AND (';
  94.                     $forbiddenAndAllowedSql .= $allowedPathSql '( '.implode(' OR '$allowedPathSql).' )' '';
  95.                     if ($forbiddenPathSql) {
  96.                         //if $allowedPathSql "implosion" is present, we need `AND` in between
  97.                         $forbiddenAndAllowedSql .= $allowedPathSql ' AND ' '';
  98.                         $forbiddenAndAllowedSql .= implode(' AND '$forbiddenPathSql);
  99.                     }
  100.                     $forbiddenAndAllowedSql .= ' )';
  101.                 }
  102.                 $forbiddenAndAllowedSql .= ' )';
  103.                 $allowedTypes[] = $forbiddenAndAllowedSql;
  104.             }
  105.         }
  106.         //if allowedTypes is still empty after getting the workspaces, it means that there are no any main permissions set
  107.         // by setting a `false` condition in the query makes sure that nothing would be displayed.
  108.         if (!$allowedTypes) {
  109.             $allowedTypes = ['false'];
  110.         }
  111.         return '('.implode(' OR '$allowedTypes).')';
  112.     }
  113. }