Browse files

Set debug back to a protected property

Backwards support for cookieName and expires
Minor comment and syntax cleanup
  • Loading branch information...
1 parent 63c4707 commit 0e8fd420d10f63ab3e13c304c7f4f1ec6d7a3902 @milesj committed Feb 7, 2012
Showing with 71 additions and 56 deletions.
  1. +41 −19 Controller/Component/AutoLoginComponent.php
  2. +30 −37 Test/Case/Controller/Component/AutoLoginComponentTest.php
View
60 Controller/Component/AutoLoginComponent.php
@@ -1,6 +1,4 @@
<?php
-App::uses('Component', 'Controller');
-
/**
* AutoLoginComponent
*
@@ -13,6 +11,9 @@
*
* @modified Mark Scherer - 2012-01-08 ms
*/
+
+App::uses('Component', 'Controller');
+
class AutoLoginComponent extends Component {
/**
@@ -38,6 +39,14 @@ class AutoLoginComponent extends Component {
* @var array
*/
public $settings = array();
+
+ /**
+ * Should we debug?
+ *
+ * @access protected
+ * @var boolean
+ */
+ protected $_debug = false;
/**
* Default settings.
@@ -46,19 +55,22 @@ class AutoLoginComponent extends Component {
* @var array
*/
protected $_defaults = array(
- 'active' => true,
+ // Model
'model' => 'User',
'username' => 'username',
'password' => 'password',
+ // Controller
'plugin' => '',
'controller' => 'users',
'loginAction' => 'login',
'logoutAction' => 'logout',
+ // Cookie
'cookieName' => 'autoLogin',
- 'expires' => '+2 weeks', # Cookie length (strtotime() format)
- 'redirect' => true,
- 'requirePrompt' => true, # Displayed checkbox determines if cookie is created
- 'debug' => null # Auto-Select based on debug mode or ip range
+ 'expires' => '+2 weeks', // Cookie length (strtotime() format)
+ // Logic
+ 'redirect' => true, // Force a redirect after successful autologin
+ 'requirePrompt' => true, // Displayed checkbox determines if cookie is created
+ 'active' => true // Force the process to continue or exit
);
/**
@@ -77,26 +89,34 @@ class AutoLoginComponent extends Component {
* @return void
*/
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
$cookie = $this->Cookie->read($this->settings['cookieName']);
$user = $this->Auth->user();
// Is debug enabled
- if ($this->settings['debug'] === null) {
- $this->settings['debug'] = Configure::read('debug') > 0 || !empty($autoLogin['email']) && !empty($autoLogin['ips']) && in_array(env('REMOTE_ADDR'), (array) $autoLogin['ips']);
- }
+ $this->_debug = (!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;
- } elseif (!is_array($cookie)) {
+ } else if (!is_array($cookie) || !$cookie) {
$this->debug('cookieFail', $this->Cookie, $user);
$this->delete();
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->delete();
return;
@@ -130,6 +150,7 @@ public function startup(Controller $controller) {
$this->Auth->user()
));
}
+
if ($this->settings['redirect']) {
$controller->redirect(array(), 301);
}
@@ -157,6 +178,7 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
if (empty($this->settings['active'])) {
return;
}
+
$model = $this->settings['model'];
if (is_array($this->Auth->loginAction)) {
@@ -192,16 +214,16 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
if (!empty($username) && !empty($password) && $autoLogin) {
$this->save($username, $password);
- } elseif (!$autoLogin) {
+ } else if (!$autoLogin) {
$this->delete();
}
}
- break;
+ break;
case $this->settings['logoutAction']:
$this->debug('logout', $this->Cookie, $this->Auth->user());
$this->delete();
- break;
+ break;
}
}
}
@@ -263,7 +285,7 @@ public function debug($key, $cookie = array(), $user = array()) {
'custom' => 'Custom Callback'
);
- if ($this->settings['debug'] && isset($scopes[$key])) {
+ if ($this->_debug && isset($scopes[$key])) {
$debug = (array) Configure::read('AutoLogin');
$content = "";
@@ -283,7 +305,7 @@ public function debug($key, $cookie = array(), $user = array()) {
if (!empty($debug['email'])) {
mail($debug['email'], '[AutoLogin] ' . $scopes[$key], $content, 'From: ' . $debug['email']);
} else {
- $this->log($scopes[$key] . ': ' . $content, 'autologin');
+ $this->log($scopes[$key] . ': ' . $content, LOG_DEBUG);
}
}
}
View
67 Test/Case/Controller/Component/AutoLoginComponentTest.php
@@ -1,14 +1,16 @@
<?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::uses('Controller', 'Controller');
-/**
- * Short description for class.
- *
- * @package cake.tests
- * @subpackage cake.tests.cases.libs.controller.components
- */
class AutoLoginComponentTest extends CakeTestCase {
/**
@@ -18,7 +20,7 @@ class AutoLoginComponentTest extends CakeTestCase {
* @return void
*/
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());
}
@@ -34,10 +36,9 @@ public function tearDown() {
}
/**
- * test if suhosin isn't messing up srand() and mt_srand()
- * run this on every the environment you want AutoLogin to work!
- * It this test fails add `suhosin.srand.ignore = Off`
- * in your `/etc/php5/apache2/php.ini`
+ * Test if suhosin isn't messing up srand() and mt_srand()
+ * Run this on every the environment you want AutoLogin to work!
+ * It this test fails add `suhosin.srand.ignore = Off` in your `/etc/php5/apache2/php.ini`
* And don't forget to restart apache or at least `/etc/init.d/apache2 force-reload`
*/
public function testIfRandWillWork() {
@@ -51,7 +52,7 @@ public function testIfRandWillWork() {
}
/**
- * test merge of configs
+ * Test merge of configs.
*/
public function testConfigs() {
$this->Controller->AutoLogin->initialize($this->Controller);
@@ -66,77 +67,69 @@ public function testConfigs() {
Configure::write('AutoLogin.cookieName', 'myOtherAutoLogin');
$this->Controller->AutoLogin->initialize($this->Controller);
$settings = $this->Controller->AutoLogin->settings;
- //debug($settings); die();
$this->assertSame('myOtherAutoLogin', $settings['cookieName']);
}
}
-
-/**
- * Short description for class.
- *
- * @package cake.tests
- * @subpackage cake.tests.cases.libs.controller.components
- */
class AutoLoginTestController extends Controller {
- /**
- * name property
- *
- * @var string 'SecurityTest'
- * @access public
- */
/**
- * components property
+ * Components.
*
- * @var array
* @access public
+ * @var array
*/
public $components = array('AutoLogin');
+
/**
- * failed property
+ * Failed property.
*
- * @var bool false
* @access public
+ * @var boolean
*/
public $failed = false;
+
/**
- * Used for keeping track of headers in test
+ * Used for keeping track of headers in test.
*
- * @var array
* @access public
+ * @var array
*/
public $testHeaders = array();
+
/**
- * fail method
+ * Fail method.
*
* @access public
* @return void
*/
public function fail() {
$this->failed = true;
}
+
/**
- * redirect method
+ * Redirect method.
*
+ * @access public
* @param mixed $option
* @param mixed $code
* @param mixed $exit
- * @access public
* @return void
*/
public function redirect($option, $code, $exit) {
return $code;
}
+
/**
- * Conveinence method for header()
+ * Convenience method for header().
*
+ * @access public
* @param string $status
* @return void
- * @access public
*/
public function header($status) {
$this->testHeaders[] = $status;
}
+
}

0 comments on commit 0e8fd42

Please sign in to comment.