From 655f3b5e70347886392fc32c6fe2d6da43317382 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 19 Oct 2014 00:27:05 +0530 Subject: [PATCH] Fix loading of PasswordHasher class from plugin. Fixes #4919 --- src/Auth/PasswordHasherFactory.php | 9 ++- .../Auth/PasswordHasherFactoryTest.php | 59 +++++++++++++++++++ .../src/Auth/LegacyPasswordHasher.php | 21 +++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 tests/TestCase/Auth/PasswordHasherFactoryTest.php create mode 100644 tests/test_app/Plugin/TestPlugin/src/Auth/LegacyPasswordHasher.php diff --git a/src/Auth/PasswordHasherFactory.php b/src/Auth/PasswordHasherFactory.php index eea1969811a..3b018b71936 100644 --- a/src/Auth/PasswordHasherFactory.php +++ b/src/Auth/PasswordHasherFactory.php @@ -25,11 +25,11 @@ class PasswordHasherFactory { /** * Returns password hasher object out of a hasher name or a configuration array * - * @param string|array $passwordHasher name of the password hasher or an array with + * @param string|array $passwordHasher Name of the password hasher or an array with * at least the key `className` set to the name of the class to use - * @return AbstractPasswordHasher Password hasher instance + * @return \Cake\Auth\AbstractPasswordHasher Password hasher instance * @throws \RuntimeException If password hasher class not found or - * it does not extend AbstractPasswordHasher + * it does not extend Cake\Auth\AbstractPasswordHasher */ public static function build($passwordHasher) { $config = []; @@ -41,9 +41,8 @@ public static function build($passwordHasher) { unset($config['className']); } - list($plugin, $class) = pluginSplit($class, true); $className = App::className($class, 'Auth', 'PasswordHasher'); - if (!class_exists($className)) { + if (!$className) { throw new \RuntimeException(sprintf('Password hasher class "%s" was not found.', $class)); } diff --git a/tests/TestCase/Auth/PasswordHasherFactoryTest.php b/tests/TestCase/Auth/PasswordHasherFactoryTest.php new file mode 100644 index 00000000000..0c6ce3dded4 --- /dev/null +++ b/tests/TestCase/Auth/PasswordHasherFactoryTest.php @@ -0,0 +1,59 @@ +assertInstanceof('Cake\Auth\DefaultPasswordHasher', $hasher); + + $hasher = PasswordHasherFactory::build([ + 'className' => 'Default', + 'hashOptions' => ['foo' =>' bar'] + ]); + $this->assertInstanceof('Cake\Auth\DefaultPasswordHasher', $hasher); + $this->assertEquals(['foo' =>' bar'], $hasher->config('hashOptions')); + + Plugin::load('TestPlugin'); + $hasher = PasswordHasherFactory::build('TestPlugin.Legacy'); + $this->assertInstanceof('TestPlugin\Auth\LegacyPasswordHasher', $hasher); + } + +/** + * test build() throws exception for non existent hasher + * + * @expectedException \RuntimeException + * @expectedExceptionMessage Password hasher class "FooBar" was not found. + * @return void + */ + public function testBuildException() { + $hasher = PasswordHasherFactory::build('FooBar'); + } + +} diff --git a/tests/test_app/Plugin/TestPlugin/src/Auth/LegacyPasswordHasher.php b/tests/test_app/Plugin/TestPlugin/src/Auth/LegacyPasswordHasher.php new file mode 100644 index 00000000000..382f283b9c2 --- /dev/null +++ b/tests/test_app/Plugin/TestPlugin/src/Auth/LegacyPasswordHasher.php @@ -0,0 +1,21 @@ + + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @since 3.0.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +namespace TestPlugin\Auth; + +use Cake\Auth\DefaultPasswordHasher; + +class LegacyPasswordHasher extends DefaultPasswordHasher { +}