-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from fruitcake/feat-add-link
Add button to login as customer from grid
- Loading branch information
Showing
5 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
namespace Fruitcake\AlwaysLoginAsCustomer\Ui\Component\Listing\Column; | ||
|
||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\AuthorizationInterface; | ||
use Magento\Framework\Escaper; | ||
use Magento\Framework\UrlInterface; | ||
use Magento\Framework\View\Element\UiComponent\ContextInterface; | ||
use Magento\Framework\View\Element\UiComponentFactory; | ||
use Magento\LoginAsCustomerAdminUi\Ui\Customer\Component\Button\DataProvider; | ||
use Magento\LoginAsCustomerApi\Api\ConfigInterface; | ||
use Magento\Ui\Component\Listing\Columns\Column; | ||
|
||
class LoginAsCustomer extends Column | ||
{ | ||
/** | ||
* @var AuthorizationInterface | ||
*/ | ||
private $authorization; | ||
|
||
/** | ||
* @var ConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var DataProvider | ||
*/ | ||
private $dataProvider; | ||
|
||
/** | ||
* @var \Magento\Framework\Data\Form\FormKey | ||
*/ | ||
private $formKey; | ||
|
||
/** | ||
* @var Escaper | ||
*/ | ||
private $escaper; | ||
|
||
public function __construct( | ||
ContextInterface $context, | ||
UiComponentFactory $uiComponentFactory, | ||
ConfigInterface $config, | ||
\Magento\Framework\AuthorizationInterface $authorization, | ||
\Magento\Framework\Data\Form\FormKey $formKey, | ||
Escaper $escaper, | ||
?DataProvider $dataProvider = null, | ||
array $components = [], | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $uiComponentFactory, $components, $data); | ||
$this->authorization = $authorization; | ||
$this->config = $config; | ||
$this->dataProvider = $dataProvider ?? ObjectManager::getInstance()->get(DataProvider::class); | ||
$this->formKey = $formKey; | ||
$this->escaper = $escaper; | ||
} | ||
|
||
/** | ||
* @param array $dataSource | ||
* @return array | ||
*/ | ||
public function prepareDataSource(array $dataSource): array | ||
{ | ||
$isAllowed = $this->authorization->isAllowed('Magento_LoginAsCustomer::login'); | ||
$isEnabled = $this->config->isEnabled(); | ||
|
||
if (!$isEnabled || !$isAllowed) { | ||
return $dataSource; | ||
} | ||
|
||
// FormKey needs to be available for the popup | ||
$formKey = $this->formKey->getFormKey(); | ||
|
||
if (isset($dataSource['data']['items'])) { | ||
foreach ($dataSource['data']['items'] as &$item) { | ||
$data = $this->dataProvider->getData($item['entity_id']); | ||
$item[$this->getName()] = '<input name="form_key" type="hidden" value="'.$formKey.'"/> | ||
<button onclick="' .$this->escaper->escapeHtmlAttr($data['on_click']) .'">'.$this->escaper->escapeHtml(__('Login as customer')) .'</button>'; | ||
} | ||
} | ||
|
||
return $dataSource; | ||
} | ||
|
||
/** | ||
* Get Login as Customer login url. | ||
* | ||
* @param int $customerId | ||
* @return string | ||
*/ | ||
private function getLoginUrl(int $customerId): string | ||
{ | ||
return $this->urlBuilder->getUrl('loginascustomer/login/login', ['customer_id' => $customerId]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd"> | ||
<update handle="loginascustomer_confirmation_popup" /> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> | ||
<columns name="customer_columns"> | ||
<column name="login_as_customer" class="Fruitcake\AlwaysLoginAsCustomer\Ui\Component\Listing\Column\LoginAsCustomer"> | ||
<argument name="data" xsi:type="array"> | ||
<item name="config" xsi:type="array"> | ||
<item name="sortOrder" xsi:type="number">90</item> | ||
<item name="label" xsi:type="string" translate="true">Login as Customer</item> | ||
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item> | ||
</item> | ||
</argument> | ||
</column> | ||
</columns> | ||
</listing> |