diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index 4aed7f01580..afe98c888fc 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -111,11 +111,20 @@ class AuthComponent extends Component { public $ajaxLogin = null; /** - * The name of the element used for SessionComponent::setFlash + * Settings to use when Auth needs to do a flash message with SessionComponent::setFlash(). + * Available keys are: * - * @var string + * - `element` - The element to use, defaults to 'default'. + * - `key` - The key to use, defaults to 'auth' + * - `params` - The array of additional params to use, defaults to array() + * + * @var array */ - public $flashElement = 'default'; + public $flash = array( + 'element' => 'default', + 'key' => 'auth', + 'params' => array() + ); /** * The name of the model that represents users which will be authenticated. Defaults to 'User'. @@ -342,13 +351,13 @@ public function startup($controller) { } } - $this->Session->setFlash($this->loginError, $this->flashElement, array(), 'auth'); + $this->flash($this->loginError); $request->data[$model->alias][$this->fields['password']] = null; return false; } else { if (!$this->user()) { if (!$request->is('ajax')) { - $this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth'); + $this->flash($this->authError); if (!empty($request->query) && count($request->query) >= 2) { $query = $request->query; unset($query['url'], $query['ext']); @@ -376,7 +385,7 @@ public function startup($controller) { return true; } - $this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth'); + $this->flash($this->authError); $controller->redirect($controller->referer(), null, true); return false; } @@ -792,4 +801,14 @@ public function loggedIn($logged = null) { } return $this->_loggedIn; } + +/** + * Set a flash message. Uses the Session component, and values from AuthComponent::$flash. + * + * @param string $message The message to set. + * @return void + */ + public function flash($message) { + $this->Session->setFlash($message, $this->flash['element'], $this->flash['params'], $this->flash['key']); + } } diff --git a/cake/tests/cases/libs/controller/components/auth.test.php b/cake/tests/cases/libs/controller/components/auth.test.php index 600b08e2067..baac8287d7a 100644 --- a/cake/tests/cases/libs/controller/components/auth.test.php +++ b/cake/tests/cases/libs/controller/components/auth.test.php @@ -1522,4 +1522,23 @@ function testLoginWithUserData() { $this->assertTrue($this->Controller->Auth->loggedIn()); $this->assertEquals($user['username'], $this->Controller->Auth->user('username')); } + +/** + * test flash settings. + * + * @return void + */ + function testFlashSettings() { + $this->Controller->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', zfalse); + $this->Controller->Auth->Session->expects($this->once()) + ->method('setFlash') + ->with('Auth failure', 'custom', array(1), 'auth-key'); + + $this->Controller->Auth->flash = array( + 'element' => 'custom', + 'params' => array(1), + 'key' => 'auth-key' + ); + $this->Controller->Auth->flash('Auth failure'); + } }