milesj / utility

[Deprecated] A CakePHP plugin for common utility classes.
MIT License
69 stars 24 forks source link

_autoLogin - sample request #3

Closed elogicmedia closed 12 years ago

elogicmedia commented 12 years ago

I was wondering how do I work with the _autoLogin function that is available in the AuthLogin Component?

See below, how do I go from $user to getting the ID out and using it for the last_login field I have setup.

Thanks

I have the following in my AppController.php file. it doesn't seem to do anything at the moment. I also tried it in my User.php model and userControoler.php files but nothing seems to work.

public function _autoLogin($user) 
{
    $this->id = $user['User']['id']; // target correct record
           $this->User->saveField('last_login', date(DATE_ATOM)); // save login time
}   

I went though the component and tested the following:

get_class_methods($controller) = get_class_methods('users') = Produced NULL

While

get_class_methods($model) = get_class_methods('User') = Produced an array of the functions in my User.php Model.

So I am wondering does the _autoLogin function go into the User.php model and does the component need to be changed from get_class_methods($controller) to get_class_methods($model) ?

I did try this and it didn't seem to work still but I am having issue testing this with the cookie time delays etc.

Thanks

milesj commented 12 years ago

It goes in the controller and is used to initialize any other session related functionality.

I have this in my current app and it works just fine. Have you tried placing any debugs or logging?

elogicmedia commented 12 years ago

OK thanks, I have put it back in my controller, is there any way to speed up the testing without having to wait for tomorrow when the cookie kicks in again?

How would you suggest I debug this?

Thanks again.

UsersController.php

public function _autoLogin($user) 
{
    $this->id = $user['User']['id']; // target correct record
       $this->User->saveField('last_login', date(DATE_ATOM)); // save login time
}   
milesj commented 12 years ago

You could always try closing the browsers and re-opening to see if that triggers it.

elogicmedia commented 12 years ago

That's what I would have thought would work but it isn't so I am still thinking the function isn't being called. This is my code within my UsersController.php now, I didn't get any error or debugging message when I open the site.

public function _autoLogin($user)
{
    debug($user);
    $this->User->id = $user['User']['id']; // target correct record
    $this->User->saveField('last_login', date(DATE_ATOM)); // save

login time }

milesj commented 12 years ago

Can you setup AutoLogin debugging and see what steps it is hitting?

http://milesj.me/code/cakephp/auto-login#live-debugging

You only need to define the ips and not the email. Everything will be sent to your logs.

elogicmedia commented 12 years ago

Thanks I have re-set that up and I got the email from the login. Should it say somewhere in the email about the _autoLogin function?

Here is the top part:

CookieComponent Object ( [name] => CakeCookie [time] => [path] => / [domain] => [secure] => 1 [key] => DYhG93b0qyJffdgsdgdf5454bWwvni R2G0FgaC9mi [httpOnly] => [_values:protected] => Array ( [CakeCookie] => Array ( [rememberMe] => Array ( [username] => YXN5bW9uZHNAZWxvZ2ljbWVkaWEuY29tLmF1 [password] => U2x1Z2dlcjk5 [hash] => 09b86675bf82db2a9917af96bd2c51ab60a6c912 [time] => 1351279556 )

            )

    )

[_type:protected] => rijndael
milesj commented 12 years ago

It looks like it's getting that far: https://github.com/milesj/cake-utility/blob/master/Controller/Component/AutoLoginComponent.php#L187

The next line is the _autoLogin trigger. I would suggest editing the AutoLogin file and trying the following:

And if that doesn't work, you can write the call directly:

$controller->_autoLogin($this->Auth->user());

elogicmedia commented 12 years ago

Thanks,

debug(get_class_methods($controller))

produced the following, _authLogin isn't in the list but neither is any of the UsersController functions...?

Thanks

array( (int) 0 => 'index', (int) 1 => 'beforeFilter', (int) 2 => 'beforeRender', (int) 3 => 'forceSSL', (int) 4 => 'construct', (int) 5 => 'isset', (int) 6 => 'get', (int) 7 => 'set', (int) 8 => 'setRequest', (int) 9 => 'invokeAction', (int) 10 => 'implementedEvents', (int) 11 => 'constructClasses', (int) 12 => 'getEventManager', (int) 13 => 'startupProcess', (int) 14 => 'shutdownProcess', (int) 15 => 'httpCodes', (int) 16 => 'loadModel', (int) 17 => 'redirect', (int) 18 => 'header', (int) 19 => 'set', (int) 20 => 'setAction', (int) 21 => 'validate', (int) 22 => 'validateErrors', (int) 23 => 'render', (int) 24 => 'referer', (int) 25 => 'disableCache', (int) 26 => 'flash', (int) 27 => 'postConditions', (int) 28 => 'paginate', (int) 29 => 'beforeRedirect', (int) 30 => 'afterFilter', (int) 31 => 'beforeScaffold', (int) 32 => 'afterScaffoldSave', (int) 33 => 'afterScaffoldSaveError', (int) 34 => 'scaffoldError', (int) 35 => 'toString', (int) 36 => 'requestAction', (int) 37 => 'dispatchMethod', (int) 38 => '_stop', (int) 39 => 'log', (int) 40 => '_set', (int) 41 => '_mergeVars' )

elogicmedia commented 12 years ago

Which function in AutoLogin do you suggest I put the following:

$controller->_autoLogin($this->Auth->user());

I put it within the startup function and I got this error: Fatal error: Call to undefined method DashboardController::_autoLogin()

Note: DashboardController is the homepage of the system, shouldn't it be trying to call users as per the variable assigned?

Thanks

milesj commented 12 years ago

Which controller is being passed each time? I wonder if it's using the same one or different depending on the page you go to.

Is the _autoLogin code in AppController?

elogicmedia commented 12 years ago

_autoLogin was in UsersController. I have moved it to AppController, modified my function a little and all is working.

Maybe the documentation could be updated a little with a example to show others how to do this a little quicker?

Thanks for you help.