diff options
-rw-r--r-- | Acl/Resources/bin/generateSql.php | 2 | ||||
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | Core/Authentication/Provider/DaoAuthenticationProvider.php | 2 | ||||
-rw-r--r-- | Core/Authentication/Provider/UserAuthenticationProvider.php | 2 | ||||
-rw-r--r-- | Core/Encoder/EncoderFactory.php | 6 | ||||
-rw-r--r-- | Core/Encoder/EncoderFactoryInterface.php | 8 | ||||
-rw-r--r-- | Core/Validator/Constraint/UserPassword.php | 27 | ||||
-rw-r--r-- | Core/Validator/Constraint/UserPasswordValidator.php | 46 | ||||
-rw-r--r-- | Http/Firewall/ExceptionListener.php | 2 | ||||
-rw-r--r-- | Http/Firewall/UsernamePasswordFormAuthenticationListener.php | 12 | ||||
-rw-r--r-- | Http/HttpUtils.php | 62 | ||||
-rw-r--r-- | Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php | 94 | ||||
-rw-r--r-- | Tests/Core/Encoder/EncoderFactoryTest.php | 51 | ||||
-rw-r--r-- | Tests/Http/HttpUtilsTest.php | 46 | ||||
-rw-r--r-- | composer.json | 2 |
15 files changed, 278 insertions, 86 deletions
diff --git a/Acl/Resources/bin/generateSql.php b/Acl/Resources/bin/generateSql.php index 0f9b4c1..25ded7a 100644 --- a/Acl/Resources/bin/generateSql.php +++ b/Acl/Resources/bin/generateSql.php @@ -47,6 +47,6 @@ foreach ($finder as $file) { } $platform = $reflection->newInstance(); - $targetFile = sprintf(__DIR__.'/../schema/%s.sql', $platform->getName()); + $targetFile = sprintf(__DIR__.'/../schema/%s.sql', $platform->name); file_put_contents($targetFile, implode("\n\n", $schema->toSql($platform))); } diff --git a/CHANGELOG.md b/CHANGELOG.md index a555f1e..6394ff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 2.1.0 ----- + * changed the HttpUtils constructor signature to take a UrlGenerator and a UrlMatcher instead of a Router + * EncoderFactoryInterface::getEncoder() can now also take a class name as an argument * allow switching to the user that is already impersonated * added support for the remember_me parameter in the query * added AccessMapInterface diff --git a/Core/Authentication/Provider/DaoAuthenticationProvider.php b/Core/Authentication/Provider/DaoAuthenticationProvider.php index f17eaa4..f22045f 100644 --- a/Core/Authentication/Provider/DaoAuthenticationProvider.php +++ b/Core/Authentication/Provider/DaoAuthenticationProvider.php @@ -59,7 +59,7 @@ class DaoAuthenticationProvider extends UserAuthenticationProvider throw new BadCredentialsException('The credentials were changed from another session.'); } } else { - if (!$presentedPassword = $token->getCredentials()) { + if ("" === ($presentedPassword = $token->getCredentials())) { throw new BadCredentialsException('The presented password cannot be empty.'); } diff --git a/Core/Authentication/Provider/UserAuthenticationProvider.php b/Core/Authentication/Provider/UserAuthenticationProvider.php index f0463ea..32d7971 100644 --- a/Core/Authentication/Provider/UserAuthenticationProvider.php +++ b/Core/Authentication/Provider/UserAuthenticationProvider.php @@ -109,7 +109,7 @@ abstract class UserAuthenticationProvider implements AuthenticationProviderInter * @param string $username The username to retrieve * @param UsernamePasswordToken $token The Token * - * @return array The user + * @return UserInterface The user * * @throws AuthenticationException if the credentials could not be validated */ diff --git a/Core/Encoder/EncoderFactory.php b/Core/Encoder/EncoderFactory.php index 738706a..7d34cc7 100644 --- a/Core/Encoder/EncoderFactory.php +++ b/Core/Encoder/EncoderFactory.php @@ -30,10 +30,10 @@ class EncoderFactory implements EncoderFactoryInterface /** * {@inheritDoc} */ - public function getEncoder(UserInterface $user) + public function getEncoder($user) { foreach ($this->encoders as $class => $encoder) { - if (!$user instanceof $class) { + if ((is_object($user) && !$user instanceof $class) || (!is_object($user) && !is_subclass_of($user, $class) && $user != $class)) { continue; } @@ -44,7 +44,7 @@ class EncoderFactory implements EncoderFactoryInterface return $this->encoders[$class]; } - throw new \RuntimeException(sprintf('No encoder has been configured for account "%s".', get_class($user))); + throw new \RuntimeException(sprintf('No encoder has been configured for account "%s".', is_object($user) ? get_class($user) : $user)); } /** diff --git a/Core/Encoder/EncoderFactoryInterface.php b/Core/Encoder/EncoderFactoryInterface.php index 3ae07e6..125e57b 100644 --- a/Core/Encoder/EncoderFactoryInterface.php +++ b/Core/Encoder/EncoderFactoryInterface.php @@ -23,9 +23,11 @@ interface EncoderFactoryInterface /** * Returns the password encoder to use for the given account. * - * @param UserInterface $user + * @param UserInterface|string $user A UserInterface instance of a class name * - * @return PasswordEncoderInterface never null + * @return PasswordEncoderInterface + * + * @throws \RuntimeException when no password encoder could be found for the user */ - function getEncoder(UserInterface $user); + function getEncoder($user); } diff --git a/Core/Validator/Constraint/UserPassword.php b/Core/Validator/Constraint/UserPassword.php new file mode 100644 index 0000000..ef6e1ec --- /dev/null +++ b/Core/Validator/Constraint/UserPassword.php @@ -0,0 +1,27 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\Validator\Constraint; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + */ +class UserPassword extends Constraint +{ + public $message = 'This value should be the user current password'; + + public function validatedBy() + { + return 'security.validator.user_password'; + } +} diff --git a/Core/Validator/Constraint/UserPasswordValidator.php b/Core/Validator/Constraint/UserPasswordValidator.php new file mode 100644 index 0000000..a54906b --- /dev/null +++ b/Core/Validator/Constraint/UserPasswordValidator.php @@ -0,0 +1,46 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\Validator\Constraint; + +use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +class UserPasswordValidator extends ConstraintValidator +{ + private $securityContext; + private $encoderFactory; + + public function __construct(SecurityContextInterface $securityContext, EncoderFactoryInterface $encoderFactory) + { + $this->securityContext = $securityContext; + $this->encoderFactory = $encoderFactory; + } + + public function validate($password, Constraint $constraint) + { + $user = $this->securityContext->getToken()->getUser(); + + if (!$user instanceof UserInterface) { + throw new ConstraintDefinitionException('The User must extend UserInterface'); + } + + $encoder = $this->encoderFactory->getEncoder($user); + + if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { + $this->context->addViolation($constraint->message); + } + } +} diff --git a/Http/Firewall/ExceptionListener.php b/Http/Firewall/ExceptionListener.php index 1a2d2b6..9a53827 100644 --- a/Http/Firewall/ExceptionListener.php +++ b/Http/Firewall/ExceptionListener.php @@ -181,7 +181,7 @@ class ExceptionListener protected function setTargetPath(Request $request) { // session isn't required when using http basic authentication mechanism for example - if ($request->hasSession()) { + if ($request->hasSession() && $request->isMethodSafe()) { $request->getSession()->set('_security.' . $this->providerKey . '.target_path', $request->getUri()); } } diff --git a/Http/Firewall/UsernamePasswordFormAuthenticationListener.php b/Http/Firewall/UsernamePasswordFormAuthenticationListener.php index bd2cec1..4bcb785 100644 --- a/Http/Firewall/UsernamePasswordFormAuthenticationListener.php +++ b/Http/Firewall/UsernamePasswordFormAuthenticationListener.php @@ -51,6 +51,18 @@ class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationL } /** + * @{inheritdoc} + */ + protected function requiresAuthentication(Request $request) + { + if ($this->options['post_only'] && !$request->isMethod('post')) { + return false; + } + + return parent::requiresAuthentication($request); + } + + /** * {@inheritdoc} */ protected function attemptAuthentication(Request $request) diff --git a/Http/HttpUtils.php b/Http/HttpUtils.php index f62f84d..1c87e77 100644 --- a/Http/HttpUtils.php +++ b/Http/HttpUtils.php @@ -15,7 +15,8 @@ use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Routing\Matcher\UrlMatcherInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; @@ -26,16 +27,19 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException; */ class HttpUtils { - private $router; + private $urlGenerator; + private $urlMatcher; /** * Constructor. * - * @param RouterInterface $router An RouterInterface instance + * @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance + * @param UrlMatcherInterface $urlMatcher A UrlMatcherInterface instance */ - public function __construct(RouterInterface $router = null) + public function __construct(UrlGeneratorInterface $urlGenerator = null, UrlMatcherInterface $urlMatcher = null) { - $this->router = $router; + $this->urlGenerator = $urlGenerator; + $this->urlMatcher = $urlMatcher; } /** @@ -49,13 +53,7 @@ class HttpUtils */ public function createRedirectResponse(Request $request, $path, $status = 302) { - if ('/' === $path[0]) { - $path = $request->getUriForPath($path); - } elseif (0 !== strpos($path, 'http')) { - $path = $this->generateUrl($path, true); - } - - return new RedirectResponse($path, $status); + return new RedirectResponse($this->generateUri($request, $path), $status); } /** @@ -68,14 +66,7 @@ class HttpUtils */ public function createRequest(Request $request, $path) { - if ($path && '/' !== $path[0] && 0 !== strpos($path, 'http')) { - $path = $this->generateUrl($path, true); - } - if (0 !== strpos($path, 'http')) { - $path = $request->getUriForPath($path); - } - - $newRequest = Request::create($path, 'get', array(), $request->cookies->all(), array(), $request->server->all()); + $newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all()); if ($session = $request->getSession()) { $newRequest->setSession($session); } @@ -97,7 +88,7 @@ class HttpUtils * Checks that a given path matches the Request. * * @param Request $request A Request instance - * @param string $path A path (an absolute path (/foo) or a route name (foo)) + * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo)) * * @return Boolean true if the path is the same as the one from the Request, false otherwise */ @@ -105,7 +96,7 @@ class HttpUtils { if ('/' !== $path[0]) { try { - $parameters = $this->router->match($request->getPathInfo()); + $parameters = $this->urlMatcher->match($request->getPathInfo()); return $path === $parameters['_route']; } catch (MethodNotAllowedException $e) { @@ -118,12 +109,33 @@ class HttpUtils return $path === $request->getPathInfo(); } + /** + * Generates a URI, based on the given path or absolute URL. + * + * @param Request $request A Request instance + * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo)) + * + * @return string An absolute URL + */ + public function generateUri($request, $path) + { + if (0 === strpos($path, 'http') || !$path) { + return $path; + } + + if ('/' === $path[0]) { + return $request->getUriForPath($path); + } + + return $this->generateUrl($path, true); + } + private function generateUrl($route, $absolute = false) { - if (null === $this->router) { - throw new \LogicException('You must provide a RouterInterface instance to be able to use routes.'); + if (null === $this->urlGenerator) { + throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.'); } - return $this->router->generate($route, array(), $absolute); + return $this->urlGenerator->generate($route, array(), $absolute); } } diff --git a/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php index e211da4..8bff354 100644 --- a/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php +++ b/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php @@ -35,13 +35,13 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase */ public function testRetrieveUserWhenUsernameIsNotFound() { - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); + $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); $userProvider->expects($this->once()) ->method('loadUserByUsername') - ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false))) + ->will($this->throwException($this->getMock('Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException', null, array(), '', false))) ; - $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface')); + $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface')); $method = new \ReflectionMethod($provider, 'retrieveUser'); $method->setAccessible(true); @@ -53,13 +53,13 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase */ public function testRetrieveUserWhenAnExceptionOccurs() { - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); + $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); $userProvider->expects($this->once()) ->method('loadUserByUsername') ->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false))) ; - $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface')); + $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface')); $method = new \ReflectionMethod($provider, 'retrieveUser'); $method->setAccessible(true); @@ -68,19 +68,19 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase public function testRetrieveUserReturnsUserFromTokenOnReauthentication() { - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); + $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); $userProvider->expects($this->never()) ->method('loadUserByUsername') ; - $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $token = $this->getSupportedToken(); $token->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)) ; - $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface')); + $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface')); $reflection = new \ReflectionMethod($provider, 'retrieveUser'); $reflection->setAccessible(true); $result = $reflection->invoke($provider, null, $token); @@ -90,15 +90,15 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase public function testRetrieveUser() { - $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); + $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); $userProvider->expects($this->once()) ->method('loadUserByUsername') ->will($this->returnValue($user)) ; - $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface')); + $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface')); $method = new \ReflectionMethod($provider, 'retrieveUser'); $method->setAccessible(true); @@ -110,17 +110,55 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase */ public function testCheckAuthenticationWhenCredentialsAreEmpty() { - $provider = $this->getProvider(); + $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface'); + $encoder + ->expects($this->never()) + ->method('isPasswordValid') + ; + + $provider = $this->getProvider(false, false, $encoder); $method = new \ReflectionMethod($provider, 'checkAuthentication'); $method->setAccessible(true); $token = $this->getSupportedToken(); - $token->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue('')) + $token + ->expects($this->once()) + ->method('getCredentials') + ->will($this->returnValue('')) + ; + + $method->invoke( + $provider, + $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), + $token + ); + } + + public function testCheckAuthenticationWhenCredentialsAre0() + { + $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface'); + $encoder + ->expects($this->once()) + ->method('isPasswordValid') + ->will($this->returnValue(true)) + ; + + $provider = $this->getProvider(false, false, $encoder); + $method = new \ReflectionMethod($provider, 'checkAuthentication'); + $method->setAccessible(true); + + $token = $this->getSupportedToken(); + $token + ->expects($this->once()) + ->method('getCredentials') + ->will($this->returnValue('0')) ; - $method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token); + $method->invoke( + $provider, + $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), + $token + ); } /** @@ -128,7 +166,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase */ public function testCheckAuthenticationWhenCredentialsAreNotValid() { - $encoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); + $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface'); $encoder->expects($this->once()) ->method('isPasswordValid') ->will($this->returnValue(false)) @@ -144,7 +182,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue('foo')) ; - $method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token); + $method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token); } /** @@ -152,7 +190,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase */ public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged() { - $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $user->expects($this->once()) ->method('getPassword') ->will($this->returnValue('foo')) @@ -163,7 +201,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase ->method('getUser') ->will($this->returnValue($user)); - $dbUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $dbUser->expects($this->once()) ->method('getPassword') ->will($this->returnValue('newFoo')) @@ -177,7 +215,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials() { - $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $user->expects($this->once()) ->method('getPassword') ->will($this->returnValue('foo')) @@ -188,7 +226,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase ->method('getUser') ->will($this->returnValue($user)); - $dbUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); + $dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); $dbUser->expects($this->once()) ->method('getPassword') ->will($this->returnValue('foo')) @@ -202,7 +240,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase public function testCheckAuthentication() { - $encoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); + $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface'); $encoder->expects($this->once()) ->method('isPasswordValid') ->will($this->returnValue(true)) @@ -218,12 +256,12 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue('foo')) ; - $method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token); + $method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token); } protected function getSupportedToken() { - $mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false); + $mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false); $mock ->expects($this->any()) ->method('getProviderKey') @@ -235,7 +273,7 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null) { - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); + $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); if (false !== $user) { $userProvider->expects($this->once()) ->method('loadUserByUsername') @@ -244,14 +282,14 @@ class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase } if (false === $userChecker) { - $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); + $userChecker = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'); } if (null === $passwordEncoder) { $passwordEncoder = new PlaintextPasswordEncoder(); } - $encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); + $encoderFactory = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'); $encoderFactory ->expects($this->any()) ->method('getEncoder') diff --git a/Tests/Core/Encoder/EncoderFactoryTest.php b/Tests/Core/Encoder/EncoderFactoryTest.php index a060809..2e55a4b 100644 --- a/Tests/Core/Encoder/EncoderFactoryTest.php +++ b/Tests/Core/Encoder/EncoderFactoryTest.php @@ -13,6 +13,8 @@ namespace Symfony\Component\Security\Tests\Core\Encoder; use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; use Symfony\Component\Security\Core\Encoder\EncoderFactory; +use Symfony\Component\Security\Core\User\User; +use Symfony\Component\Security\Core\User\UserInterface; class EncoderFactoryTest extends \PHPUnit_Framework_TestCase { @@ -37,7 +39,56 @@ class EncoderFactoryTest extends \PHPUnit_Framework_TestCase $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface')); $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + $encoder = $factory->getEncoder(new User('user', 'pass')); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); } + + public function testGetEncoderWithClassName() + { + $factory = new EncoderFactory(array( + 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'), + )); + + $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser'); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + } + + public function testGetEncoderConfiguredForConcreteClassWithService() + { + $factory = new EncoderFactory(array( + 'Symfony\Component\Security\Core\User\User' => new MessageDigestPasswordEncoder('sha1'), + )); + + $encoder = $factory->getEncoder(new User('user', 'pass')); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + } + + public function testGetEncoderConfiguredForConcreteClassWithClassName() + { + $factory = new EncoderFactory(array( + 'Symfony\Component\Security\Tests\Core\Encoder\SomeUser' => new MessageDigestPasswordEncoder('sha1'), + )); + + $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser'); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + } +} + +class SomeUser implements UserInterface +{ + public function getRoles() {} + public function getPassword() {} + public function getSalt() {} + public function getUsername() {} + public function eraseCredentials() {} +} + +class SomeChildUser extends SomeUser +{ } diff --git a/Tests/Http/HttpUtilsTest.php b/Tests/Http/HttpUtilsTest.php index ff6c241..a30051f 100644 --- a/Tests/Http/HttpUtilsTest.php +++ b/Tests/Http/HttpUtilsTest.php @@ -30,7 +30,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase public function testCreateRedirectResponse() { - $utils = new HttpUtils($this->getRouter()); + $utils = new HttpUtils($this->getUrlGenerator()); // absolute path $response = $utils->createRedirectResponse($this->getRequest(), '/foobar'); @@ -42,14 +42,14 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($response->isRedirect('http://symfony.com/')); // route name - $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock()); - $router + $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface')); + $urlGenerator ->expects($this->any()) ->method('generate') ->with('foobar', array(), true) ->will($this->returnValue('http://localhost/foo/bar')) ; - $router + $urlGenerator ->expects($this->any()) ->method('getContext') ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext'))) @@ -60,7 +60,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase public function testCreateRequest() { - $utils = new HttpUtils($this->getRouter()); + $utils = new HttpUtils($this->getUrlGenerator()); // absolute path $request = $this->getRequest(); @@ -72,13 +72,13 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $subRequest->server->get('Foo')); // route name - $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock()); - $router + $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface')); + $urlGenerator ->expects($this->once()) ->method('generate') ->will($this->returnValue('/foo/bar')) ; - $router + $urlGenerator ->expects($this->any()) ->method('getContext') ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext'))) @@ -93,55 +93,55 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase public function testCheckRequestPath() { - $utils = new HttpUtils($this->getRouter()); + $utils = new HttpUtils($this->getUrlGenerator()); $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/')); $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo')); - $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); - $router + $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); + $urlMatcher ->expects($this->any()) ->method('match') ->will($this->throwException(new ResourceNotFoundException())) ; - $utils = new HttpUtils($router); + $utils = new HttpUtils(null, $urlMatcher); $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar')); - $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); - $router + $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); + $urlMatcher ->expects($this->any()) ->method('match') ->will($this->returnValue(array('_route' => 'foobar'))) ; - $utils = new HttpUtils($router); + $utils = new HttpUtils(null, $urlMatcher); $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar')); } /** * @expectedException \RuntimeException */ - public function testCheckRequestPathWithRouterLoadingException() + public function testCheckRequestPathWithUrlMatcherLoadingException() { - $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); - $router + $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); + $urlMatcher ->expects($this->any()) ->method('match') ->will($this->throwException(new \RuntimeException())) ; - $utils = new HttpUtils($router); + $utils = new HttpUtils(null, $urlMatcher); $utils->checkRequestPath($this->getRequest(), 'foobar'); } - private function getRouter() + private function getUrlGenerator() { - $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); - $router + $urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); + $urlGenerator ->expects($this->any()) ->method('generate') ->will($this->returnValue('/foo/bar')) ; - return $router; + return $urlGenerator; } private function getRequest($path = '/') diff --git a/composer.json b/composer.json index 20308a0..17b3e15 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "require-dev": { "symfony/form": "2.1.*", "symfony/routing": "2.1.*", + "symfony/validator": "2.1.*", "doctrine/common": ">=2.2,<2.4-dev", "doctrine/dbal": ">=2.2,<2.4-dev" }, @@ -31,6 +32,7 @@ "symfony/class-loader": "self.version", "symfony/finder": "self.version", "symfony/form": "self.version", + "symfony/validator": "self.version", "symfony/routing": "self.version", "doctrine/dbal": "to use the built-in ACL implementation" }, |