|
|
@@ -30,7 +30,14 @@ use Cake\Utility\Security;
|
|
|
abstract class BaseAuthenticate {
|
|
|
|
|
|
/**
|
|
|
- * Settings for this object.
|
|
|
+ * Runtime config for this object
|
|
|
+ *
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $_config = [];
|
|
|
+
|
|
|
+/**
|
|
|
+ * Default config for this object.
|
|
|
*
|
|
|
* - `fields` The fields to use to identify a user by.
|
|
|
* - `userModel` The alias for users table, defaults to Users.
|
|
|
@@ -39,11 +46,11 @@ abstract class BaseAuthenticate {
|
|
|
* - `contain` Extra models to contain and store in session.
|
|
|
* - `passwordHasher` Password hasher class. Can be a string specifying class name
|
|
|
* or an array containing `className` key, any other keys will be passed as
|
|
|
- * settings to the class. Defaults to 'Blowfish'.
|
|
|
+ * config to the class. Defaults to 'Blowfish'.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- public $settings = [
|
|
|
+ protected $_defaultConfig = [
|
|
|
'fields' => [
|
|
|
'username' => 'username',
|
|
|
'password' => 'password'
|
|
|
@@ -72,11 +79,11 @@ abstract class BaseAuthenticate {
|
|
|
* Constructor
|
|
|
*
|
|
|
* @param ComponentRegistry $registry The Component registry used on this request.
|
|
|
- * @param array $settings Array of settings to use.
|
|
|
+ * @param array $config Array of config to use.
|
|
|
*/
|
|
|
- public function __construct(ComponentRegistry $registry, $settings) {
|
|
|
+ public function __construct(ComponentRegistry $registry, $config) {
|
|
|
$this->_registry = $registry;
|
|
|
- $this->settings = Hash::merge($this->settings, $settings);
|
|
|
+ $this->_config = Hash::merge($this->_defaultConfig, $config);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -91,20 +98,24 @@ abstract class BaseAuthenticate {
|
|
|
* @return boolean|array Either false on failure, or an array of user data.
|
|
|
*/
|
|
|
protected function _findUser($username, $password = null) {
|
|
|
- $userModel = $this->settings['userModel'];
|
|
|
+ $userModel = $this->config('userModel');
|
|
|
list(, $model) = pluginSplit($userModel);
|
|
|
- $fields = $this->settings['fields'];
|
|
|
+ $fields = $this->config('fields');
|
|
|
|
|
|
$conditions = [$model . '.' . $fields['username'] => $username];
|
|
|
|
|
|
- if (!empty($this->settings['scope'])) {
|
|
|
- $conditions = array_merge($conditions, $this->settings['scope']);
|
|
|
+ $scope = $this->config('scope');
|
|
|
+ if ($scope) {
|
|
|
+ $conditions = array_merge($conditions, $scope);
|
|
|
}
|
|
|
|
|
|
$table = TableRegistry::get($userModel)->find('all');
|
|
|
- if ($this->settings['contain']) {
|
|
|
- $table = $table->contain($this->settings['contain']);
|
|
|
+
|
|
|
+ $contain = $this->config('contain');
|
|
|
+ if ($contain) {
|
|
|
+ $table = $table->contain($contain);
|
|
|
}
|
|
|
+
|
|
|
$result = $table
|
|
|
->where($conditions)
|
|
|
->hydrate(false)
|
|
|
@@ -125,6 +136,50 @@ abstract class BaseAuthenticate {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * config getter and setter
|
|
|
+ *
|
|
|
+ * Usage:
|
|
|
+ * {{{
|
|
|
+ * $instance->config(); will return full config
|
|
|
+ * $instance->config('foo'); will return configured foo
|
|
|
+ * $instance->config('notset'); will return null
|
|
|
+ * }}}
|
|
|
+ *
|
|
|
+ * @param string|null $key to return
|
|
|
+ * @param mixed $val value to set
|
|
|
+ * @return mixed array or config value
|
|
|
+ */
|
|
|
+ public function config($key = null, $val = null) {
|
|
|
+ if ($key === null) {
|
|
|
+ return $this->_config;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($val !== null) {
|
|
|
+ return $this->_configSet([$key => $val]);
|
|
|
+ } elseif (is_array($key)) {
|
|
|
+ return $this->_configSet($key);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Hash::get($this->_config, $key);
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Update config with passed argument
|
|
|
+ *
|
|
|
+ * @param array $config
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function _configSet($config) {
|
|
|
+ foreach ($config as $key => $val) {
|
|
|
+ if (strpos($key, '.')) {
|
|
|
+ $this->_config = Hash::insert($this->_config, $key, $val);
|
|
|
+ } else {
|
|
|
+ $this->_config[$key] = $val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
* Return password hasher object
|
|
|
*
|
|
|
* @return AbstractPasswordHasher Password hasher instance
|
|
|
@@ -136,12 +191,14 @@ abstract class BaseAuthenticate {
|
|
|
return $this->_passwordHasher;
|
|
|
}
|
|
|
|
|
|
+ $passwordHasher = $this->config('passwordHasher');
|
|
|
+
|
|
|
$config = array();
|
|
|
- if (is_string($this->settings['passwordHasher'])) {
|
|
|
- $class = $this->settings['passwordHasher'];
|
|
|
+ if (is_string($passwordHasher)) {
|
|
|
+ $class = $passwordHasher;
|
|
|
} else {
|
|
|
- $class = $this->settings['passwordHasher']['className'];
|
|
|
- $config = $this->settings['passwordHasher'];
|
|
|
+ $class = $passwordHasher['className'];
|
|
|
+ $config = $passwordHasher;
|
|
|
unset($config['className']);
|
|
|
}
|
|
|
|