Skip to content

Commit 6d02c6c

Browse files
devtronicXWB
authored andcommitted
Add EmailProvider for email only login (FriendsOfSymfony#2781)
* Add EmailProvider for email only login Feature Request: FriendsOfSymfony#2768 * Fix TestCase for EmailProvider
1 parent 47a4d12 commit 6d02c6c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

Resources/config/security.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<argument type="service" id="fos_user.user_manager" />
3434
</service>
3535

36+
<service id="fos_user.user_provider.email" class="FOS\UserBundle\Security\EmailProvider" public="false">
37+
<argument type="service" id="fos_user.user_manager" />
38+
</service>
39+
3640
<service id="fos_user.security.controller" class="FOS\UserBundle\Controller\SecurityController" public="true">
3741
<argument type="service" id="security.csrf.token_manager" on-invalid="null" />
3842
<call method="setContainer">

Security/EmailProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Security;
13+
14+
class EmailProvider extends UserProvider
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function findUser($username)
20+
{
21+
return $this->userManager->findUserByEmail($username);
22+
}
23+
}

Tests/Security/EmailProviderTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Tests\Security;
13+
14+
use FOS\UserBundle\Security\EmailProvider;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class EmailProviderTest extends TestCase
18+
{
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $userManager;
23+
24+
/**
25+
* @var UserProvider
26+
*/
27+
private $userProvider;
28+
29+
protected function setUp()
30+
{
31+
$this->userManager = $this->getMockBuilder('FOS\UserBundle\Model\UserManagerInterface')->getMock();
32+
$this->userProvider = new EmailProvider($this->userManager);
33+
}
34+
35+
public function testLoadUserByUsername()
36+
{
37+
$user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')->getMock();
38+
$this->userManager->expects($this->once())
39+
->method('findUserByEmail')
40+
->with('foobar')
41+
->will($this->returnValue($user));
42+
43+
$this->assertSame($user, $this->userProvider->loadUserByUsername('foobar'));
44+
}
45+
46+
/**
47+
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
48+
*/
49+
public function testLoadUserByInvalidUsername()
50+
{
51+
$this->userManager->expects($this->once())
52+
->method('findUserByEmail')
53+
->with('foobar')
54+
->will($this->returnValue(null));
55+
56+
$this->userProvider->loadUserByUsername('foobar');
57+
}
58+
59+
public function testRefreshUserBy()
60+
{
61+
$user = $this->getMockBuilder('FOS\UserBundle\Model\User')
62+
->setMethods(array('getId'))
63+
->getMock();
64+
65+
$user->expects($this->once())
66+
->method('getId')
67+
->will($this->returnValue('123'));
68+
69+
$refreshedUser = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')->getMock();
70+
$this->userManager->expects($this->once())
71+
->method('findUserBy')
72+
->with(array('id' => '123'))
73+
->will($this->returnValue($refreshedUser));
74+
75+
$this->userManager->expects($this->atLeastOnce())
76+
->method('getClass')
77+
->will($this->returnValue(get_class($user)));
78+
79+
$this->assertSame($refreshedUser, $this->userProvider->refreshUser($user));
80+
}
81+
82+
/**
83+
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
84+
*/
85+
public function testRefreshDeleted()
86+
{
87+
$user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
88+
$this->userManager->expects($this->once())
89+
->method('findUserBy')
90+
->will($this->returnValue(null));
91+
92+
$this->userManager->expects($this->atLeastOnce())
93+
->method('getClass')
94+
->will($this->returnValue(get_class($user)));
95+
96+
$this->userProvider->refreshUser($user);
97+
}
98+
99+
/**
100+
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
101+
*/
102+
public function testRefreshInvalidUser()
103+
{
104+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
105+
$this->userManager->expects($this->any())
106+
->method('getClass')
107+
->will($this->returnValue(get_class($user)));
108+
109+
$this->userProvider->refreshUser($user);
110+
}
111+
112+
/**
113+
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
114+
*/
115+
public function testRefreshInvalidUserClass()
116+
{
117+
$user = $this->getMockBuilder('FOS\UserBundle\Model\User')->getMock();
118+
$providedUser = $this->getMockBuilder('FOS\UserBundle\Tests\TestUser')->getMock();
119+
120+
$this->userManager->expects($this->atLeastOnce())
121+
->method('getClass')
122+
->will($this->returnValue(get_class($user)));
123+
124+
$this->userProvider->refreshUser($providedUser);
125+
}
126+
}

0 commit comments

Comments
 (0)