Skip to content

Commit

Permalink
Fixing Authcomponent tests. Updating the component + tests to use the…
Browse files Browse the repository at this point in the history
… new request object.

Deprecating a number of Authcomponent properties.
  • Loading branch information
markstory committed May 15, 2010
1 parent 36a7158 commit b2d8536
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 207 deletions.
95 changes: 40 additions & 55 deletions cake/libs/controller/components/auth.php
Expand Up @@ -37,23 +37,20 @@ class AuthComponent extends Object {
* Maintains current user login state.
*
* @var boolean
* @access private
*/
protected $_loggedIn = false;

/**
* Other components utilized by AuthComponent
*
* @var array
* @access public
*/
public $components = array('Session', 'RequestHandler');

/**
* A reference to the object used for authentication
*
* @var object
* @access public
* @link http://book.cakephp.org/view/1278/authenticate
*/
public $authenticate = null;
Expand All @@ -67,7 +64,6 @@ class AuthComponent extends Object {
* 'object' will validate Controller::action against object::isAuthorized(user, controller, action)
*
* @var mixed
* @access public
* @link http://book.cakephp.org/view/1275/authorize
*/
public $authorize = false;
Expand All @@ -77,7 +73,6 @@ class AuthComponent extends Object {
* with an invalid or expired session
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1277/ajaxLogin
*/
public $ajaxLogin = null;
Expand All @@ -86,15 +81,13 @@ class AuthComponent extends Object {
* The name of the element used for SessionComponent::setFlash
*
* @var string
* @access public
*/
public $flashElement = 'default';

/**
* The name of the model that represents users which will be authenticated. Defaults to 'User'.
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1266/userModel
*/
public $userModel = 'User';
Expand All @@ -104,7 +97,6 @@ class AuthComponent extends Object {
* i.e. array('User.is_active' => 1).
*
* @var array
* @access public
* @link http://book.cakephp.org/view/1268/userScope
*/
public $userScope = array();
Expand All @@ -114,7 +106,6 @@ class AuthComponent extends Object {
* $userModel, i.e. array('username' => 'login_name', 'password' => 'passwd').
*
* @var array
* @access public
* @link http://book.cakephp.org/view/1267/fields
*/
public $fields = array('username' => 'username', 'password' => 'password');
Expand All @@ -124,7 +115,6 @@ class AuthComponent extends Object {
* unspecified, it will be "Auth.{$userModel name}".
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1276/sessionKey
*/
public $sessionKey = null;
Expand All @@ -136,7 +126,6 @@ class AuthComponent extends Object {
* "Controllers/".
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1279/actionPath
*/
public $actionPath = null;
Expand All @@ -146,7 +135,6 @@ class AuthComponent extends Object {
* logins.
*
* @var mixed
* @access public
* @link http://book.cakephp.org/view/1269/loginAction
*/
public $loginAction = null;
Expand All @@ -158,7 +146,6 @@ class AuthComponent extends Object {
* set, the user will be redirected to the page specified in $loginRedirect.
*
* @var mixed
* @access public
* @link http://book.cakephp.org/view/1270/loginRedirect
*/
public $loginRedirect = null;
Expand All @@ -169,7 +156,6 @@ class AuthComponent extends Object {
* Defaults to AuthComponent::$loginAction.
*
* @var mixed
* @access public
* @see AuthComponent::$loginAction
* @see AuthComponent::logout()
* @link http://book.cakephp.org/view/1271/logoutRedirect
Expand All @@ -180,7 +166,6 @@ class AuthComponent extends Object {
* The name of model or model object, or any other object has an isAuthorized method.
*
* @var string
* @access public
*/
public $object = null;

Expand All @@ -189,7 +174,6 @@ class AuthComponent extends Object {
* login failures, so as not to expose information on why the login failed.
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1272/loginError
*/
public $loginError = null;
Expand All @@ -199,7 +183,6 @@ class AuthComponent extends Object {
* acccess.
*
* @var string
* @access public
* @link http://book.cakephp.org/view/1273/authError
*/
public $authError = null;
Expand All @@ -208,7 +191,6 @@ class AuthComponent extends Object {
* Determines whether AuthComponent will automatically redirect and exit if login is successful.
*
* @var boolean
* @access public
* @link http://book.cakephp.org/view/1274/autoRedirect
*/
public $autoRedirect = true;
Expand All @@ -217,7 +199,6 @@ class AuthComponent extends Object {
* Controller actions for which user validation is not required.
*
* @var array
* @access public
* @see AuthComponent::allow()
* @link http://book.cakephp.org/view/1251/Setting-Auth-Component-Variables
*/
Expand All @@ -227,7 +208,6 @@ class AuthComponent extends Object {
* Maps actions to CRUD operations. Used for controller-based validation ($validate = 'controller').
*
* @var array
* @access public
* @see AuthComponent::mapActions()
*/
public $actionMap = array(
Expand All @@ -238,27 +218,33 @@ class AuthComponent extends Object {
'remove' => 'delete'
);

/**
* Request object
*
* @var CakeRequest
*/
public $request;

/**
* Form data from Controller::$data
*
* @deprecated Use $this->request->data instead
* @var array
* @access public
*/
public $data = array();

/**
* Parameter data from Controller::$params
*
* @deprecated Use $this->request instead
* @var array
* @access public
*/
public $params = array();

/**
* Method list for bound controller
*
* @var array
* @access protected
*/
protected $_methods = array();

Expand All @@ -268,8 +254,10 @@ class AuthComponent extends Object {
* @param object $controller A reference to the instantiating controller object
* @return void
*/
public function initialize(&$controller, $settings = array()) {
$this->params = $controller->params;
public function initialize(Controller $controller, $settings = array()) {
$this->request = $controller->request;
$this->params = $this->request;

$crud = array('create', 'read', 'update', 'delete');
$this->actionMap = array_merge($this->actionMap, array_combine($crud, $crud));
$this->_methods = $controller->methods;
Expand Down Expand Up @@ -314,7 +302,7 @@ public function startup(&$controller) {
}

$methods = array_flip($controller->methods);
$action = strtolower($controller->params['action']);
$action = strtolower($controller->request->params['action']);
$isMissingAction = (
$controller->scaffold === false &&
!isset($methods[$action])
Expand All @@ -327,12 +315,13 @@ public function startup(&$controller) {
if (!$this->__setDefaults()) {
return false;
}

$this->data = $controller->data = $this->hashPasswords($controller->data);
$request =& $controller->request;

$this->request->data = $controller->request->data = $this->hashPasswords($request->data);
$url = '';

if (isset($controller->params['url']['url'])) {
$url = $controller->params['url']['url'];
if (isset($request->query['url'])) {
$url = $request->query['url'];
}
$url = Router::normalize($url);
$loginAction = Router::normalize($this->loginAction);
Expand All @@ -348,20 +337,20 @@ public function startup(&$controller) {
}

if ($loginAction == $url) {
$model =& $this->getModel();
if (empty($controller->data) || !isset($controller->data[$model->alias])) {
$model = $this->getModel();
if (empty($request->data) || !isset($request->data[$model->alias])) {
if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
$this->Session->write('Auth.redirect', $controller->referer(null, true));
}
return false;
}

$isValid = !empty($controller->data[$model->alias][$this->fields['username']]) &&
!empty($controller->data[$model->alias][$this->fields['password']]);
$isValid = !empty($request->data[$model->alias][$this->fields['username']]) &&
!empty($request->data[$model->alias][$this->fields['password']]);

if ($isValid) {
$username = $controller->data[$model->alias][$this->fields['username']];
$password = $controller->data[$model->alias][$this->fields['password']];
$username = $request->data[$model->alias][$this->fields['username']];
$password = $request->data[$model->alias][$this->fields['password']];

$data = array(
$model->alias . '.' . $this->fields['username'] => $username,
Expand All @@ -377,14 +366,14 @@ public function startup(&$controller) {
}

$this->Session->setFlash($this->loginError, $this->flashElement, array(), 'auth');
$controller->data[$model->alias][$this->fields['password']] = null;
$request->data[$model->alias][$this->fields['password']] = null;
return false;
} else {
if (!$this->user()) {
if (!$this->RequestHandler->isAjax()) {
$this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth');
if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) {
$query = $controller->params['url'];
if (!empty($request->query) && count($request->query) >= 2) {
$query = $request->query;
unset($query['url'], $query['ext']);
$url .= Router::queryString($query, array());
}
Expand All @@ -409,12 +398,12 @@ public function startup(&$controller) {
extract($this->__authType());
switch ($type) {
case 'controller':
$this->object =& $controller;
$this->object = $controller;
break;
case 'crud':
case 'actions':
if (isset($controller->Acl)) {
$this->Acl =& $controller->Acl;
$this->Acl = $controller->Acl;
} else {
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.'), E_USER_WARNING);
}
Expand Down Expand Up @@ -525,22 +514,22 @@ public function isAuthorized($type = null, $object = null, $user = null) {
break;
case 'crud':
$this->mapActions();
if (!isset($this->actionMap[$this->params['action']])) {
if (!isset($this->actionMap[$this->request['action']])) {
trigger_error(
sprintf(__('Auth::startup() - Attempted access of un-mapped action "%1$s" in controller "%2$s"'), $this->params['action'], $this->params['controller']),
sprintf(__('Auth::startup() - Attempted access of un-mapped action "%1$s" in controller "%2$s"'), $this->request['action'], $this->request['controller']),
E_USER_WARNING
);
} else {
$valid = $this->Acl->check(
$user,
$this->action(':controller'),
$this->actionMap[$this->params['action']]
$this->actionMap[$this->request['action']]
);
}
break;
case 'model':
$this->mapActions();
$action = $this->params['action'];
$action = $this->request['action'];
if (isset($this->actionMap[$action])) {
$action = $this->actionMap[$action];
}
Expand Down Expand Up @@ -717,7 +706,7 @@ public function user($key = null) {
}

if ($key == null) {
$model =& $this->getModel();
$model = $this->getModel();
return array($model->alias => $this->Session->read($this->sessionKey));
} else {
$user = $this->Session->read($this->sessionKey);
Expand Down Expand Up @@ -783,10 +772,10 @@ public function validate($object, $user = null, $action = null) {
* @link http://book.cakephp.org/view/1256/action
*/
public function action($action = ':plugin/:controller/:action') {
$plugin = empty($this->params['plugin']) ? null : Inflector::camelize($this->params['plugin']) . '/';
$plugin = empty($this->request['plugin']) ? null : Inflector::camelize($this->request['plugin']) . '/';
return str_replace(
array(':controller', ':action', ':plugin/'),
array(Inflector::camelize($this->params['controller']), $this->params['action'], $plugin),
array(Inflector::camelize($this->request['controller']), $this->request['action'], $plugin),
$this->actionPath . $action
);
}
Expand All @@ -804,11 +793,7 @@ public function &getModel($name = null) {
$name = $this->userModel;
}

if (PHP5) {
$model = ClassRegistry::init($name);
} else {
$model =& ClassRegistry::init($name);
}
$model = ClassRegistry::init($name);

if (empty($model)) {
trigger_error(__('Auth::getModel() - Model is not set or could not be found'), E_USER_WARNING);
Expand All @@ -834,7 +819,7 @@ public function identify($user = null, $conditions = null) {
} else {
$conditions = $this->userScope;
}
$model =& $this->getModel();
$model = $this->getModel();
if (empty($user)) {
$user = $this->user();
if (empty($user)) {
Expand Down Expand Up @@ -908,7 +893,7 @@ public function hashPasswords($data) {
}

if (is_array($data)) {
$model =& $this->getModel();
$model = $this->getModel();

if(isset($data[$model->alias])) {
if (isset($data[$model->alias][$this->fields['username']]) && isset($data[$model->alias][$this->fields['password']])) {
Expand Down

0 comments on commit b2d8536

Please sign in to comment.