Extracting common logic into the base class.

This commit is contained in:
mark_story 2011-01-21 20:52:38 -05:00
parent 4610a0bf3c
commit bcd8dcd0f7
3 changed files with 36 additions and 32 deletions

View file

@ -59,6 +59,36 @@ abstract class BaseAuthenticate {
return Security::hash($password, null, true);
}
/**
* Find a user record using the standard options.
*
* @param string $username The username/identifier.
* @param string $password The unhashed password.
* @return Mixed Either false on failure, or an array of user data.
*/
protected function _findUser($username, $password) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
$conditions = array(
$model . '.' . $fields['username'] => $username,
$model . '.' . $fields['password'] => $this->hash($password),
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => 0
));
if (empty($result) || empty($result[$model])) {
return false;
}
unset($result[$model][$fields['password']]);
return $result[$model];
}
/**
* Authenticate a user based on the request information.
*

View file

@ -66,30 +66,16 @@ class BasicAuthenticate extends BaseAuthenticate {
return false;
}
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
$result = $this->_findUser($username, $pass);
$conditions = array(
$model . '.' . $fields['username'] => $username,
$model . '.' . $fields['password'] => $this->hash($pass),
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => 0
));
if (empty($result) || empty($result[$model])) {
if (empty($result)) {
$response->header($this->loginHeaders());
$response->header('Location', Router::reverse($request));
$response->statusCode(401);
$response->send();
return false;
}
unset($result[$model][$fields['password']]);
return $result[$model];
return $result;
}
/**

View file

@ -58,22 +58,10 @@ class FormAuthenticate extends BaseAuthenticate {
) {
return false;
}
$conditions = array(
$model . '.' . $fields['username'] => $request->data[$model][$fields['username']],
$model . '.' . $fields['password'] => $this->hash($request->data[$model][$fields['password']]),
return $this->_findUser(
$request->data[$model][$fields['username']],
$request->data[$model][$fields['password']]
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => 0
));
if (empty($result) || empty($result[$model])) {
return false;
}
unset($result[$model][$fields['password']]);
return $result[$model];
}
}