2010-09-29 41 views
0

我正在使用CakePHP,CAS進行身份驗證,並使用ACL進行授權。 如果用戶無權查看該頁面,我需要刷新一條消息,說明不允許或重定向到另一個頁面。例如:如果用戶正在查看/ users/view/1。現在用戶請求/用戶/刪除/ 1。用戶無權刪除。所以我想在他請求的頁面(/ users/view/1)上顯示一個flash消息。在PHP中的beforefilter()中取消請求

在我app_controller,我有以下功能:

function beforeFilter() { 
    $this->__initPhpCas(); 
    if (isset($_SESSION['loggedIn'])){ 
    if(!$this->Acl->check(.....){ 
     //User do not have permission to view the page. 
     // Need to cancel this request and flash a message 
    } 
    } 

任何建議都讚賞

最終的答案是

function beforeFilter() { 
     $this->__initPhpCas(); 
     if (isset($_SESSION['loggedIn'])){ 
     if(!$this->Acl->check(.....){ 
      //User do not have permission to view the page. 
      // Need to cancel this request and flash a message 
      $this->Session->setFlash(__('You are not authorized to view this page.', true)); 
     $this->redirect($_SERVER['HTTP_REFERER']); 
     } 
     } 

回答

0

redirect使用$this->redirect();add a message使用$this->Session->setFlash();。我已經包含鏈接來展示給你。

編輯:

我會建議設置的Flash消息,然後做重定向。然後在重定向頁面上,用$session->flash();顯示閃光消息。

EDIT2:

既然你不想做一個重定向,你需要做這樣的事情。

function view() { 
    if($this->Acl->check(.....){ 
     //display the page and continue with the view action 
    } 
    else { 
     $this->Session->setFlash("You do not have access to use this feature"); 
    } 
} 

編輯3:

Try this。看看鏈接中的最後一篇文章。

編輯4:嘗試使用deny()

編輯5:

如果我理解正確,你要使用beforeFilter檢查,如果他們有機會,如果沒有,那麼請不要繼續運行的動作。 CakePHP並不真的允許這個,但是解決方法是。

function beforeFilter() { 
    if($this->Acl->check(.....){ 
     //display the page and continue with the view action 
    } 
    else { 
     $this->Session->setFlash("You do not have access to use this feature"); 
     $this->params['action'] = "failedCheck"; 
    } 
} 

function failedCheck() { 
    //blah blah blah 
} 
+0

謝謝你的回覆。我在重定向時遇到的問題是,我想讓用戶停留在他請求的同一頁面上。例如,他向/ user/view請求/用戶/刪除。在我的beforefilter中,請求是/ user/delete。我怎麼能得到「/用戶/查看」網址 - 他從哪裏請求?所以dat我可以重定向他/用戶/查看 – metalhawk 2010-09-29 21:33:28

+0

我無法找出一種方法來取消它,所以我最終做的是檢查我的行動,如果他們被認證,如果是的話,我會繼續處理這一頁。如果沒有,那麼我不會顯示用戶請求的信息,而是顯示Flash消息。例如,請參閱答案 – Bot 2010-09-29 22:40:00

+0

感謝您的答覆。 「編輯2」的想法很有效,但是有很多代碼冗餘。我必須爲所有的方法執行這個檢查,所以我想如果我能以某種方式在beforefilter()中執行它,那麼它會更單一點和模塊化。 – metalhawk 2010-09-30 02:50:37