Skip to content
This repository has been archived by the owner on Jan 29, 2021. It is now read-only.

Commit

Permalink
Set debug back to a protected property
Browse files Browse the repository at this point in the history
Backwards support for cookieName and expires
Minor comment and syntax cleanup
  • Loading branch information
milesj committed Feb 8, 2012
1 parent 63c4707 commit 0e8fd42
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
60 changes: 41 additions & 19 deletions Controller/Component/AutoLoginComponent.php
@@ -1,6 +1,4 @@
<?php <?php
App::uses('Component', 'Controller');

/** /**
* AutoLoginComponent * AutoLoginComponent
* *
Expand All @@ -13,6 +11,9 @@
* *
* @modified Mark Scherer - 2012-01-08 ms * @modified Mark Scherer - 2012-01-08 ms
*/ */

App::uses('Component', 'Controller');

class AutoLoginComponent extends Component { class AutoLoginComponent extends Component {


/** /**
Expand All @@ -38,6 +39,14 @@ class AutoLoginComponent extends Component {
* @var array * @var array
*/ */
public $settings = array(); public $settings = array();

/**
* Should we debug?
*
* @access protected
* @var boolean
*/
protected $_debug = false;


/** /**
* Default settings. * Default settings.
Expand All @@ -46,19 +55,22 @@ class AutoLoginComponent extends Component {
* @var array * @var array
*/ */
protected $_defaults = array( protected $_defaults = array(
'active' => true, // Model
'model' => 'User', 'model' => 'User',
'username' => 'username', 'username' => 'username',
'password' => 'password', 'password' => 'password',
// Controller
'plugin' => '', 'plugin' => '',
'controller' => 'users', 'controller' => 'users',
'loginAction' => 'login', 'loginAction' => 'login',
'logoutAction' => 'logout', 'logoutAction' => 'logout',
// Cookie
'cookieName' => 'autoLogin', 'cookieName' => 'autoLogin',
'expires' => '+2 weeks', # Cookie length (strtotime() format) 'expires' => '+2 weeks', // Cookie length (strtotime() format)
'redirect' => true, // Logic
'requirePrompt' => true, # Displayed checkbox determines if cookie is created 'redirect' => true, // Force a redirect after successful autologin
'debug' => null # Auto-Select based on debug mode or ip range 'requirePrompt' => true, // Displayed checkbox determines if cookie is created
'active' => true // Force the process to continue or exit
); );


/** /**
Expand All @@ -77,26 +89,34 @@ class AutoLoginComponent extends Component {
* @return void * @return void
*/ */
public function initialize(Controller $controller) { public function initialize(Controller $controller) {
$this->settings = array_merge($this->_defaults, (array) Configure::read('AutoLogin')); $autoLogin = (array) Configure::read('AutoLogin');
$this->settings = array_merge($this->_defaults, $autoLogin);

// Backwards support
if (isset($this->cookieName)) {
$this->settings['cookieName'] = $this->cookieName;
}

if (isset($this->expires)) {
$this->settings['expires'] = $this->expires;
}


// Validate the cookie // Validate the cookie
$cookie = $this->Cookie->read($this->settings['cookieName']); $cookie = $this->Cookie->read($this->settings['cookieName']);
$user = $this->Auth->user(); $user = $this->Auth->user();


// Is debug enabled // Is debug enabled
if ($this->settings['debug'] === null) { $this->_debug = (!empty($autoLogin['email']) && !empty($autoLogin['ips']) && in_array(env('REMOTE_ADDR'), (array) $autoLogin['ips']));
$this->settings['debug'] = Configure::read('debug') > 0 || !empty($autoLogin['email']) && !empty($autoLogin['ips']) && in_array(env('REMOTE_ADDR'), (array) $autoLogin['ips']);
}


if (!$this->settings['active'] || !empty($user) || !$cookie || !$controller->request->is('get')) { if (!$this->settings['active'] || !empty($user) || !$controller->request->is('get')) {
return; return;


} elseif (!is_array($cookie)) { } else if (!is_array($cookie) || !$cookie) {
$this->debug('cookieFail', $this->Cookie, $user); $this->debug('cookieFail', $this->Cookie, $user);
$this->delete(); $this->delete();
return; return;


} elseif ($cookie['hash'] != $this->Auth->password($cookie['username'] . $cookie['time'])) { } else if ($cookie['hash'] != $this->Auth->password($cookie['username'] . $cookie['time'])) {
$this->debug('hashFail', $this->Cookie, $user); $this->debug('hashFail', $this->Cookie, $user);
$this->delete(); $this->delete();
return; return;
Expand Down Expand Up @@ -130,6 +150,7 @@ public function startup(Controller $controller) {
$this->Auth->user() $this->Auth->user()
)); ));
} }

if ($this->settings['redirect']) { if ($this->settings['redirect']) {
$controller->redirect(array(), 301); $controller->redirect(array(), 301);
} }
Expand Down Expand Up @@ -157,6 +178,7 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
if (empty($this->settings['active'])) { if (empty($this->settings['active'])) {
return; return;
} }

$model = $this->settings['model']; $model = $this->settings['model'];


if (is_array($this->Auth->loginAction)) { if (is_array($this->Auth->loginAction)) {
Expand Down Expand Up @@ -192,16 +214,16 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
if (!empty($username) && !empty($password) && $autoLogin) { if (!empty($username) && !empty($password) && $autoLogin) {
$this->save($username, $password); $this->save($username, $password);


} elseif (!$autoLogin) { } else if (!$autoLogin) {
$this->delete(); $this->delete();
} }
} }
break; break;


case $this->settings['logoutAction']: case $this->settings['logoutAction']:
$this->debug('logout', $this->Cookie, $this->Auth->user()); $this->debug('logout', $this->Cookie, $this->Auth->user());
$this->delete(); $this->delete();
break; break;
} }
} }
} }
Expand Down Expand Up @@ -263,7 +285,7 @@ public function debug($key, $cookie = array(), $user = array()) {
'custom' => 'Custom Callback' 'custom' => 'Custom Callback'
); );


