2013-11-26 2 views
1

Я пытаюсь сделать простое приложение для входа с тортом 2.4. вот код для модели пользователя.cakephp auth-> login() всегда возвращает true

App::uses('AuthComponent', 'Controller/Component'); 
class User extends AppModel 
{ 
public $useTable = "user_master"; 
public $primaryKey = "user_id"; 
/* 
public $validate = array(
    'username' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'A username is required' 
      ) 
      ), 
    'password' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'A password is required' 
      ) 
      )); 
*/ 

public function beforeSave($options = array()) 
{ 
    if (isset($this->data[$this->alias]['password'])) 
    { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
} 

} 

В моей UsersController код ..

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->authorize = 'controller'; 
} 

public function isAuthorized() { 
    return true; 
} 
public function index() { 
    $this->User->recursive = 0; 
    $this->set('users', $this->paginate()); 
} 

public function view($id = null) { 
    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    $this->set('user', $this->User->read(null, $id)); 
} 

public function add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      return $this->redirect(array('action' => 'index')); 
     } 
     $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
    } 
} 

public function edit($id = null) { 
    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this->request->is('post') || $this->request->is('put')) { 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      return $this->redirect(array('action' => 'index')); 
     } 
     $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
    } else { 
     $this->request->data = $this->User->read(null, $id); 
     unset($this->request->data['User']['password']); 
    } 
} 

public function delete($id = null) { 
    $this->request->onlyAllow('post'); 

    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this->User->delete()) { 
     $this->Session->setFlash(__('User deleted')); 
     return $this->redirect(array('action' => 'index')); 
    } 
    $this->Session->setFlash(__('User was not deleted')); 
    return $this->redirect(array('action' => 'index')); 
} 



public function login() { 
if ($this->request->is('post')) { 
    //pr(AuthComponent::password($this->request->data[$this->alias]['password'])); 
    //debug($this->Auth->login()); die(); 
    if ($this->Auth->login()) { 
     $this->Session->setFlash(__('hi')); 
     return $this->redirect($this->Auth->redirect()); 
    } 
    $this->Session->setFlash(__('Invalid username or password, try again')); 
} 
} 

public function logout() { 
$this->Session->destroy(); 
return $this->redirect($this->Auth->logout()); 
} 

код AppController

App::uses('Controller', 'Controller'); 
App::uses('AuthComponent', 'Component'); 
class AppController extends Controller { 

public $components = array(
    'DebugKit.Toolbar', 
    'Session', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'products', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
     'loginError' => 'Invalid account specified', 
     'authorize' => array('Controller') 
    ) 
); 

public function beforeFilter() { 
    $this->Auth->allow('index', 'view'); 
} 

login.ctp

<div class="users form"> 
<?php echo $this->Session->flash('auth'); ?> 
<?php echo $this->Form->create('User'); ?> 
<fieldset> 
    <legend><?php echo __('Please enter your username and password'); ?></legend> 
    <?php echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
?> 
</fieldset> 
<?php echo $this->Form->end(__('Login')); ?> 
</div> 

Теперь проблема, когда я пытаюсь войти в систему с неправильные учетные данные, то также пути перенаправляются на страницу индекса контроллера продукта. Когда я раскомментировал строку в UserController debug ($ this-> Auth-> login()); умереть();

тогда он всегда отображается правдиво для всех пользователей и пароль. Можно ли мне помочь мне найти, где я делаю неправильно?

Заранее спасибо ..

ответ

0

Если предположить, что ваш create.ctp правильно ваш код выглядит близко к моему. Я бы предложил использовать шифрование blowfish.

if (isset($this->data['User']['password'])) { 
     $hash = Security::hash($this->data['User']['password'], 'blowfish');    
     $this->data['User']['password'] = $hash; 
    } 

попробовать Также установка параметры Auth в beforeFilter в вашем AppController вместо переменной $ компонентов. Если я правильно помню, я тоже не смог бы заставить меня работать.

public function beforeFilter() { 
     $this->Auth->authorize = array('Controller'); 
     $this->Auth->authenticate = array(
      'Blowfish' => array( 
       'userModel' => 'User', 
       'fields' => array('username' => 'username', 'password' => 'password') 
      ) 
     ); 
} 
3
public $components = array(
        'loginAction' => array('controller'=>'Dashboard', 'action'=>'login'), 
        'loginRedirect' => array('contoller'=>'Dashboard', 'action'=>'login'), 
        'logoutRedirect' => array('controller'=>'Dashboard', 'action'=>'logout'), 
        'authenticate' => array(
         'Form' => array(
          'userModel' => 'User', 
          'fields' => array(
           'username' => 'email', 
           'password'=>'password') 
         ) 
        ) 
       ) 

); 
+0

Спасибо, работает хорошо –

Смежные вопросы