Replacing Authcomponent::$flashElement with Authcomponent::$flash, which gives access to all the flash parameters. Also adding a wrapper method for more terse code and ability to extend functionality.

This commit is contained in:
mark_story 2011-01-19 16:17:17 -05:00
parent ff889c2c8e
commit b59d0e8bb1
2 changed files with 44 additions and 6 deletions

View file

@ -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 @@ class AuthComponent extends Component {
}
}
$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 @@ class AuthComponent extends Component {
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 @@ class AuthComponent extends Component {
}
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']);
}
}

View file

@ -1522,4 +1522,23 @@ class AuthTest extends CakeTestCase {
$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');
}
}