if ($this->settings['debug'] && isset($scopes[$key])) { if ($this->_debug && isset($scopes[$key])) {
$debug = (array) Configure::read('AutoLogin'); $debug = (array) Configure::read('AutoLogin');
$content = ""; $content = "";


Expand All @@ -283,7 +305,7 @@ public function debug($key, $cookie = array(), $user = array()) {
if (!empty($debug['email'])) { if (!empty($debug['email'])) {
mail($debug['email'], '[AutoLogin] ' . $scopes[$key], $content, 'From: ' . $debug['email']); mail($debug['email'], '[AutoLogin] ' . $scopes[$key], $content, 'From: ' . $debug['email']);
} else { } else {
$this->log($scopes[$key] . ': ' . $content, 'autologin'); $this->log($scopes[$key] . ': ' . $content, LOG_DEBUG);
} }
} }
} }
Expand Down
67 changes: 30 additions & 37 deletions Test/Case/Controller/Component/AutoLoginComponentTest.php
@@ -1,14 +1,16 @@
<?php <?php
/**
* AutoLogin Test Cases.
*
* @author Miles Johnson - http://milesj.me
* @copyright Copyright 2006-2011, Miles Johnson, Inc.
* @license http://opensource.org/licenses/mit-license.php - Licensed under The MIT License
* @link http://milesj.me/code/cakephp/auto-login
*/


App::import('Component', 'AutoLogin'); App::import('Component', 'AutoLogin');
App::uses('Controller', 'Controller'); App::uses('Controller', 'Controller');


/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.controller.components
*/
class AutoLoginComponentTest extends CakeTestCase { class AutoLoginComponentTest extends CakeTestCase {


/** /**
Expand All @@ -18,7 +20,7 @@ class AutoLoginComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
public function setUp() { public function setUp() {
$this->Controller = new AutoLoginTestController(new CakeRequest, new CakeResponse); $this->Controller = new AutoLoginTestController(new CakeRequest(), new CakeResponse());
$this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection()); $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
} }


Expand All @@ -34,10 +36,9 @@ public function tearDown() {
} }


/** /**
* test if suhosin isn't messing up srand() and mt_srand() * Test if suhosin isn't messing up srand() and mt_srand()
* run this on every the environment you want AutoLogin to work! * Run this on every the environment you want AutoLogin to work!
* It this test fails add `suhosin.srand.ignore = Off` * It this test fails add `suhosin.srand.ignore = Off` in your `/etc/php5/apache2/php.ini`
* in your `/etc/php5/apache2/php.ini`
* And don't forget to restart apache or at least `/etc/init.d/apache2 force-reload` * And don't forget to restart apache or at least `/etc/init.d/apache2 force-reload`
*/ */
public function testIfRandWillWork() { public function testIfRandWillWork() {
Expand All @@ -51,7 +52,7 @@ public function testIfRandWillWork() {
} }


/** /**
* test merge of configs * Test merge of configs.
*/ */
public function testConfigs() { public function testConfigs() {
$this->Controller->AutoLogin->initialize($this->Controller); $this->Controller->AutoLogin->initialize($this->Controller);
Expand All @@ -66,77 +67,69 @@ public function testConfigs() {
Configure::write('AutoLogin.cookieName', 'myOtherAutoLogin'); Configure::write('AutoLogin.cookieName', 'myOtherAutoLogin');
$this->Controller->AutoLogin->initialize($this->Controller); $this->Controller->AutoLogin->initialize($this->Controller);
$settings = $this->Controller->AutoLogin->settings; $settings = $this->Controller->AutoLogin->settings;
//debug($settings); die();
$this->assertSame('myOtherAutoLogin', $settings['cookieName']); $this->assertSame('myOtherAutoLogin', $settings['cookieName']);
} }


} }



/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.controller.components
*/
class AutoLoginTestController extends Controller { class AutoLoginTestController extends Controller {
/**
* name property
*
* @var string 'SecurityTest'
* @access public
*/


/** /**
* components property * Components.
* *
* @var array
* @access public * @access public
* @var array
*/ */
public $components = array('AutoLogin'); public $components = array('AutoLogin');

/** /**
* failed property * Failed property.
* *
* @var bool false
* @access public * @access public
* @var boolean
*/ */
public $failed = false; public $failed = false;

/** /**
* Used for keeping track of headers in test * Used for keeping track of headers in test.
* *
* @var array
* @access public * @access public
* @var array
*/ */
public $testHeaders = array(); public $testHeaders = array();

/** /**
* fail method * Fail method.
* *
* @access public * @access public
* @return void * @return void
*/ */
public function fail() { public function fail() {
$this->failed = true; $this->failed = true;
} }

/** /**
* redirect method * Redirect method.
* *
* @access public
* @param mixed $option * @param mixed $option
* @param mixed $code * @param mixed $code
* @param mixed $exit * @param mixed $exit
* @access public
* @return void * @return void
*/ */
public function redirect($option, $code, $exit) { public function redirect($option, $code, $exit) {
return $code; return $code;
} }

/** /**
* Conveinence method for header() * Convenience method for header().
* *
* @access public
* @param string $status * @param string $status
* @return void * @return void
* @access public
*/ */
public function header($status) { public function header($status) {
$this->testHeaders[] = $status; $this->testHeaders[] = $status;
} }

} }

0 comments on commit 0e8fd42

Please sign in to comment.