自定制magento登陆验证过程

自定制magento登陆验证过程
自定制magento登陆验证过程

现在有一个客户需要将Magento与另一个系统整合,在Magento中产生购买的时候,要求用户使用原系统用户名密码登陆,验证此用户信息为合法用户后,用户信息存入到Magento中,并且完成购买流程。得知原系统使用用户名来完成注册登陆,并且标示用户唯一性,与Magento使用邮箱登陆不同。

这个要求需要注意的是:

1.用户只在原先的系统完成注册。

2.需要通过接口去原先系统验证用户的登陆信息是否合法。

3.Magento 的用户是通过邮件地址来登陆的,所以要给原系统的用户名后加个邮箱,再存到Magento中去。(不想大改,直接实现用户名登陆比较复杂。一个基于邮箱的系统,改成基于用户名的意义还有待考虑)。

于是:

1.关闭Magento的注册功能,使用户只能在原先的系统平台注册。

2.关闭游客结账功能,在结账页面让用户输入原系统用户名、密码。

3.下面我们就开始修改下这个登陆验证的流程了,我们关闭了注册功能也禁止了游客结账,那么我们就剩下在结账页面的登陆框了(如上图),这里就是我们的需要hack的地方。

a.在模板文件中 app/design/frontend/default/default/template/checkout/onepage/login.phtml 修改登陆表单为

<div class="col2-set">
	<div class="col-1">
	</div>
	<div class="col-2">
        <h3><?php echo $this->__('Login') ?></h3>
        <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
        <form id="login-form" action="<?php echo $this->getloginasusernamePost() ?>" method="post">

            <h4><?php echo $this->__('Already registered?') ?></h4>
            <p><?php echo $this->__('Please log in below:') ?></p>
            <ul class="form-list">
                <li>
                    <label for="login-username" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="text" class="input-text required-entry" id="login-username" name="login[username]" value="<?php echo $this->htmlEscape($this->getUsername()) ?>" />
                    </div>
                </li>
                <li>
                    <label for="login-password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                    <div class="input-box">
                        <input type="password" class="input-text required-entry" id="login-password" name="login[password]" />
                    </div>
                </li>
            </ul>
			<input type="submit" value="Submit" />
        </form>
    </div>

</div>

我们注意到,此表单的action为 $this->getloginasusernamePost(),此表单简写,你需要补充上更多自己需要的内容。

因为我们使用我们自己的登陆方式,所以我们增加属于我们自己的action

在 app/code/core/Mage/Customer/controllers/AccountController.php (请勿像我一样直接修改核心文件,见我的文章:重写Magento的Controller(控制器))添加我们自己的action如下

//add by hellokeykey.com loginasusernamePost
	public function loginasusernamePostAction(){
		if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $session = $this->_getSession();

        if ($this->getRequest()->isPost()){
			$login = $this->getRequest()->getPost('login');
			if (!empty($login['username']) && !empty($login['password'])) {
				//将我们需要做的事情放在这个里面
				// check first
				//......
				//then save or not
				//.......
				//$session->addError($this->__('Login and password are required.'));//可以使用这个做调试
			}
		}
		$this->_loginPostRedirect();
	}

我们注意到,这个是根据loginPostAction()修改的,中间省略部分为我们需要做的操作,完成验证,保存,登陆动作。你需要根据你的实际情况做更多的调整。

这样我们就有了一个只属于我们自己的登陆验证过程。

注意:你的Magento版本,本例使用Magento1.5,使用Default模板,其余版本可能稍有不同。也要注意Magento对表单的数据验证,比如email的输入框,只能输入email,如果想输入文本,属性就要改成text,见:magento中给form添加输入验证

转载表明出处:www.hellokeykey.com

 

 

《自定制magento登陆验证过程》有3个想法

  1. 钥匙你好,我想问下你的博文中的代码是用的wordpress什么代码插件的吗,谢谢

评论已关闭。