Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added UserLoaderInterface for loading users through Doctrine. #15947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public function loadUserByUsername($username)
if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username));
} else {
if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
if (!$this->repository instanceof UserLoaderInterface) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rephrased the clause a bit as the deprecation warning should be thrown if the UserLoaderInterface is not being implemented.

if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($this->repository)));
}

@trigger_error('Implementing loadUserByUsername from Symfony\Component\Security\Core\User\UserProviderInterface is deprecated since version 2.8 and will be removed in 3.0. Implement the Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead.', E_USER_DEPRECATED);
}

$user = $this->repository->loadUserByUsername($username);
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Bridge/Doctrine/Security/User/UserLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Security\User;

use Symfony\Component\Security\Core\User\UserInterface;

/**
* Represents a class that loads UserInterface objects from Doctrine source for the authentication system.
*
* This interface is meant to facilitate the loading of a User from Doctrine source using a custom method.
* If you want to implement your own logic of retrieving the user from Doctrine your repository should implement this
* interface.
*
* @see UserInterface
*
* @author Michal Trojanowski <[email protected]>
*/
interface UserLoaderInterface
{
/**
* Loads the user for the given username.
*
* This method must return null if the user is not found.
*
* @param string $username The username
*
* @return UserInterface|null
*/
public function loadUserByUsername($username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ public function testSupportProxy()
$this->assertTrue($provider->supportsClass(get_class($user2)));
}

public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided()
{
$repository = $this->getMock('\Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface');
$repository->expects($this->once())
->method('loadUserByUsername')
->with('name')
->willReturn(
$this->getMock('\Symfony\Component\Security\Core\User\UserInterface')
);

$provider = new EntityUserProvider(
$this->getManager($this->getObjectManager($repository)),
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
);

$provider->loadUserByUsername('name');
}

/**
* @expectedException \InvalidArgumentException
*/
public function testLoadUserByUserNameShouldDeclineInvalidInterface()
{
$repository = $this->getMock('\Symfony\Component\Security\Core\User\AdvancedUserInterface');

$provider = new EntityUserProvider(
$this->getManager($this->getObjectManager($repository)),
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
);

$provider->loadUserByUsername('name');
}

private function getManager($em, $name = null)
{
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
Expand All @@ -100,6 +133,18 @@ private function getManager($em, $name = null)
return $manager;
}

private function getObjectManager($repository)
{
$em = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
->setMethods(array('getClassMetadata', 'getRepository'))
->getMockForAbstractClass();
$em->expects($this->any())
->method('getRepository')
->willReturn($repository);

return $em;
}

private function createSchema($em)
{
$schemaTool = new SchemaTool($em);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ interface UserProviderInterface
*
* @return UserInterface
*
* @see UsernameNotFoundException
*
* @throws UsernameNotFoundException if the user is not found
*/
public function loadUserByUsername($username);
Expand Down