从后端在前端自动登录


15

请参阅以下情形。
我有一些自定义模块,允许前端用户对某些自定义实体执行某些操作。(细节不是很重要)。
要求管理员应该能够使用客户帐户(没有密码)在前端登录,并能够为客户执行这些操作。
由于您不能使用后端的前端会话,并且我不想为前端创建永久的自动登录链接,因为这可能是一个很大的安全漏洞,这就是我到目前为止所做的。

  • 为客户实体添加一个空属性。(叫它login_key
  • 在客户编辑页面的后端添加一个按钮,该按钮重定向到管理页面,在该页面中生成一个随机字符串并将其保存在属性中login_key
  • 在同一操作中,我将管理员重定向到这样的前端URL autologin/index/index/customer_id/7/login_key/ajkshdkjah123123(在上一步中生成的值)。
  • 在前端网址中,如果客户ID与login_key特定客户匹配,则我在会话中设置了客户对象(如已登录)并删除了login_key该对象,因此该网址将来将无法使用。

这可以正常工作。我的意思是,我以所选客户的身份登录,并且用于自动登录的链接第二次不起作用。
不利的一面是,如果2个管理员大约同时单击“自动登录”按钮,则一个人将无法登录,但这是可以接受的风险。
我主要担心的是,这也可能是一个(不是那个)重大的安全问题。有人可以看到这种方法有问题吗?还是建议一个更好的?
可以忽略客户帐户可以由网站分隔的事实。这并不重要,也可以轻松管理。


常规的管理员URL密钥不会给您同样的安全性吗?
kalenjordan

@kalenjordan问题不是管理部分。接缝好。我担心的是在调用自动​​登录的前端URL时。我不能在其中使用管理URL密钥。
马吕斯

嗯,对不起。您是否已签出magentocommerce.com/magento-connect/login-as-customer-9893.html?它会在管理员每次登录尝试时生成唯一记录,并在前端控制器中使用与客户ID相关联的唯一哈希。
kalenjordan

@kalenjordan哈哈。我不知道那个扩展名。但是从您所描述的是我在问题中所描述的相同方法。:)。我会看一下。谢谢。
马里斯(Marius)

1
@ mageUz.True,但是就像我说的那样,这是可以接受的风险。我在这里更关心安全性。
马里斯(Marius)

Answers:


9

由于没有人提出一个很好的理由不执行我的要求,因此我认为我的方法是安全的。因此,为了不让这个问题公开,我决定将代码添加为答案并将其标记为已接受。
因此,我有一个名为Easylife_Simulate以下文件的新扩展名: app/etc/modules/Easylife_Simulte.xml-声明文件:

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Simulate>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Customer />
            </depends>
        </Easylife_Simulate>
    </modules>
</config>

app/code/local/Easylife/Simulte/etc/config.xml -配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Simulate>
            <version>0.0.1</version>
        </Easylife_Simulate>
    </modules>
    <global>
        <helpers>
            <easylife_simulate>
                <class>Easylife_Simulate_Helper</class>
            </easylife_simulate>
        </helpers>
        <models>
            <easylife_simulate>
                <class>Easylife_Simulate_Model</class>
            </easylife_simulate>
        </models>
        <resources>
            <easylife_simulate_setup>
                <setup>
                    <module>Easylife_Simulate</module>
                    <class>Mage_Customer_Model_Resource_Setup</class>
                </setup>
            </easylife_simulate_setup>
        </resources>
    </global>
    <frontend>
        <routers>
            <easylife_simulate>
                <use>standard</use>
                <args>
                    <module>Easylife_Simulate</module>
                    <frontName>simulate</frontName>
                </args>
            </easylife_simulate>
        </routers>
    </frontend>
    <adminhtml>
        <events>
            <controller_action_layout_render_before_adminhtml_customer_edit>
                <observers>
                    <easylife_simulate>
                        <class>easylife_simulate/observer</class>
                        <method>addAutoLoginButton</method>
                    </easylife_simulate>
                </observers>
            </controller_action_layout_render_before_adminhtml_customer_edit>
        </events>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Easylife_Simulate before="Mage_Adminhtml">Easylife_Simulate_Adminhtml</Easylife_Simulate>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

app/code/local/Easylife/Simulate/sql/easylife_simulate_setup/install-0.0.1.php -安装脚本-添加新的客户属性:

<?php
$this->addAttribute('customer', 'login_key', array(
    'type'      => 'text',
    'label'     => 'Auto login key',
    'input'     => 'text',
    'position'  => 999,
    'required'  => false
));

