8000 feature #15947 Added UserLoaderInterface for loading users through Do… · symfony/symfony@f19a7c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f19a7c9

Browse files
committed
feature #15947 Added UserLoaderInterface for loading users through Doctrine. (mtrojanowski)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #15947). Discussion ---------- Added UserLoaderInterface for loading users through Doctrine. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #11157 | License | MIT | Doc PR | symfony/symfony-docs#4543 Based on the @weaverryan and @stof propositions from [this](#12733 (comment)) discussion I created a new interface in the Doctrine Bridge which can be used to more easily implement a repository capable of returning a User. Commits ------- a8d3d12 Added UserLoaderInterface for loading users through Doctrine.
2 parents 1828d06 + a8d3d12 commit f19a7c9

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ public function loadUserByUsername($username)
5454
if (null !== $this->property) {
5555
$user = $this->repository->findOneBy(array($this->property => $username));
5656
} else {
57-
if (!$this->repository instanceof UserProviderInterface) {
58-
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
57+
if (!$this->repository instanceof UserLoaderInterface) {
58+
if (!$this->repository instanceof UserProviderInterface) {
59+
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($this->repository)));
60+
}
61+
62+
@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);
5963
}
6064

6165
$user = $this->repository->loadUserByUsername($username);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Security\User;
13+
14+
use Symfony\Component\Security\Core\User\UserInterface;
15+
16+
/**
17+
* Represents a class that loads UserInterface objects from Doctrine source for the authentication system.
18+
*
19+
* This interface is meant to facilitate the loading of a User from Doctrine source using a custom method.
20+
* If you want to implement your own logic of retrieving the user from Doctrine your repository should implement this
21+
* interface.
22+
*
23+
* @see UserInterface
24+
*
25+
* @author Michal Trojanowski <michal@kmt-studio.pl>
26+
*/
27+
interface UserLoaderInterface
28+
{
29+
/**
30+
* Loads the user for the given username.
31+
*
32+
* This method must return null if the user is not found.
33+
*
34+
* @param string $username The username
35+
*
36+
* @return UserInterface|null
37+
*/
38+
public function loadUserByUsername($username);
39+
}

src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ public function testSupportProxy()
8989
$this->assertTrue($provider->supportsClass(get_class($user2)));
9090
}
9191

92+
public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided()
93+
{
94+
$repository = $this->getMock('\Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface');
95+
$repository->expects($this->once())
96+
->method('loadUserByUsername')
97+
->with('name')
98+
->willReturn(
99+
$this->getMock('\Symfony\Component\Security\Core\User\UserInterface')
100+
);
101+
102+
$provider = new EntityUserProvider(
103+
$this->getManager($this->getObjectManager($repository)),
104+
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
105+
);
106+
107+
$provider->loadUserByUsername('name');
108+
}
109+
110+
/**
111+
* @expectedException \InvalidArgumentException
112+
*/
113+
public function testLoadUserByUserNameShouldDeclineInvalidInterface()
114+
{
115+
$repository = $this->getMock('\Symfony\Component\Security\Core\User\AdvancedUserInterface');
116+
117+
$provider = new EntityUserProvider(
118+
$this->getManager($this->getObjectManager($repository)),
119+
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
120+
);
121+
122+
$provider->loadUserByUsername('name');
123+
}
124+
92125
private function getManager($em, $name = null)
93126
{
94127
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
@@ -100,6 +133,18 @@ private function getManager($em, $name = null)
100133
return $manager;
101134
}
102135

136+
private function getObjectManager($repository)
137+
{
138+
$em = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
139+
->setMethods(array('getClassMetadata', 'getRepository'))
140+
->getMockForAbstractClass();
141+
$em->expects($this->any())
142+
->method('getRepository')
143+
->willReturn($repository);
144+
145+
return $em;
146+
}
147+
103148
private function createSchema($em)
104149
{
105150
$schemaTool = new SchemaTool($em);

src/Symfony/Component/Security/Core/User/UserProviderInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ interface UserProviderInterface
4343
*
4444
* @return UserInterface
4545
*
46-
* @see UsernameNotFoundException
47-
*
4846
* @throws UsernameNotFoundException if the user is not found
4947
*/
5048
public function loadUserByUsername($username);

0 commit comments

Comments
 (0)
0