Skip to content

Commit a8d3d12

Browse files
mtrojanowskifabpot
authored andcommitted
Added UserLoaderInterface for loading users through Doctrine.
1 parent 1828d06 commit a8d3d12

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy