0
votes

I am following the CakePHP ACL tutorial http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html

I have a controller called ImagesController that basically lets the user list all the images that have been uploaded. There are also actions such as "upload" and "delete" images.

I also have a UserController that has login and logout function.

My AppController.php looks like this

  1 <?php
  2 
  3 class AppController extends Controller {
  4     public $components = array(
  5             'Acl',
  6             'Auth' => array(
  7                 'authorize' => array(
  8                     'Actions' => array('actionPath' => 'controllers')
  9                     )
 10                 ),
 11             'Session'
 12             );
 13     public $helpers = array('Html', 'Form', 'Session');
 14 
 15     public function beforeFilter() {
 16  //       $this->Auth->actionPath = 'controllers/';
 17         //Configure AuthComponent
 18         $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
 19         $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
 20         $this->Auth->loginRedirect = array('controller' => 'images', 'action' => 'index');
 21         $this->Auth->allow('display');
 22  //       $this->Auth->allow('*');
 23     }
 24 }
 25 
 26 
 27 ?>
~             

My UsersController.php looks like this

<?php
  2 App::uses('AppController', 'Controller');
  3 /**
  4  * Users Controller
  5  *
  6  * @property User $User
  7  */
  8 class UsersController extends AppController {
 30     public function beforeFilter() {
 31         parent::beforeFilter();
 32         //$this->Auth->allow("initDB"); // remove this later
 33         $this->Auth->allow('login', 'logout');
 34     }
 35     public function login() {
 36         if ($this->Session->read('Auth.User')) {
 37             $this->Session->setFlash('You are logged in!');
 38             $this->redirect('/', null, false);
 39         }
 40     }
 41     public function logout() {
 42         //Leave empty for now.
 43         $this->Session->setFlash('Good-Bye');
 44         $this->redirect($this->Auth->logout());
 45     }
}

So now when I click on an action in images/index such as upload or delete, it sends me to user/login page but when I try to log in nothing happens, even though I have my redirect pointed to images/index in line 20. What gives?

1
Any beforeFilter() method in your UsersController that forgets to call parent::beforeFilter() maybe ? - nIcO
No I have parent::beforeFilter(); in my beforeFilter() UsersController. What else can it be? - CodeCrack
Updated with UsersController.php code - CodeCrack

1 Answers

1
votes

Unlike Cake 1.3, in Cake 2, the login is not done automatically. This means you have to call $this->Auth->login() explicitely.

Have a look at the login() action in the tutorial. So far, you never perform the login, and that explains why you are not redirected.