我正在使用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;
}
}