cakephp登录页面不会去任何地方

时间:2012-02-13 19:37:19

标签: cakephp authentication redirect cakephp-2.0

我尝试设置登录页面,但是当我尝试登录时,即使使用错误的用户名/密码,蛋糕也会重定向到登录页面(注销功能会正确重定向)。即使我插入错误的信息,我也没有任何错误闪烁,我没有得到它。这是我的控制器代码:

class UsersController extends AppController {
public $name='Users';
public $layout='pagelayout';
public $uses=array('User');



public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout', 'overview');
}

public function login() {
$this->set('title', 'Log in to your Gulf Shores 4 Less account');
if ($this->request->is('post')) {
    if ($this->Auth->login()) {
        return $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
    }
}

}

这是我的模特:

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name='User';
public $hasMany=array('Unit', 'Complex', 'Coupon', 'Location', 'Image', 'Charter', 'Course', 'Nightclub', 'Store');

public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
    $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
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'
        )
    ),
    'role' => array(
        'valid' => array(
            'rule' => array('inList', array('admin', 'advertiser')),
            'message' => 'Please enter a valid role',
            'allowEmpty' => false
        )
    )
  );
}

?>

以下是来自AppController的代码:

<?php
class AppController extends Controller {
 public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'overview'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'index')
    )
);


function beforeFilter() {
    $this->Auth->allow('login','index', 'view', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us');
}
}
?>

这是视图代码:

<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>

正如您所看到的,我几乎复制并粘贴了Cakephp-2.0手册中的内容。我的db表和手册之间的唯一区别是我的密码在我的users表中存储为MD5哈希。我无法弄清楚它出轨的地方。

2 个答案:

答案 0 :(得分:1)

确保使用Auth组件哈希存储密码。 AFAIK,不支持'普通'md5密码。 Cake生成的哈希值比md5更复杂。

有关如何散列密码的信息,请参阅documentation。如果您要从使用md5哈希的应用迁移,则必须将所有密码重置为随机的所有用户。

答案 1 :(得分:0)

您可以尝试在AppController.php文件中使用以下代码

<强> AppController.php

$this->Auth->authenticate = array(
            AuthComponent::ALL => array('userModel' => 'User'),
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        );
        $this->Auth->authorize =false;
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->loginRedirect = array('controller' => 'media', 'action' => 'index/');
        $this->Auth->logoutRedirect = '/logout';
        $this->Auth->loginError = 'Invalid e-mail / password combination. Please try again';
相关问题