app/code/local/Easylife/Simulate/Model/Observer.php -观察员在客户管理员编辑表单中添加按钮

<?php
class Easylife_Simulate_Model_Observer extends Mage_ProductAlert_Model_Observer{
    public function addAutoLoginButton($observer){
        $block = Mage::app()->getLayout()->getBlock('customer_edit');
        if ($block){
            $customer = Mage::registry('current_customer');
            $block->addButton('login', array(
                'label'     => Mage::helper('customer')->__('Login as this customer'),
                'onclick'   => 'window.open(\''.Mage::helper('adminhtml')->getUrl('adminhtml/simulate/login', array('id'=>$customer->getId())).'\')',
            ), 100);
        }

    }
}

app/code/local/Easylife/Simulate/controllers/Adminhtml/SimulateController.php -处理上面生成的按钮的点击的管理控制器。

<?php
class Easylife_Simulate_Adminhtml_SimulateController extends Mage_Adminhtml_Controller_Action{
    public function loginAction(){
        $id = $this->getRequest()->getParam('id');
        $customer = Mage::getModel('customer/customer')->load($id);
        if (!$customer->getId()){
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('easylife_simulate')->__('Customer does not exist'));
            $this->_redirectReferer();
        }
        else {
            $key = Mage::helper('core')->uniqHash();
            $customer->setLoginKey($key)->save();
            $this->_redirect('simulate/index/index', array('id'=>$customer->getId(), 'login_key'=>$key));
        }
    }
}

app/code/local/Easylife/Simulate/controllers/IndexController.php -进行自动登录的前端控制器。

<?php
class Easylife_Simulate_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $id = $this->getRequest()->getParam('id');
        $key = $this->getRequest()->getParam('login_key');
        if (empty($key)){
            $this->_redirect('');
        }
        else{
            $customer = Mage::getModel('customer/customer')->load($id);
            if ($customer->getId() && $customer->getLoginKey() == $key){
                $customer->setLoginKey('')->save();
                Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
                Mage::getSingleton('customer/session')->renewSession();
            }
            $this->_redirect('customer/account/index');
        }
    }
}

app/code/local/Easylife/Simulte/Helper/Data.php -模块助手

<?php
class Easylife_Simulate_Helper_Data extends Mage_Core_Helper_Abstract{

}

而已。它为我工作。就像我在问题中说的那样,缺点是,如果2个管理员同时(大约)同时按下同一位客户的登录按钮,则其中一位将无法登录。但是他可以在几秒钟后重复该过程。


当有多个客户时会发生什么?
Milople Inc 2014年

@GarthHuff我不明白你的问题。请描述您的情况。
马吕斯

我认为,我已经更改了整个方案,我所做的是将用户名输入框替换为可能的用户名下拉列表,并从下拉列表中选择用户名时自动登录。这是我的实现techworkslab.pixub.com/2014/01/script-for-auto-login
Milople Inc

@GarthHuff。感谢您的脚本,但是我的问题与前端客户有关,而不是管理员。
马里乌斯

@Marius您打算制作此版本的Magento 2吗?

0

我们为客户服务团队使用了一种类似的方法,称为“幽灵登录”,在该方法中,我们可以通过admin中的客户帐户提供一个按钮。我们没有为login_key或类似的东西使用任何自定义属性,而是实际上使用了从Mage_Customer_AccountController扩展的覆盖/自定义的loginAction来处理登录。

此外,在loginAction期间,在执行自定义逻辑和验证之后,我们将使用Mage_Customer_Model_Session :: setCustomerAsLoggedIn来确保我们不会丢失在登录期间可能执行的任何事件功能。如果您看一下这种方法,您会注意到它设置了会话中的客户并调度了customer_login事件。

在此处输入图片说明

通过这种方法,我们实际上可以让多个代理商作为我们选择的同一位客户登录(尽管我们不希望有多个代理商同时添加到同一帐户的购物车/下订单)。

我们已经使用了两年了,在此期间没有明显的问题。


1
谢谢(你的)信息。setCustomerAsLoggedIn出于与您相同的原因,我也在代码中使用了。但是我很好奇用于自动登录的方法。(如果不是秘密的话)。
马吕斯

我们构建了一个自定义模块来处理此问题,该模块是从核心前端登录功能扩展而来的。
Anthony Leach Jr,

我明白了。我在问一些代码,如果可能的话,或者至少是代码背后的想法。或者,如果我的想法是安全的或不安全的,也许有一些提示。
马吕斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.