Я новичок в Cakephp и пытаюсь создать простое приложение для регистрации и входа в систему. После написания кода проблема, с которой я столкнулся, заключается в том, что моя форма входа авторизует любой адрес электронной почты и пароль и перенаправляет на ту страницу, которую я установил после входа в систему. Вот мой код:
В AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller
{
var $components = array('Auth');
}
В UserController.php
<?php
//Filename -UsersController.php
//Registering new users and login
class UsersController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session',
'Auth' => array(
'authenticate' => array(
'Form' => array (
'fields' => array('username'=>'email','password'=>'password')
))));
/**
* Function for saving a new user
*/
public function index()
{
if($this->request->is('post'))
{
//print_r($this->request->data);die;
$this->User->create();
if($this->User->save($this->request->data))
{
$this->Session->setFlash(__('Congrats, you are registered successfully'));
return $this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Unable to store the user'));
}
}
/**
* Function for login functionality
*/
function beforeFilter()
{
$this->Auth->loginRedirect=array('controller'=>'pages','action'=>'welcome');
$this->Auth->logoutRedirect = array('controller'=>'pages','action'=>'welcome');
}
public function login()
{
if($this->request->is('post'))
{
if($this->Auth->login())
{
//print_r($this->request->data);die;
$this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
} }
В модели User.php
<?php
// Filename - User.php
// Model for registering new users
App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel
{
public $validate = array(
'username'=>array('required'=>array(
'rule'=>array('notEmpty'),
'message'=>'Username is required'
)),
'password'=>array('required'=>array(
'rule'=>array('notEmpty'),
'message'=>'Password is required'
)),
'email'=>array('required'=>array(
'rule'=>array('notEmpty'),
'message'=>'Email is required'
))
);
public function beforeSave($options = array())
{
if(isset($this->data['User']['password']))
{
$passwordHasher = new SimplePasswordHasher();
$this->data['User']['password']=
$passwordHasher->hash($this->data['User']['password']);
}
return true;
}
}
В login.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('username',array('style'=>'width:200px;'));
echo $this->Form->input('password',array('style'=>'width:200px;'));
echo $this->Form->end('Sign in');
?>
Пожалуйста, помогите мне решить эту проблему. Заранее спасибо.