So I wanted txpmanual.com to remember administrators who’ve logged in (if they so choose). Apparently cake isn’t so friendly with creating your own custom cookies. Luckily, Miguel Ros built a component that start to give one the ability to more easily control as many cookies as you please.
Using Miguel’s component, I was able to get a new cookie created that stored a special hash so I could auto-login administrative users when they show up at the site.
Unfortunately, this meant I needed to test for such in the beforeFilter for the controller. I tried creating the below beforeFilter in the app_controller.php file that I put in /app.
function beforeFilter()
{
if(!$this->Session->check('User') && $this->cookie->check('login'))
{
$cookie_arr = $this->cookie->read('login');
$someone = $this->query("SELECT * FROM test_users WHERE email='$cookie_arr[0]'");
// At this point, $someone is full of user data, or its empty.
// Let's compare the form-submitted password with the one in
// the database.
if($someone['User']['password'] == $cookie_arr[1])
{
$this->Session->write('User', $someone['User']);
}
}
}
But then I started to get errors on every action, as this runs before every action, I knew it was causing a problem. I finally realized that the controllers having trouble running this filter were the ones that did not have ‘Users’ explicitly stated in the uses array. So I tried putting that in the app_controller.php file in /app.
var $uses = array('User');
No good. Apparently, this array doesn’t just add more models to the controllers, it completely overrides the models available.
So, when the controller of the action running tries to pull it’s data, unless I had overridden the uses array in the child controller as well, it didn’t work at all.
So I ended up putting something like the below in the beforeFilter of every controller and then also specifying uses ‘User’ in every controller.
if(!$this->Session->check('User') && $this->cookie->check('login'))
{
$cookie_arr = $this->cookie->read('login');
$someone = $this->User->findByEmail($cookie_arr[0]);
// At this point, $someone is full of user data, or its empty.
// Let's compare the form-submitted password with the one in
// the database.
if($someone['User']['password'] == $cookie_arr[1])
{
$this->Session->write('User', $someone['User']);
}
}
Is there a way to refactor this code? I sure hope so.
Sphere: Related Content
Actually, var $uses in the appcontroller doesn’t overwrite all controllers $uses variable, it add to them. I add the Aro and Aco models in my AppController, and it’s inherited by all child controllers.This is true from at least v1.1.6.
Ben Dec 1, 04:00 PM #That’s weird. I’m in 1.1.10 and essentially, it overwrote it all. I was going to try doing just $uses[]= today, in the app_controller.php file and see how that goes.
When I did $uses = array(‘User’), the model of the controller I was in would no longer be available in that controller.
Walker Hamilton Dec 1, 07:13 PM #Okay, so I tried $uses[] = and that definitely didn’t work. It’s a new instance of the $uses variable.
But when I put $uses=array(‘ModelName’); the model for the controller I was in would get overwritten. But, I guess just specifying $uses in each controller is a little better than needing to have a crazy beforeFilter() in each controller.
Walker Hamilton Dec 1, 07:35 PM #Weird. I’ve just upgraded my project to Cake 1.1.10 and it still works here. In my AppController:
var $uses = array(‘Aro’, ‘Aco’);
in my CommentsController:
var $uses = array(‘Post’, ‘Comment’);
and in the CommentsController, all of the above classes work from $this->whatever quite happily.
Ben Dec 1, 11:55 PM #You can likely put $this->uses[] = ‘User’; in your AppController::beforeFilter(). That will add the Model to the user array of every Controller that inherits from AppController. That is if the Controller doesn’t override the beforeFilter(). If it does you’ll need to call parent::beforeFilter() in the child::beforeFilter();
Kinda late, hope it still helps someone.
Pilgrim Aug 26, 11:38 AM #