2011-09-14 8 views
0

私はフロントエンドと管理者がいます。ユーザーがサインインしてから管理者のURLに移動しようとすると、インデックスページにリダイレクトされます。管理者としてログインするようにメッセージを表示して管理者ログインページにリダイレクトしたいと思います。cakephpログインリダイレクト

管理者がユーザーとしてログインし、管理者エリアにログインしようとする場合があります。私は管理者のログインにRediectすることができず、ログアウトして管理者としてログインするオプションを与えました。

function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allowedActions = array('admin_login','admin_logout'); 
    //$this->Auth->allowedActions = array('*'); 
    $this->set('select_nav', array('admin','users')); 

} 


function admin_login() { 
    // $this->layout = 'admin'; // nothing required 
    $this->layout = 'blank'; // nothing required 
} 

答えて

3

users_controller

app_controller

function beforeFilter() { 

    $this->Auth->loginError = "Wrong credentials"; 
    $this->Auth->authError = "This part of the website is protected."; 

    //Configure AuthComponent 
    $this->Auth->allow('display'); 
    $this->Auth->authorize = 'actions'; 
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); 
    //$this->Auth->autoRedirect = false; 
    //$this->Auth->loginRedirect = array('controller' => 'reservatins', 'action' => 'index'); 


} // end before filter 

私は私のプロジェクトの1にそれを行っています。ユーザーはログインしています(匿名ユーザー、管理者として)。彼がどこに来るか、彼が現在持っている権限に応じて、別のログインエラーが表示されます。 ...これは私がやったことであることを行うには

...

まず、あなたは法認可「コントローラ」を使用する必要があります。今から

$this->Auth->authorize = 'controller'; 

を、すべてのあなたの行動を通過します現在のコントローラのisAuthorizedメソッドを使用します。私は私のAppControllerを介してユーザの権限をチェックして、ここでやっている何

public function isAuthorized() 
{ 
    if (!$this->__permitted($this->name, $this->action)) 
    { 
     $this->cakeError('error403'); 
     return false; 
    } 
    return true; 
} 

__permitted:私は私のデータベースやグループごとに私のユーザー、グループおよび権限が異なる権限を持ってきたように、私は私のapp_controllerにisAuthorizedメソッドを作成しましたメソッド(単にセッションのパーミッションをチェックし、セッションに保存されていない場合はDBでチェックしてセッションに保存します)。

ユーザーに権限がない場合、私は彼にエラー403を表示します。ここには面白い部分があります。

AppErrorにerror403というメソッドを追加します。ここでは、ユーザーをリダイレクトする場所と、どのようなメッセージを表示するかを制御できます。ここで

は、私が使用したコードは、(明らかにあなたがあなたのニーズに応じて、コードの独自の作品を作成する必要があります)です。

public function error403() 
{ 
    // Extract params 
    extract($this->controller->params, EXTR_OVERWRITE); 

    // Store url to be redirected on success 
    if (!isset($url)) 
    { 
     $url = $this->controller->here; 
    } 
    if (isset($url['url'])) 
    { 
     $url = $url['url']; 
    } 
    $url = Router::normalize($url); 

    // The page is trying to access is an admin page? 
    $is_admin_page = isset($this->controller->params['admin']) && $this->controller->params['admin'] == true ? true : false; 

    if (!empty($url) && count($url) >= 2) 
    { 
     $query = $url; 
     unset($query['url'], $query['ext']); 
     $url .= Router::queryString($query, array()); 
    } 
    // 403 header 
    $this->controller->header("HTTP/1.0 403 Forbidden"); 

    // If my method is NOT an upload 
    if (!preg_match('/upload/', $url)) 
    { 
     // Write referer to session, so we can use it later 
     $this->controller->Session->write('Auth.redirect', $url); 
    } 
    else exit; // else exit, so we prevent 302 header from redirect 

    // NOTE: we can't use $this->controller->Auth->loginAction because there's no controller loaded 
    $loginAction = array('controller' => 'users', 'action' => 'login'); 

    // If is ajax... 
    if (isset($this->controller->params['isAjax']) && $this->controller->params['isAjax'] == true) 
    { 
     $this->controller->layout = 'ajax'; 

     $message = __("No tens permisos per fer aquesta acció", true); 
     // If user is anonymous.. 
     if ($this->controller->ISession->isAnonymous()) 
     { 
      // AJAX Error Message 
      $message = __('La teva sessió no està iniciada.', true) 
       . ' <a href="'.Router::url($loginAction).'">' 
       . __('Fes clic aquí per iniciar-la', true) . '</a>'; 
     } 

     $this->controller->set(compact('message')); 
     $this->controller->render('error403'); 

     $this->controller->afterFilter(); 
     echo $this->controller->output; 
    } 
    else 
    { 
     $message = __("No tens permisos per fer aquesta acció", true); 
     $redirect = $this->controller->referer(); 

     // If is anonymous... 
     if ($this->controller->ISession->isAnonymous()) 
     { 
      $message = __('La teva sessió no està iniciada.', true); 
      $redirect = $loginAction; 
     } 
     // If user can't access the requested page, we redirect him to login 
     if (!$this->controller->ISession->userCan($redirect)) 
     { 
      $redirect = $loginAction; 
     } 

     // Show different auth messages for admin and user pages 
     $this->controller->Session->setFlash($message, $is_admin_page ? 'default' : 'gritter', array(), 'auth'); 
     $this->controller->redirect($redirect, null, true); 
    } 
} 

が、これは私の場合のためのコードであることを覚えておいてください。必要に応じて独自のerror403ページを作成する必要があります。もちろん、あなたは私の方法でそれを得ることができます:)

+0

ありがとう。私は本当にその返事に感謝します。すべて意味があるので、私はそれをコード化し始めるつもりです。 –

関連する問題