Created
June 3, 2014 18:53
-
-
Save baldurrensch/a877253523509e93d778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<service id="acme_oauth_server_bundle.event_listener.update_user_with_ldap_user" | |
class="Acme\OAuthServerBundle\EventListener\UpdateUserWithLdapListener"> | |
<argument type="service" id="fr3d_ldap.ldap_manager"/> | |
<argument type="service" id="fos_user.user_manager" /> | |
<tag name="kernel.event_listener" event="security.interactive_login" method="onLogin" /> | |
</service> | |
</services> | |
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\OAuthServerBundle\EventListener; | |
use FOS\UserBundle\Model\UserManagerInterface; | |
use FR3D\LdapBundle\Ldap\LdapManagerInterface; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | |
class UpdateUserWithLdapListener | |
{ | |
/** | |
* @var LdapManagerInterface | |
*/ | |
private $ldapManager; | |
/** | |
* @var UserManagerInterface | |
*/ | |
private $userManager; | |
public function __construct(LdapManagerInterface $ldapManager, UserManagerInterface $userManager) | |
{ | |
$this->ldapManager = $ldapManager; | |
$this->userManager = $userManager; | |
} | |
public function onLogin(InteractiveLoginEvent $event) | |
{ | |
$username = $event->getAuthenticationToken()->getUser()->getUsername(); | |
$authUser = $event->getAuthenticationToken()->getUser(); | |
if (null === $authUser->getId()) { | |
return; | |
} | |
$ldapUser = $this->ldapManager->findUserByUsername($username); | |
if (empty($ldapUser)) { | |
return; | |
} | |
$changed = false; | |
if (($roles = $ldapUser->getRoles()) != $authUser->getRoles()) { | |
$changed = true; | |
$authUser->setRoles($roles); | |
} | |
if (($realName = $ldapUser->getRealName()) != $authUser->getRealName()) { | |
$changed = true; | |
$authUser->setRealName($realName); | |
} | |
if ($changed) { | |
$this->userManager->updateUser($authUser); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\OAuthServerBundle\Tests\EventListener; | |
use FOS\UserBundle\Model\UserManagerInterface; | |
use FR3D\LdapBundle\Ldap\LdapManagerInterface; | |
use Acme\OAuthServerBundle\Entity\User; | |
use Acme\OAuthServerBundle\EventListener\UpdateUserWithLdapListener; | |
use Prophecy\PhpUnit\ProphecyTestCase; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Role\RoleInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | |
class UpdateUserWithLdapListenerTest extends ProphecyTestCase | |
{ | |
private $userManager; | |
private $ldapManager; | |
public function testNewUser() | |
{ | |
$event = new InteractiveLoginEvent(new Request(), new TestToken()); | |
$this->ldapManager->findUserByUsername()->shouldNotBeCalled(); | |
$this->userManager->updateUser()->shouldNotBeCalled(); | |
$this->onLogin($event); | |
} | |
public function testNoLdapUser() | |
{ | |
$event = new InteractiveLoginEvent(new Request(), new TestToken(new TestUser(1))); | |
$this->ldapManager->findUserByUsername('testuser')->willReturn(null); | |
$this->userManager->updateUser()->shouldNotBeCalled(); | |
$this->onLogin($event); | |
} | |
public function testNoChanges() | |
{ | |
$event = new InteractiveLoginEvent(new Request(), new TestToken(new TestUser(1))); | |
$ldapUser = new TestUser(1); | |
$this->ldapManager->findUserByUsername('testuser')->willReturn($ldapUser); | |
$this->userManager->updateUser()->shouldNotBeCalled(); | |
$this->onLogin($event); | |
} | |
/** | |
* @dataProvider getChangedUsers | |
*/ | |
public function testChanges($ldapUser) | |
{ | |
$event = new InteractiveLoginEvent(new Request(), new TestToken(new TestUser(1))); | |
$this->ldapManager->findUserByUsername('testuser')->willReturn($ldapUser); | |
$this->userManager->updateUser($ldapUser)->shouldBeCalled(); | |
$this->onLogin($event); | |
} | |
public function getChangedUsers() | |
{ | |
$user1 = new TestUser(1); | |
$user1->setRoles(['CHANGED']); | |
$user2 = new TestUser(1); | |
$user2->setRealName('Changed'); | |
$user3 = new TestUser(1); | |
$user3->setEmail('bla'); | |
return [ | |
[$user1], | |
[$user2], | |
]; | |
} | |
protected function setUp() | |
{ | |
parent::setUp(); | |
$this->ldapManager = $this->prophesize(LdapManagerInterface::CLASS); | |
$this->userManager = $this->prophesize(UserManagerInterface::CLASS); | |
} | |
/** | |
* @param $event | |
*/ | |
private function onLogin(InteractiveLoginEvent $event) | |
{ | |
$listener = new UpdateUserWithLdapListener( | |
$this->ldapManager->reveal(), | |
$this->userManager->reveal() | |
); | |
$listener->onLogin($event); | |
} | |
} | |
class TestUser extends User | |
{ | |
public function __construct($id) | |
{ | |
$this->id = $id; | |
$this->username = 'testuser'; | |
} | |
} | |
class TestToken implements TokenInterface | |
{ | |
public function __construct(UserInterface $user = null) | |
{ | |
$this->user = (null === $user) ? new TestUser(null) : $user; | |
} | |
public function serialize() | |
{ | |
} | |
public function unserialize($serialized) | |
{ | |
} | |
public function __toString() | |
{ | |
} | |
public function getRoles() | |
{ | |
} | |
public function getCredentials() | |
{ | |
} | |
public function getUser() | |
{ | |
return $this->user; | |
} | |
public function setUser($user) | |
{ | |
} | |
public function getUsername() | |
{ | |
} | |
public function isAuthenticated() | |
{ | |
} | |
public function setAuthenticated($isAuthenticated) | |
{ | |
} | |
public function eraseCredentials() | |
{ | |
} | |
public function getAttributes() | |
{ | |
} | |
public function setAttributes(array $attributes) | |
{ | |
} | |
public function hasAttribute($name) | |
{ | |
} | |
public function getAttribute($name) | |
{ | |
} | |
public function setAttribute($name, $value) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment