diff --git a/vendor/magento/module-company/Plugin/Company/CollectionFilter.php b/vendor/magento/module-company/Plugin/Company/CollectionFilter.php new file mode 100644 index 000000000000..cd5d83a1799f --- /dev/null +++ b/vendor/magento/module-company/Plugin/Company/CollectionFilter.php @@ -0,0 +1,124 @@ +filterCollection($collection); + + return [$printQuery, $logQuery]; + } + + /** + * Adds only allowed websites company filter count. + * + * @param Collection $collection + * @throws LocalizedException + * @throws \Zend_Db_Select_Exception + */ + public function beforeGetSelectCountSql(Collection $collection): void + { + $this->filterCollection($collection); + } + + /** + * Add filter to collection. + * + * @param Collection $collection + * @throws LocalizedException + * @throws \Zend_Db_Select_Exception + */ + private function filterCollection(Collection $collection): void + { + $role = $this->backendAuthSession->getUser() ? + $this->backendAuthSession->getUser()->getRole() : null; + if ($role && !$role->getGwsIsAll() && !$collection->getFlag(self::FILTERED_FLAG_NAME)) { + if (isset($collection->getSelect()->getPart(Select::FROM)['main_table']) + && $this->request->getParam('id') === null) { + $this->tableBasedFilterByCompanyAdmin($collection, $role); + $collection->setFlag(self::FILTERED_FLAG_NAME, true); + } + } + } + + /** + * Filter the company collection by company admins' store/website. + * + * @param Collection $collection + * @param Role $role + * @return void + */ + private function tableBasedFilterByCompanyAdmin(Collection $collection, Role $role): void + { + $restrictedWebsiteIds = $role->getGwsWebsites(); + + $existsSelect = $collection->getConnection()->select()->from( + ['admin_entity' => $collection->getTable('company_advanced_customer_entity')] + )->joinLeft( + ['customer_grid_flat' => $collection->getTable('customer_grid_flat')], + 'admin_entity.customer_id = customer_grid_flat.entity_id', + ['website_id' => 'website_id'] + )->where('customer_grid_flat.website_id IN (?)', $restrictedWebsiteIds); + + $collection->getSelect()->exists( + $existsSelect, + 'admin_entity.company_id = main_table.entity_id' + ); + } +} diff --git a/vendor/magento/module-company/etc/adminhtml/di.xml b/vendor/magento/module-company/etc/adminhtml/di.xml index 883df345fe1b..45821f657cab 100644 --- a/vendor/magento/module-company/etc/adminhtml/di.xml +++ b/vendor/magento/module-company/etc/adminhtml/di.xml @@ -38,4 +38,7 @@ + + +