Magento,从管理员登录为客户


Magento, login as customer from admin

我用一个新的动作loginAction扩展了Mage_Adminhtml_CustomerController,以便能够从管理界面作为客户登录。

我在customer/session上调用loginById,但是客户的会话在重定向后没有被修改。

有人能解释一下吗?这应该是一个简单的操作。

以下是包含loginAction

的要点我很感激你的帮助。

谢谢!


更新:

我创建了一个包含该模块所有代码的github-repo: https://github.com/KATT/Magento-CustomerLogin.

一旦这个问题解决了,它可能对其他人也很有用。

嗨,我创建了一种以客户身份登录的方法。使用下面的解决方案,您可以在后端为每个客户获得客户网格管理视图中的操作:

你必须为前端创建一个控制器并对管理块进行类重写,请适应你的情况,不要忘记在app/etc/modules/Yourmodule_Customer.xml中创建一个xml文件来激活你的模块下面是您必须创建的模块的config.xml:

<?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>

然后你必须在Youmodule/Customer/block/Adminhtml/Overwrite/Grid.php文件夹中创建一个block类,内容如下:请注意,如果你在URL中激活了存储代码,你需要在这里提供一个默认的存储代码。

<?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;
    }
}

然后你必须创建一个新的前端控制器,在这种情况下,它被限制在后端配置中定义的授权IP地址:

<?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');
    }
}

我意识到这个问题已经得到了回答,但是对于那些寻找不同(不是说更好)方法的人来说,这里是我写的Magento扩展https://github.com/ajzele/Inchoo_LoginAsCustomer。它不重写任何块,它使用事件/观察者在客户编辑屏幕上注入Login as Customer按钮。它进一步使用管理和前端控制器的组合来传递加密的客户信息,以便它知道要登录哪个客户。

您不应该通过客户模型登录吗?

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
OR
Mage::getSingleton('customer/session')->loginById($customer->getId());

对于那些不想调试解决方案的人,您可以通过magento connect安装扩展:http://www.magentocommerce.com/magento-connect/login-as-customer-9893.html它允许从客户视图和订单视图页面从管理员登录。扩展是稳定的,并在所有情况下工作良好