Ronny vdb
Ronny vdb

Reputation: 2464

Cakephp 2 ACL ERR_TOO_MANY_REDIRECTS

I am trying to implement the ACL tutorial found here: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

I followed all the instructions, however when I try to go to [my_site]/users/add ERR_TOO_MANY_REDIRECTS error.

I found this on the Cakephp website:

This happens when for example we have an element that receives data from a method of a controller, and the implementation of this method requires conducting the login would create an infinite loop that eventually will cause the browser to decline redirects

And they suggest this as the fix:

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

Which doesn't seem to work.

If I change the AppController from this:

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

to:

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

I dont get the error anymore but get redirected to [my_site]/users/login

Any suggestions as to what I am doing wrong that I can't view the User-Add page? TIA!

UserController:

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

Login function (UsersController):

Public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

Auth Component Loader:

public $components = array(
        'Session',
        'RequestHandler',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'projects', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
        )
    );

Upvotes: 2

Views: 4436

Answers (3)

Ronny vdb
Ronny vdb

Reputation: 2464

I finally managed to solve the problem with help Brian:

Do you have an requestAction() code? If so, try adding this in

AppController::beforeFilter() 

if (isset($this->params['requested'])) $this->Auth->allow($this->action); 

Upvotes: 1

AKKAweb
AKKAweb

Reputation: 3807

Please change your beforeFilter()'s Auth->allow('_CONTROLLER_NAME') to:

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

Hope that works for you!

Upvotes: 0

Oldskool
Oldskool

Reputation: 34837

The error you are getting has nothing to do with ACL, but with the Auth component denying access to your UsersController add and login functions, to which it is trying to redirect the user. Make sure the add and login functions are public, by adding this line in your UsersController (rather than your AppController):

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

The loop you are now encountering is because the add and login functions are not public and therefor the loop looks like: add -> Unauthorized -> login -> Unauthorized -> login ... and so on.

Upvotes: 2

Related Questions