0

これが私のプロジェクトです:

ユーザーまたは管理者が使用できるWebサイトがあります。index、viewなどのメソッドを作成しました...これらのビューは、ユーザーと管理者がアクセスできる必要があります。admin_index、admin_editなどのメソッドもあります。管理者が表示する必要があります。

今のところ、すべてのユーザーページは公開されており、管理ページはログインによって保護されています。ユーザーページのログインもお願いしたいのですが、公開ページは/ users/loginのみです。

AuthComponentを使用してこれを行うにはどうすればよいですか?

私がそれをタイプするとき:

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

リダイレクトループがあります。

ご協力ありがとうございました


私はまだ問題を抱えています。これが私のコードです。

AppController

class AppController extends Controller {
    public $helpers = array('Text', 'Form', 'Html', 'Session', 'Cache');

    public $components = array(
        'Session', 
        'Auth' => array(
            'loginAction'   => array('controller' => 'users', 'action' => 'login', 'admin' => false),
            'loginRedirect' => array('controller' => 'index', 'action' => 'index', 'admin' => true),
            'logoutRedirect'=> array('controller' => 'users', 'action' => 'login', 'admin' => false),
        )
    );


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

そして私のUsersController

class UsersController extends AppController {

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

    public function login(){

        if(isset($_SESSION['Auth']['User'])){
            return $this->redirect($this->Auth->redirect());
        }

        if($this->request->is('post')){
            if($this->Auth->login()){
                return $this->redirect($this->Auth->redirect());
            } else{
                $this->Session->setFlash('Identifiants incorrects', 'notif');
            }
        }
    }

    public function logout() {
        $this->Session->setFlash('Vous êtes déconnecté', 'notif', array('type' => 'success'));
        $this->redirect($this->Auth->logout());
    }
}

まだリダイレクトループがあります。理由がわかりません。

4

2 に答える 2

0

このコードにも問題があります。

class AppController extends Controller {
public $helpers = array('Text', 'Form', 'Html', 'Session', 'Cache');

    public $components = array(
        'Session', 
        'Auth' => array(
            'loginAction'   => array('controller' => 'users', 'action' => 'login', 'admin' => false),
            // Change controller = index to controller = users
            //'loginRedirect' => array('controller' => 'index', 'action' => 'index', 'admin' => true),
            'loginRedirect' => array('controller' => 'users', 'action' => 'index', 'admin' => true),
            'logoutRedirect'=> array('controller' => 'users', 'action' => 'login', 'admin' => false),
        )
    );


    public function beforeFilter() {
        $this->Auth->allow('login');
    }
}
于 2012-05-16T14:43:16.047 に答える
0

最善の方法は ACL を使用することですが、それでも AuthComponent を使用したい場合は、そのまま beforeFilter() を使用することをお勧めしますが、さらに、ここにログイン済みのチェックを追加します。

public function beforeFilter(){
    parent::beforeFilter();

    if($this->Auth->user()){
        $this->Auth->allow('*');
    }else{
        $this->Auth->allow('login');
    }
}

AuthComponent だけを使用してさらに先に進みたい場合は、次のようなチェックをさらに設定できます。

    // group_id = 2 is for users who are paid subscribers for example
    // and group_id = 1 is for administrators
    if($this->Auth->user('group_id') == 1){
        $this->Auth->allow('*');
    }elseif($this->Auth->user('group_id') == 2 ){
        $this->Auth->allow('paidcontent','video','photo','etc');
    }elseif($this->Auth->user()){
        $this->Auth->allow('index','view');
    }else{
        $this->Auth->allow('login');
    }

上記のコードはテストされていません。さらにサポートが必要な場合はお知らせください

于 2012-05-16T02:19:13.340 に答える