Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replacing Authcomponent::$flashElement with Authcomponent::$flash, wh…
…ich gives access to all the flash parameters. Also adding a wrapper method for more terse code and ability to extend functionality.
  • Loading branch information
markstory committed Feb 4, 2011
1 parent ff889c2 commit b59d0e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
31 changes: 25 additions & 6 deletions cake/libs/controller/components/auth.php
Expand Up @@ -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'.
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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']);
}
}
19 changes: 19 additions & 0 deletions cake/tests/cases/libs/controller/components/auth.test.php
Expand Up @@ -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');
}
}

0 comments on commit b59d0e8

Please sign in to comment.