| 
 | 1 | +diff --git a/vendor/magento/module-company/Plugin/Company/CollectionFilter.php b/vendor/magento/module-company/Plugin/Company/CollectionFilter.php  | 
 | 2 | +new file mode 100644  | 
 | 3 | +index 000000000000..cd5d83a1799f  | 
 | 4 | +--- /dev/null  | 
 | 5 | ++++ b/vendor/magento/module-company/Plugin/Company/CollectionFilter.php  | 
 | 6 | +@@ -0,0 +1,124 @@  | 
 | 7 | ++<?php  | 
 | 8 | ++/************************************************************************  | 
 | 9 | ++ *  | 
 | 10 | ++ * ADOBE CONFIDENTIAL  | 
 | 11 | ++ * ___________________  | 
 | 12 | ++ *  | 
 | 13 | ++ * Copyright 2024 Adobe  | 
 | 14 | ++ * All Rights Reserved.  | 
 | 15 | ++ *  | 
 | 16 | ++ * NOTICE: All information contained herein is, and remains  | 
 | 17 | ++ * the property of Adobe and its suppliers, if any. The intellectual  | 
 | 18 | ++ * and technical concepts contained herein are proprietary to Adobe  | 
 | 19 | ++ * and its suppliers and are protected by all applicable intellectual  | 
 | 20 | ++ * property laws, including trade secret and copyright laws.  | 
 | 21 | ++ * Dissemination of this information or reproduction of this material  | 
 | 22 | ++ * is strictly forbidden unless prior written permission is obtained  | 
 | 23 | ++ * from Adobe.  | 
 | 24 | ++ * ************************************************************************  | 
 | 25 | ++ */  | 
 | 26 | ++declare(strict_types=1);  | 
 | 27 | ++  | 
 | 28 | ++namespace Magento\Company\Plugin\Company;  | 
 | 29 | ++  | 
 | 30 | ++use Magento\Authorization\Model\Role;  | 
 | 31 | ++use Magento\Backend\Model\Auth\Session;  | 
 | 32 | ++use Magento\Company\Model\ResourceModel\Company\Collection;  | 
 | 33 | ++use Magento\Framework\App\RequestInterface;  | 
 | 34 | ++use Magento\Framework\DB\Select;  | 
 | 35 | ++use Magento\Framework\Exception\LocalizedException;  | 
 | 36 | ++  | 
 | 37 | ++/**  | 
 | 38 | ++ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)  | 
 | 39 | ++ */  | 
 | 40 | ++class CollectionFilter  | 
 | 41 | ++{  | 
 | 42 | ++    private const FILTERED_FLAG_NAME = 'admin_gws_filtered_b2b';  | 
 | 43 | ++  | 
 | 44 | ++    /**  | 
 | 45 | ++     * @param Session $backendAuthSession  | 
 | 46 | ++     * @param RequestInterface $request  | 
 | 47 | ++     */  | 
 | 48 | ++    public function __construct(  | 
 | 49 | ++        private readonly Session $backendAuthSession,  | 
 | 50 | ++        private readonly RequestInterface $request  | 
 | 51 | ++    ) {  | 
 | 52 | ++    }  | 
 | 53 | ++  | 
 | 54 | ++    /**  | 
 | 55 | ++     * Adds only allowed websites to company filter.  | 
 | 56 | ++     *  | 
 | 57 | ++     * @param Collection $collection  | 
 | 58 | ++     * @param bool $printQuery  | 
 | 59 | ++     * @param bool $logQuery  | 
 | 60 | ++     * @return array  | 
 | 61 | ++     * @throws LocalizedException  | 
 | 62 | ++     * @throws \Zend_Db_Select_Exception  | 
 | 63 | ++     */  | 
 | 64 | ++    public function beforeLoadWithFilter(  | 
 | 65 | ++        Collection $collection,  | 
 | 66 | ++        bool $printQuery = false,  | 
 | 67 | ++        bool $logQuery = false  | 
 | 68 | ++    ): array {  | 
 | 69 | ++        $this->filterCollection($collection);  | 
 | 70 | ++  | 
 | 71 | ++        return [$printQuery, $logQuery];  | 
 | 72 | ++    }  | 
 | 73 | ++  | 
 | 74 | ++    /**  | 
 | 75 | ++     * Adds only allowed websites company filter count.  | 
 | 76 | ++     *  | 
 | 77 | ++     * @param Collection $collection  | 
 | 78 | ++     * @throws LocalizedException  | 
 | 79 | ++     * @throws \Zend_Db_Select_Exception  | 
 | 80 | ++     */  | 
 | 81 | ++    public function beforeGetSelectCountSql(Collection $collection): void  | 
 | 82 | ++    {  | 
 | 83 | ++        $this->filterCollection($collection);  | 
 | 84 | ++    }  | 
 | 85 | ++  | 
 | 86 | ++    /**  | 
 | 87 | ++     * Add filter to collection.  | 
 | 88 | ++     *  | 
 | 89 | ++     * @param Collection $collection  | 
 | 90 | ++     * @throws LocalizedException  | 
 | 91 | ++     * @throws \Zend_Db_Select_Exception  | 
 | 92 | ++     */  | 
 | 93 | ++    private function filterCollection(Collection $collection): void  | 
 | 94 | ++    {  | 
 | 95 | ++        $role = $this->backendAuthSession->getUser() ?  | 
 | 96 | ++            $this->backendAuthSession->getUser()->getRole() : null;  | 
 | 97 | ++        if ($role && !$role->getGwsIsAll() && !$collection->getFlag(self::FILTERED_FLAG_NAME)) {  | 
 | 98 | ++            if (isset($collection->getSelect()->getPart(Select::FROM)['main_table'])  | 
 | 99 | ++                && $this->request->getParam('id') === null) {  | 
 | 100 | ++                $this->tableBasedFilterByCompanyAdmin($collection, $role);  | 
 | 101 | ++                $collection->setFlag(self::FILTERED_FLAG_NAME, true);  | 
 | 102 | ++            }  | 
 | 103 | ++        }  | 
 | 104 | ++    }  | 
 | 105 | ++  | 
 | 106 | ++    /**  | 
 | 107 | ++     * Filter the company collection by company admins' store/website.  | 
 | 108 | ++     *  | 
 | 109 | ++     * @param Collection $collection  | 
 | 110 | ++     * @param Role $role  | 
 | 111 | ++     * @return void  | 
 | 112 | ++     */  | 
 | 113 | ++    private function tableBasedFilterByCompanyAdmin(Collection $collection, Role $role): void  | 
 | 114 | ++    {  | 
 | 115 | ++        $restrictedWebsiteIds = $role->getGwsWebsites();  | 
 | 116 | ++  | 
 | 117 | ++        $existsSelect = $collection->getConnection()->select()->from(  | 
 | 118 | ++            ['admin_entity' => $collection->getTable('company_advanced_customer_entity')]  | 
 | 119 | ++        )->joinLeft(  | 
 | 120 | ++            ['customer_grid_flat' => $collection->getTable('customer_grid_flat')],  | 
 | 121 | ++            'admin_entity.customer_id = customer_grid_flat.entity_id',  | 
 | 122 | ++            ['website_id' => 'website_id']  | 
 | 123 | ++        )->where('customer_grid_flat.website_id IN (?)', $restrictedWebsiteIds);  | 
 | 124 | ++  | 
 | 125 | ++        $collection->getSelect()->exists(  | 
 | 126 | ++            $existsSelect,  | 
 | 127 | ++            'admin_entity.company_id = main_table.entity_id'  | 
 | 128 | ++        );  | 
 | 129 | ++    }  | 
 | 130 | ++}  | 
 | 131 | +diff --git a/vendor/magento/module-company/etc/adminhtml/di.xml b/vendor/magento/module-company/etc/adminhtml/di.xml  | 
 | 132 | +index 883df345fe1b..45821f657cab 100644  | 
 | 133 | +--- a/vendor/magento/module-company/etc/adminhtml/di.xml  | 
 | 134 | ++++ b/vendor/magento/module-company/etc/adminhtml/di.xml  | 
 | 135 | +@@ -38,4 +38,7 @@  | 
 | 136 | +     <type name="Magento\Catalog\Model\Product\ReservedAttributeList">  | 
 | 137 | +         <plugin name="reservedAttributeListPlugin" type="Magento\Company\Plugin\Catalog\Model\Product\ReservedAttributeListPlugin"/>  | 
 | 138 | +     </type>  | 
 | 139 | ++    <type name="Magento\Company\Model\ResourceModel\Company\Collection">  | 
 | 140 | ++        <plugin name="admin_gws_collection_filter" type="Magento\Company\Plugin\Company\CollectionFilter"/>  | 
 | 141 | ++    </type>  | 
 | 142 | + </config>  | 
0 commit comments