如何在Magento2中覆盖或禁用核心插件?


15

我正在使用Magento2建立市场。因此,我需要能够使用供应商的客户凭证来加载客户的订单。

问题在于,Magento2使用插件来验证只有该订单的客户(或管理员)才能加载该订单。

在这种情况下,我需要覆盖整个插件或覆盖protected方法isAllowed()。不修改内核怎么办?

Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization 看起来像这样:

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class Authorization
{
    /**
     * @var UserContextInterface
     */
    protected $userContext;

    /**
     * @param UserContextInterface $userContext
     */
    public function __construct(
        \Magento\Authorization\Model\UserContextInterface $userContext
    ) {
        $this->userContext = $userContext;
    }

    /**
     * Checks if order is allowed
     *
     * @param \Magento\Sales\Model\ResourceModel\Order $subject
     * @param callable $proceed
     * @param \Magento\Framework\Model\AbstractModel $order
     * @param mixed $value
     * @param null|string $field
     * @return \Magento\Sales\Model\Order
     * @throws NoSuchEntityException
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundLoad(
        \Magento\Sales\Model\ResourceModel\Order $subject,
        \Closure $proceed,
        \Magento\Framework\Model\AbstractModel $order,
        $value,
        $field = null
    ) {
        $result = $proceed($order, $value, $field);
        if (!$this->isAllowed($order)) {
            throw NoSuchEntityException::singleField('orderId', $order->getId());
        }
        return $result;
    }

    /**
     * Checks if order is allowed for current customer
     *
     * @param \Magento\Sales\Model\Order $order
     * @return bool
     */
    protected function isAllowed(\Magento\Sales\Model\Order $order)
    {
        return $this->userContext->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER
            ? $order->getCustomerId() == $this->userContext->getUserId()
            : true;
    }
}

Answers:


23

1)您可以按名称禁用插件。就您而言authorization

<type name="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization">
    <plugin name="authorization" disabled="true" />
</type>
<type name="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization">
    <plugin name="vendor_name_authorization" type="Vendor\Name\Model\ResourceModel\Plugin\Sales\Order\Authorization" sortOrder="1" />
</type>

然后,您应该创建自己的类,这将扩展magento插件类。在自己的类中,您可以覆盖受保护的方法。

2)您可以执行此操作,而无需删除并重新创建插件:

<type name="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization">
    <plugin name="authorization" type="Vendor\Name\Model\ResourceModel\Plugin\Sales\Order\Authorization" sortOrder="1" />
</type>

您的插件类的示例代码:

namespace Vendor\Name\Model\ResourceModel\Plugin\Sales\Order;
class Authorization extends \Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization
{
    protected function isAllowed(\Magento\Sales\Model\Order $order)
    {
            ///You code here
    }
}

我已经尝试通过覆盖webapi_rest文件夹中的插件来尝试,但是它不起作用
bhargav shastri

6

如果使用sergei.sss 1st解决方案,则将得到重复的Magento \ Sales \ Model \ ResourceModel \ Order \ Plugin \ Authorization
的错误正确的方法是:

<type name="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization">
    <plugin name="authorization" disabled="true" />
    <plugin name="vendor_name_authorization" type="Vendor\Name\Model\ResourceModel\Plugin\Sales\Order\Authorization" sortOrder="1" />
</type>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.