2013-07-11 31 views
2

登錄網站後出現問題。有兩種類型的用戶,即'管理員','僱主'。當我通過僱主登錄後,我可以訪問管理員的禁區。下面是該網站的AppController ..在cakePHP中限制和重定向其他用戶從管理員

class AppController extends Controller { 
     public $helpers = array('Form', 'Html', 'Js', 'Time', 'Auth'); 

     // Change template extension to .php instead of .ctp 
     var $ext = '.php'; 
     public $components = array(
      'Session', 
      'Auth' => array(
       'loginAction' => array(
        'controller' => 'users', 
        'action' => 'login' 
       ), 
       'loginRedirect' => array('controller' => 'users', 'action' => 'index'), 
       'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
       'authenticate' => array('Form' => array('fields' => array('username' => 'email'))), 
       'authorize' => array('Controller') 
      ) 
     ); 

     public function isAuthorized($user) { 

      // Admin can access every action 
      if (isset($user['type']) && $user['type'] === 'admin') { 
       return true; 
      } 

      // Default deny 
      return false; 
     } 

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

現在這裏是控制器有管理方法。

class TopicsController extends AppController { 

    public $scaffold = 'admin'; 
    public function beforeFilter() { 

     if($this->Auth->user('type')!='employer'){ 
      parent::beforeFilter(); 
      $this->Auth->allow(array('view', 'index','moveup')); 
     } else { 
      $this->Auth->deny(array('view', 'index','moveup')); 
      $this->redirect(array('controller' => 'employer' , 'action' => 'index')); 
     } 

    } 
    public function isAuthorized($user) { 
     return true; 
    } 

    public function index() { 
     $this->set('topics', $this->Topic->children()); 
    } 

} 

如果管理URL是www.example.com/admin/topics,僱主將被重定向到www.example.com/admin/employer這不是重定向正確的網址。

也想知道關於public $scaffold = 'admin';因爲這對我來說不太清楚。 請幫助我..

+1

有關'$支架=「admin'';閱讀:http://book.cakephp.org/2.0/en/controllers/scaffolding.html。你試過了:'$ this-> redirect(array('controller'=>'employer','action'=>'index','admin'=> false));' –

+0

謝謝Alex,我已經過了鏈接。我從來沒有嘗試過,並希望它的工作很好..非常感謝.. –

回答

3

確定..發現重定向,這使我的問題解決了現在..還在尋找,如果有人有正確答案的一種方式..

我從

改變代碼
$this->redirect(array('controller' => 'employer' , 'action' => 'index')); 

$this->redirect('employer'); 

.. 編輯:感謝亞歷克斯,我用

$this->redirect(array('controller' => 'employer' , 'action' => 'index', 'admin'=>false)); 

,它的工作太..

+1

如果你想重定向這是方式。您也可以將HTTP狀態代碼作爲第二個參數傳遞給此功能 - 因此所有3xx代碼均適用。 –

+0

感謝Borislav,會通過傳遞HTTP狀態碼來嘗試。 –

相關問題