2014-10-29 45 views
0

我正在學習cakePHP,我已經寫了手冊的例子,問題是用UsersController註銷的方法,當我按下鏈接註銷時應用程序被重定向到登錄表單,但瀏覽器的後退按鈕允許返回到需要身份驗證的用戶,例如關於此頁面時與頁面添加的帖子Cakephp在註銷後不會過期

源代碼

UsersController.php

<?php 

class UsersController extends AppController { 

    public function beforeFilter() { 
     parent::beforeFilter(); 
// Allow users to register and logout. 
     $this->Auth->allow('add', 'logout'); 
    } 


    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() { 
     //$this->layout=null; 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       $this->Session->write('userid',$this->Auth->user('id')); 
       //$this->Session->write('userid',AuthComponent::user('id')); 
       return $this->redirect($this->Auth->redirect()); 
      } 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 

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

} 

?> 

PostsController.php

<?php 

class PostsController extends AppController { 

    public $helpers = array('Html', 'Form'); 

    public function isAuthorized($user) { 
// All registered users can add posts 
     if ($this->action === 'add') { 
      return true; 
     } 
// The owner of a post can edit and delete it 
     if (in_array($this->action, array('edit', 'delete'))) { 
      $postId = (int) $this->request->params['pass'][0]; 
      if ($this->Post->isOwnedBy($postId, $user['id'])) { 
       return true; 
      } 
     } 
     return parent::isAuthorized($user); 
    } 

    public function index() { 
     if ($this->Session->read('userid')) { 
      $this->set('posts', $this->Post->find('all', array('conditions' => array('Post.user_id' => AuthComponent::user('id'))))); 
     } else { 
      $this->set('posts', $this->Post->find('all')); 
     } 
    } 

    public function view($id = null) { 
     if (!$id) { 
      throw new NotFoundException(__('Invalid post')); 
     } 
     $post = $this->Post->findById($id); 
     if (!$post) { 
      throw new NotFoundException(__('Invalid post')); 
     } 
     $this->set('post', $post); 
    } 

    public function add() { 
    if ($this->Auth->loggedIn()) { 
     if ($this->request->is('post')) { 
      $this->request->data['Post']['user_id'] = $this->Auth->user('id'); 
      $this->Post->create(); 
      if ($this->Post->save($this->request->data)) { 
       $this->Session->setFlash(__('Your post has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(__('Unable to add your post.')); 
     } 
    } else { 
     return $this->redirect(array('controller' => 'users', 'action' => 'login')); 
    } 
} 

    public function edit($id = null) { 
     if (!$id) { 
      throw new NotFoundException(__('Invalid post')); 
     } 
     $post = $this->Post->findById($id); 
     if (!$post) { 
      throw new NotFoundException(__('Invalid post')); 
     } 
     if ($this->request->is(array('post', 'put'))) { 
      $this->Post->id = $id; 
      if ($this->Post->save($this->request->data)) { 
       $this->Session->setFlash(__('Your post has been updated.')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(__('Unable to update your post.')); 
     } 
     if (!$this->request->data) { 
      $this->request->data = $post; 
     } 
    } 

    public function delete($id) { 
     if ($this->request->is('get')) { 
      throw new MethodNotAllowedException(); 
     } 
     if ($this->Post->delete($id)) { 
      $this->Session->setFlash(
        __('The post with id: %s has been deleted.', h($id)) 
      ); 
      return $this->redirect(array('action' => 'index')); 
     } 
    } 

} 

?> 

AppController.php

<?php 
App::uses('Controller', 'Controller'); 

/** 
* Application Controller 
* 
* Add your application-wide methods in the class below, your controllers 
* will inherit them. 
* 
* @package  app.Controller 
* @link  http://book.cakephp.org/2.0/en/controllers.html#the-app-controller 
*/ 
class AppController extends Controller { 

    public $components = array(
     'Session', 
     'Auth' => array(
      'loginRedirect' => array('controller' => 'posts', 'action' => 'index'), 
      'logoutRedirect' => array('controller' => 'users','action' => 'login'), 
      'authorize' => array('Controller') // Added this line 
     ) 
    ); 

    public function isAuthorized($user) { 
// Admin can access every action 
     if (isset($user['role']) && $user['role'] === 'admin') { 
      return true; 
     } 
// Default deny 
     return false; 
    } 

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

} 

?> 

回答

0

請從您的AppController

檢查beforeFilter功能已明確允許一些行動,通過AuthComponent

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

請驗證您希望允許未經身份驗證的訪問者執行的操作。

由於AppController由cakephp中的每個控制器擴展。其中最讓你喜歡的就是允許未經身份驗證的用戶訪問您創建或將創建的每個控制器的索引,查看,登錄等操作。

+0

是的,你是對的,但問題是瀏覽器的後退按鈕允許返回到需要認證的操作。例如,這發生在需要驗證的動作Add的PostsController。 – 2014-10-30 14:56:28