Войти с адресом электронной почты или именем пользователя в CakePHP v2.0

В CakePHP до 2.0 вы можете позволить пользователю войти в систему, используя свой адрес электронной почты, остановив autoRedirect, а затем сравните данные имени пользователя с столбцом электронной почты в своей базе данных (видимо, Cake мог бы вернуться к проверке имени пользователя, если не по электронной почте).

В CakePHP 2.0 это изменилось, и вы $this->Auth->login() систему вручную, используя $this->Auth->login()

Мой вопрос в том, как я могу заставить это работать для 2.0? У меня есть довольно сложный код, который выполняет множество функций, таких как обработка запросов ajax и postback, блокировка учетных записей, если пользователь пытается войти в систему слишком много раз и т. Д., Поэтому это довольно долго!

Как вы увидите, я проверяю, действительно ли учетная запись существует вручную, поэтому я могу показать сообщение об учетной записи, которое не было обнаружено, прежде чем проходить процесс аутентификации в случае, а также использовать это, чтобы заблокировать учетную запись этого пользователя, если 5 попыток сбоя.

Основная проблема здесь заключается в том, что система позволяет проверять как имена пользователей, так и адреса электронной почты для аутентификации, система на месте делает LOCK пользователем, если вы используете адрес электронной почты, поскольку она обрабатывает это в указанной проверке, но она всегда будет терпеть неудачу, поскольку аутентификация не может справиться с этим.

Надеюсь, кто-то может помочь, предложите советы по идее. благодаря

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