0

私はcakePHPにかなり慣れておらず、cakeサイトのすべてのチュートリアルを読んだことがあります。ケーキ2.1を使用して小さなサンプルアプリを作成していますが、問題が発生しました。基本的に、管理者ユーザーは、ログイン時に通常のユーザーがリダイレクトされるページとは異なるページにリダイレクトされるようにします。これを行う簡単な方法があると確信していますが、私は苦労しています!

管理ルーティングを有効にして、認証コンポーネント(およびACLコンポーネントを使用していますが、問題には影響していません)を使用しています。私は2つのログインを持っています。1つはadmin用です-admin_login()と通常のユーザー用のログイン-login()-通常の非管理者ログインは正常に機能します。

私のAppController.phpにはこれがあります:

class AppController extends Controller {

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);
public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->allow('display'); // Allows access to the homepage of the app
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}

}

ご覧のとおり、デフォルトでは、ユーザーはログイン時にpostsコントローラーのadd()関数にリダイレクトされます。これは正常に機能します。ただし、admin_login()関数に別のログインリダイレクトを設定するにはどうすればよいですか?

私はここで多くの投稿を読みましたが、ほとんどはケーキ2ではなく古いバージョンのケーキに関連しています。

それが役に立ったら、これはUsersController.phpの私のadmin_login関数です

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

public function admin_login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Session->read('Auth.User')) {
                $this->Session->setFlash('You are logged in!');
                //$this->redirect($this->Auth->redirect());

                // This doesnt work! Grrrrrr
                //$this->redirect(array('controller'=>'pages', 'action'=>'admin_index'));

                // This nearly works...
                if($this->Auth->user())$this->redirect(array('controller' => 'pages', 'action' => 'admin_index'));
            }            
        }
        else {
            $this->Session->setFlash('Your username or password was incorrect.');
        }
    }

誰かが私を正しい方向に向けることができますか?

4

2 に答える 2

2

データベースにロール->「admin、user」がある場合は、AppControllerに入れます-beforeFilter():

if($this->Auth->user('role') == 'admin'){
     $this->Auth->loginRedirect = array('controller' => 'controller1', 'action' => 'action1');
}else{
     $this->Auth->loginRedirect = array('controller' => 'controller2', 'action' => 'action2');
}

$ this-> Auth-> user()の内容を知りたい場合は、次のように入力してください。

debug($this->Auth->user());
于 2012-07-24T10:23:49.310 に答える
0

多分これはうまくいく:

if($this->request->params['admin']) {
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => true);
}
else {
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}

beforeFilter()のloginRedirect設定をこれらの行に置き換えます

于 2012-07-24T09:08:02.980 に答える