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

Open in your IDE?
  1. <?php
  2. /*
  3.  * The software is distributed under the enclosed dual licence agreement. Accordingly, it is the responsibility of the licensee to use the software under the terms of the GPLv3 or, if purchased, under the terms of the commercial licence. Simultaneous use of the software under both licence forms for the same licensed object or a subsequent ‘change’ of licence forms by the same licensee is not permitted.
  4.  *
  5.  * In accordance with the provisions of the respective licences, it is only possible to
  6.  * a.) use Blackbit software under GPLv3 with the Pimcore Community Edition under GPLv3 or
  7.  * b.) combine Blackbit software under a commercial licence and Pimcore under a commercial licence.
  8.  *
  9.  * You can use the software under the terms of the GNU General Public Licence, Version 3 (GPLv3). This brings with it many rights of use and freedoms:
  10.  * Open source - You can view the unencrypted programme code of our software.
  11.  * Customisation - You can adapt the software to your needs and develop it further as you wish.
  12.  * Distribution - You can copy, distribute and even sell the software or further developments of the software as long as you comply with the GNU GPL v3 licence. Because you become the owner of the code when you purchase the bundle, there is no warranty and no free service.
  13.  *
  14.  * Alternatively, you can use the software under the Blackbit Commercial Licence. This means that the software remains the property of Blackbit and may be used in one instance per licence. Blackbit grants the user a limited right of use and eliminates defects and errors and provides updates free of charge during the contract period. Optionally, separate service and support contracts can be concluded for Blackbit software.
  15.  *
  16.  * For further information, please refer to the respective licence files or contact sales@blackbit.de
  17.  */
  18. namespace Blackbit\DataDirectorBundle\EventListener;
  19. use Blackbit\DataDirectorBundle\lib\Pim\Helper;
  20. use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
  21. use Blackbit\DataDirectorBundle\model\Dataport;
  22. use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
  23. use Pimcore\Bundle\SimpleBackendSearchBundle\Model\Search\Backend\Data\Id;
  24. use Pimcore\Db;
  25. use Pimcore\Model\Asset;
  26. use Pimcore\Model\DataObject\AbstractObject;
  27. use Pimcore\Model\DataObject\ClassDefinition;
  28. use Pimcore\Model\DataObject\ClassDefinition\Data;
  29. use Pimcore\Model\DataObject\ClassDefinition\Listing;
  30. use Pimcore\Model\DataObject\Concrete;
  31. use Pimcore\Model\Document;
  32. use Pimcore\Model\Element\ElementInterface;
  33. use Pimcore\Model\Element\Service;
  34. use Pimcore\Model\Listing\AbstractListing;
  35. use Pimcore\Translation\Translator;
  36. use Psr\Log\LoggerInterface;
  37. class QuickSearchListener
  38. {
  39.     public function loadByKeyFields($event) {
  40.         $db \Pimcore\Db::get();
  41.         $query $event->getArgument('query');
  42.         $searcherList $event->getArgument('list');
  43.         $conditionParts = [];
  44.         $conditionParts[] = $this->getPermittedPaths();
  45.         $queryParts preg_split('/(\s+|,|\.)/'$query);
  46.         foreach ($queryParts as &$queryPart) {
  47.             if (mb_substr($queryPart01) !== '-' && strlen($queryPart) > 2) {
  48.                 $queryPart '+'.$queryPart;
  49.             }
  50.         }
  51.         unset($queryPart);
  52.         $matchCondition 'MATCH (`data`,`properties`) AGAINST ('.$db->quote(implode(' '$queryParts)).' IN BOOLEAN MODE)';
  53.         $conditionParts[] = $matchCondition." AND `type` != 'folder'";
  54.         $archiveFolders = ['_default_upload_bucket'];
  55.         foreach(Dataport::getInstance()->find() as $dataport) {
  56.             if(!empty($dataport['sourceconfig']['archiveFolder'])) {
  57.                 $archiveFolders[] = rtrim($dataport['sourceconfig']['archiveFolder'], '/').'/';
  58.             }
  59.         }
  60.         // filter for Pimcore asset folders (no filesystem folders)
  61.         $assetArchiveFolders PimcoreDbRepository::getInstance()->findColumnInSql('SELECT path FROM assets WHERE path IN (?)', [$archiveFolders]);
  62.         foreach($assetArchiveFolders as $assetArchiveFolder) {
  63.             $conditionParts[] = 'fullpath NOT LIKE '.$db->quote($assetArchiveFolder.'%');
  64.         }
  65.         $queryCondition '('.implode(') AND ('array_unique($conditionParts)).')';
  66.         $searcherList->setCondition($queryCondition);
  67.         $searcherList->setOrderKey($matchConditionfalse);
  68.         $event->setArgument('list'$searcherList);
  69.     }
  70.     protected function getPermittedPaths($types = ['asset''document''object'])
  71.     {
  72.         $user Helper::getUser();
  73.         if($user->isAdmin()) {
  74.             return '1';
  75.         }
  76.         $db \Pimcore\Db::get();
  77.         $allowedTypes = [];
  78.         foreach ($types as $type) {
  79.             if ($user->isAllowed($type.'s')) { //the permissions are just plural
  80.                 $elementPaths Service::findForbiddenPaths($type$user);
  81.                 $forbiddenPathSql = [];
  82.                 $allowedPathSql = [];
  83.                 foreach ($elementPaths['forbidden'] as $forbiddenPath => $allowedPaths) {
  84.                     $exceptions '';
  85.                     $folderSuffix '';
  86.                     if ($allowedPaths) {
  87.                         $exceptionsConcat implode("%' OR fullpath LIKE '"$allowedPaths);
  88.                         $exceptions " OR (fullpath LIKE '".$exceptionsConcat."%')";
  89.                         $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
  90.                     }
  91.                     $forbiddenPathSql[] = ' (fullpath NOT LIKE '.$db->quote($forbiddenPath.$folderSuffix.'%').$exceptions.') ';
  92.                 }
  93.                 foreach ($elementPaths['allowed'] as $allowedPaths) {
  94.                     $allowedPathSql[] = ' fullpath LIKE '.$db->quote($allowedPaths.'%');
  95.                 }
  96.                 // this is to avoid query error when implode is empty.
  97.                 // the result would be like `(maintype = type AND ((path1 OR path2) AND (not_path3 AND not_path4)))`
  98.                 $forbiddenAndAllowedSql '(maintype = \''.$type.'\'';
  99.                 if ($allowedPathSql || $forbiddenPathSql) {
  100.                     $forbiddenAndAllowedSql .= ' AND (';
  101.                     $forbiddenAndAllowedSql .= $allowedPathSql '( '.implode(' OR '$allowedPathSql).' )' '';
  102.                     if ($forbiddenPathSql) {
  103.                         //if $allowedPathSql "implosion" is present, we need `AND` in between
  104.                         $forbiddenAndAllowedSql .= $allowedPathSql ' AND ' '';
  105.                         $forbiddenAndAllowedSql .= implode(' AND '$forbiddenPathSql);
  106.                     }
  107.                     $forbiddenAndAllowedSql .= ' )';
  108.                 }
  109.                 $forbiddenAndAllowedSql .= ' )';
  110.                 $allowedTypes[] = $forbiddenAndAllowedSql;
  111.             }
  112.         }
  113.         //if allowedTypes is still empty after getting the workspaces, it means that there are no any main permissions set
  114.         // by setting a `false` condition in the query makes sure that nothing would be displayed.
  115.         if (!$allowedTypes) {
  116.             $allowedTypes = ['false'];
  117.         }
  118.         return '('.implode(' OR '$allowedTypes).')';
  119.     }
  120. }