Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento, login as customer from admin

I've extended the Mage_Adminhtml_CustomerController with a new action loginAction in order to be able to login as a customer from the admin interface.

I call the loginById on the customer/session, but the customer's session isn't modified after the redirect.

Can someone explain why? It should be a simple operation.

Here's a gist containing the loginAction

I appreciate any help.

Thanks!


Update:

I created a github-repo containing all the code for the module: https://github.com/KATT/Magento-CustomerLogin.

Once this issue is solved, it might be useful for someone else as well.

like image 935
Alex Avatar asked Sep 14 '11 08:09

Alex


People also ask

How can I unlock Magento 2 customer account?

Follow the steps to unlock your account from Magento Admin: Step 1: On the Admin sidebar, go to System > Permissions > Locked Users Step 2: In the grid, select the checkbox of the locked account. Step 3: In the upper-left corner, set Unlock from the Actions dropdown. Step 4: Click Submit to unlock the account.

What is the full URL to the Magento login page?

Default Base URL: http://yourdomain.com/magento/ Default Admin URL and Path: http://yourdomain.com/magento/admin.

How do I access Magento 2 admin panel?

When you install Magento 2, it will automatically create an admin URL with a random string. The Magento 2 admin URL allows you to access the backend of the store and then edit and manage the administrative tasks. Typically, the default Magento 2 admin URL is “yourdomain.com/magento/admin.”


1 Answers

Hi I created a way to login as a customer. With the following solution below you can get an action in the customer grid management view in backend for each customer:

You have to create a controller for the frontend and do a class rewrite of an admin block, please adapt to your situation and don't forget to create a xml file which activate your module at app/etc/modules/Yourmodule_Customer.xml Here is the config.xml of a module that you will have to create:

<?xml version="1.0"?>
<config>
<modules>
    <Yourmodule_Customer>
        <version>0.1.0</version>
    </Yourmodule_Customer>
</modules>
<global>
    <blocks>
      <adminhtml>
        <rewrite>
          <customer_grid>Yourmodule_Customer_Block_Adminhtml_Overwrite_Grid</customer_grid>
         </rewrite>
       </adminhtml>
</global>
<frontend>
    <routers>
        <customer>
            <args>
                <modules>
                    <customer before="Mage_Customer">Yourmodule_Customer_Overwrite</customer>
                </modules>
            </args>
        </customer>
    </routers>
</frontend>

Then you have to create a block class in the folder Youmodule/Customer/Block/Adminhtml/Overwrite/Grid.php with the following content: Be aware that if you have Store Code in URL activated, you need here to provide a default store code.

<?php
class Yourmodule_Customer_Block_Adminhtml_Overwrite_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $column = $this->getColumn('action');
        $actions = $column->getActions();
        $actions[] = array(
            'caption' => 'Log in',
            'popup' => true,
            'url' => array(
                'base' => 'customer/support/autologin',
                'params' => array('_store' => 'de', '_secure' => true)),
                'field' => 'customerid'
        );
        $column->setActions( $actions );

        return $this;
    }
}

Then you have to create a new frontend controller, in this case, it's restricted to authorized IP address defined in the backend configuration:

<?php
class Yourmodule_Customer_Overwrite_SupportController extends Mage_Core_Controller_Front_Action
{
    public function preDispatch(){

        parent::preDispatch();

        if (!$this->getRequest()->isDispatched()) {
            return;
        }

        $action = $this->getRequest()->getActionName();
        $pattern = '/^(autologin)/i';
        if (!preg_match($pattern, $action) && Mage::helper('core')->isDevAllowed(Mage::app()->getStore()->getId())) {
            if (!$this->_getSession()->authenticate($this)) {
                $this->setFlag('', 'no-dispatch', true);
            }
        } else {
            $this->_getSession()->setNoReferer(true);
        }
    }

    public function autologinAction(){
        $session = $this->_getSession();

        $id = (int) trim($this->getRequest()->getParam('customerid'));
        try{
            if($id){
                $customer = Mage::getModel('customer/customer')->load($id);
                $session->setCustomerAsLoggedIn($customer);
                $message = $this->__('You are now logged in as %s', $customer->getName());
                $session->addNotice($message);
                Mage::log($message);
            }else{
                throw new Exception ($this->__('Auto Loggin didn\'t worked. Some parameter are missing'));
            }
        }catch (Exception $e){
            $session->addError($e->getMessage());
        }
        $this->_redirect('customer/account');       
    }

    public function _getSession(){
        return Mage::getSingleton('customer/session');
    }
}
like image 179
Sylvain Rayé Avatar answered Oct 27 '22 03:10

Sylvain Rayé