0

我从 cakephp 开始,我正在尝试构建一个登录系统。问题是服务器响应我的登录请求重新加载登录页面,没有任何消息,并且登录没有完成,因为它不允许我进入受保护的页面。

这是我的用户表:

mysql> show columns from users;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| email    | varchar(255)     | YES  |     | NULL    |                |
| password | varchar(255)     | YES  |     | NULL    |                |
| role     | varchar(20)      | YES  |     | user    |                |
| created  | datetime         | YES  |     | NULL    |                |
| updated  | datetime         | YES  |     | NULL    |                |
| username | varchar(255)     | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

这是我的用户模型

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel{
    var $name = "User";    
    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;
    }
}
?>

这是我的应用控制器

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

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

        $this->Auth->authorize = 'actions';
        $this->Auth->loginError = "This message shows up when the wrong credentials are used";
        $this->Auth->authError = "This error shows up with the user tries to access a part of the website that is protected.";

    }
}

这是我的用户控制器

<?php
class UsersController extends AppController{

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

    public function index(){
        ...
    }

    public function view($id = null){
        ...
    }

    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){
        ...
    }

    public function delete($id){
        ...
    }

    public function login(){
    }

    public function logout(){
        $this->redirect($this->Auth->logout());
    }
}
?>
4

0 回答 0