Skip to content

Commit

Permalink
Merge pull request #3 from fruitcake/feat-add-link
Browse files Browse the repository at this point in the history
Add button to login as customer from grid
  • Loading branch information
barryvdh authored Jul 17, 2024
2 parents 39af473 + 7468556 commit 29113d1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
98 changes: 98 additions & 0 deletions Ui/Component/Listing/Column/LoginAsCustomer.php
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]);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
],
"require": {
"php": "^7|^8",
"php": "^7.1|^8",
"magento/module-login-as-customer": "^100.4"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<module name="Fruitcake_AlwaysLoginAsCustomer">
<sequence>
<module name="Magento_LoginAsCustomer"/>
<module name="Magento_LoginAsCustomerAdminUi"/>
</sequence>
</module>
</config>
10 changes: 10 additions & 0 deletions view/adminhtml/layout/customer_index_index.xml
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>
15 changes: 15 additions & 0 deletions view/adminhtml/ui_component/customer_listing.xml
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>

0 comments on commit 29113d1

Please sign in to comment.