在CakePHP v2.0中使用电子邮件地址或用户名登录 [英] Login with email address or username in CakePHP v2.0

查看:75
本文介绍了在CakePHP v2.0中使用电子邮件地址或用户名登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2.0之前的CakePHP中,您可以通过以下方式允许用户使用其电子邮件地址登录:停止autoRedirect,然后将用户名数据与数据库中的电子邮件列进行比较(显然,如果不是电子邮件,那么Cake可以回退到用户名检查) 。

In CakePHP prior to 2.0 you could allow a user to login using their email address by stopping the autoRedirect and then comparing the username data to the email column in your database (apparently Cake could then fallback to username checks if not an email).

在CakePHP 2.0中,这已更改,您可以使用 $ this-> Auth-> login()

In CakePHP 2.0 this has changed and you login manually using $this->Auth->login()

我的问题是如何使它在2.0下工作?我有一些相当复杂的代码,可以执行各种操作,例如处理ajax和回发请求,如果用户尝试登录过多次则锁定帐户等,所以它很长!

My question is how do I get this working for 2.0? I have some quite complicated code that does a variety of things such as handle ajax and postback requests, locking of accounts if a user tries to login too many times etc so it's quite long!

如您所见,我将检查该帐户是否实际存在,因此在进行身份验证之前,如果出现这种情况,我会显示一条未找到帐户的消息,如果5次失败尝试,也可以使用该消息来锁定该用户的帐户

As you will see I check if the account actually exists manually so I can show a message of account not found before going through the authentication process if the case, and also use this to lock the account of that user if 5 fail attempts.

这里的主要问题是允许系统同时检查用户名和电子邮件地址以进行身份​​验证,如果您使用电子邮件地址作为密码,则系统会锁定用户

The main problem here is allowing the system to check on both usernames and email addresses for authentication, the system in place does LOCK the user if you use the email address as it handles that in the mentioned check, but it will always fail because the authentication cannot handle it.

希望有人可以提供帮助,并提供一些提示。谢谢

Hope someone can help, offer ideas tips. Thanks

if ($this->request->is('post'))
        {       
            $opts = array(
               'conditions'=>array(
                   'OR'=>array(
                      'User.username'=>$this->data['User']['username'],
                      'User.email'=>$this->data['User']['username']
                    )
                )
            );

            $user = $this->User->find('first', $opts);

            if(!empty($user))
            {
                if($user['User']['status'] == 0)
                {
                    if($this->request->is('ajax'))
                    {
                        $this->autoRender = false;
                        echo json_encode(array('authenticated'=>false,'error'=>__('Sorry your account is currently locked. Please reset your password.?')));
                    }
                    else
                    {
                        $this->Session->setFlash(__('Sorry your account is currently locked. Please reset your password.'), 'default', array(), 'auth');
                    }
                }
                else
                {   
                    if ($this->Auth->login())
                    {   
                        if ($this->request->is('ajax'))
                        {   
                            $this->autoRender = false;
                            if(isset($this->params['url']['continue']))
                            {
                                $pathtoredirect = $this->UrlEncode->base64url_decode($this->params['url']['continue']);

                                echo json_encode(array('authenticated'=>true,'redirect'=>$pathtoredirect,'base'=>false));
                            }
                            else
                            {
                                $pathtoredirect = $this->Auth->redirect();

                                echo json_encode(array('authenticated'=>true,'redirect'=>$pathtoredirect,'base'=>true));
                            }
                        }
                        else
                        {
                            if(isset($this->params['url']['continue']))
                            {
                                $pathtoredirect = $this->UrlEncode->base64url_decode($this->params['url']['continue']);
                            }
                            else
                            {
                                $pathtoredirect = $this->Auth->redirect();
                            }
                            return $this->redirect($pathtoredirect);
                        }               
                    }
                    else
                    {               
                        if($this->Session->read('attempts'))
                        {
                            $attempts = $this->Session->read('attempts') + 1;
                        }
                        else
                        {
                            $attempts = 1;
                        }

                        $this->Session->write('attempts', $attempts);

                        if($attempts >= 5)
                        {
                            $this->User->id = $user['User']['id'];
                            $this->User->saveField('status', 0);
                            if ($this->request->is('ajax'))
                            {
                                $this->autoRender = false;
                                echo json_encode(array('authenticated'=>false,'error'=>__('Username or password is incorrect. For security reasons this account has now been locked and you must reset your password to unlock it.')));
                            }
                            else
                            {
                                $this->Session->setFlash(__('Username or password is incorrect. For security reasons this account has now been locked and you must reset your password to unlock it.'), 'default', array(), 'auth');
                            }
                        }
                        else
                        {
                            if ($this->request->is('ajax'))
                            {
                                $this->autoRender = false;
                                echo json_encode(array('authenticated'=>false,'error'=>__('Username or password is incorrect')));
                            }
                            else
                            {
                                $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
                            }
                        }
                    }
                }
            }
            else
            {
                if ($this->request->is('ajax'))
                {
                    $this->autoRender = false;
                    echo json_encode(array('authenticated'=>false,'error'=>__('Sorry that account does not exist.')));
                }
                else
                {
                    $this->Session->setFlash(__('Sorry that account does not exist.'), 'default', array(), 'auth');
                }
            }
        }


推荐答案

我不确定是否可以将AuthComponent配置为自动检查两个字段,但这是一种替代方法:

I'm not sure if the AuthComponent can be configured to check for two fields automatically, but here is an alternative:

/*
* AppController
*/
beforeFilter()
{
    $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'email', 'password' => 'password')));
}

/*
* UsersController
*/
function login()
{
  if($this->request->is('post'))
  {
    $logged_in = false;

    if($this->Auth->login())
    {
      $logged_in = true;
    }
    else
    {
      $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'username', 'password' => 'password')));
      $this->Auth->constructAuthenticate();

      $this->request->data['User']['username'] = $this->request->data['User']['email']; 

      if($this->Auth->login())
      {
          $logged_in = true;
      }
    }

    if($logged_in)
    {
      /*
       * Do what you want here
       */
    }
    else
    {
      /*
       * Do what you want here
       */
    }
  }
}

然后当然,如果您只想执行一项测试来检查两个字段,您可以将此代码移到Component中,而不用直接调用 $ this-> Auth-> login()方法。

Then of course if you want to be able to perform only one test to check for both fields, you could move this code into a Component instead of calling the $this->Auth->login() method directly.

这篇关于在CakePHP v2.0中使用电子邮件地址或用户名登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