Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from Converia/master
Browse files Browse the repository at this point in the history
Event dispatcher
  • Loading branch information
DarwinOnLine authored Jun 25, 2020
2 parents 76ef370 + a6d879e commit b01b428
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 13 deletions.
37 changes: 37 additions & 0 deletions Event/SwitchParameterSetEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Created by PhpStorm.
* User: boellmann
* Date: 02.10.18
* Time: 14:38
*/

namespace DoL\LdapBundle\Event;


use Symfony\Component\EventDispatcher\Event;

class SwitchParameterSetEvent extends Event
{
const PARAMETERSET = 'dol_ldap.manager.switch_parameter_set';

private $parameterSet = [];

/**
* SwitchParameterSet constructor.
* @param array $parameterSet
*/
public function __construct(array $parameterSet)
{
$this->parameterSet = $parameterSet;
}

/**
* @return array
*/
public function getParameterSet(): array
{
return $this->parameterSet;
}

}
35 changes: 24 additions & 11 deletions Ldap/LdapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace DoL\LdapBundle\Ldap;

use DoL\LdapBundle\Driver\LdapDriverInterface;
use DoL\LdapBundle\Event\SwitchParameterSetEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use DoL\LdapBundle\Hydrator\HydratorInterface;

Expand All @@ -22,16 +24,21 @@ class LdapManager implements LdapManagerInterface
protected $params = [];
protected $ldapAttributes = [];
protected $ldapUsernameAttr;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* @var HydratorInterface
*/
protected $hydrator;

public function __construct(LdapDriverInterface $driver, HydratorInterface $hydrator, array $paramSets)
public function __construct(LdapDriverInterface $driver, HydratorInterface $hydrator, EventDispatcherInterface $eventDispatcher, array $paramSets)
{
$this->driver = $driver;
$this->hydrator = $hydrator;
$this->eventDispatcher = $eventDispatcher;
$this->paramSets = $paramSets;
}

Expand All @@ -47,8 +54,7 @@ public function bind(UserInterface $user, $password)
$this->driver->init($paramSet['driver']);

if (false !== $this->driver->bind($user, $password)) {
$this->params = $paramSet['user'];
$this->setLdapAttr();
$this->switchParameterSet($paramSet);

return true;
}
Expand All @@ -68,16 +74,14 @@ public function findUserByUsername($username)
} else {
foreach ($this->paramSets as $paramSet) {
$this->driver->init($paramSet['driver']);
$this->params = $paramSet['user'];
$this->setLdapAttr();
$this->switchParameterSet($paramSet);

$user = $this->findUserBy([$this->ldapUsernameAttr => $username]);
if (false !== $user && $user instanceof UserInterface) {
return $user;
}

$this->params = [];
$this->setLdapAttr();
$this->switchParameterSet([]);
}
}
}
Expand All @@ -103,16 +107,14 @@ public function findUserBy(array $criteria)
} else {
foreach ($this->paramSets as $paramSet) {
$this->driver->init($paramSet['driver']);
$this->params = $paramSet['user'];
$this->setLdapAttr();
$this->switchParameterSet($paramSet);

$user = $this->findUserBy($criteria);
if (false !== $user && $user instanceof UserInterface) {
return $user;
}

$this->params = [];
$this->setLdapAttr();
$this->switchParameterSet([]);
}
}
}
Expand Down Expand Up @@ -157,4 +159,15 @@ protected function buildFilter(array $criteria, $condition = '&')

return sprintf('(%s%s)', $condition, implode($filters));
}

private function switchParameterSet(array $parameter)
{
if (isset($parameter['user'])) {
$this->params = $parameter['user'];
} else {
$this->params = [];
}
$this->setLdapAttr();
$this->eventDispatcher->dispatch(SwitchParameterSetEvent::PARAMETERSET, new SwitchParameterSetEvent($parameter));
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
# Ldap manager
dol_ldap.ldap_manager.default:
class: "%dol_ldap.ldap_manager.class%"
arguments: [ "@dol_ldap.ldap_driver", "@dol_ldap.user_hydrator", "%dol_ldap.domains.parameters%" ]
arguments: [ "@dol_ldap.ldap_driver", "@dol_ldap.user_hydrator", '@event_dispatcher', "%dol_ldap.domains.parameters%" ]

# Ldap hydrator
dol_ldap.user_hydrator.default:
Expand Down
50 changes: 50 additions & 0 deletions Tests/Event/SwitchParameterSetEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: boellmann
* Date: 02.10.18
* Time: 14:44
*/

namespace DoL\LdapBundle\Event;


class SwitchParameterSetEventTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SwitchParameterSetEvent
*/
private $event;
/**
* @var array
*/
private $parameter;

protected function setUp()
{
$this->parameter = [
'driver' => [
// SOME ATTRIBUTES
],
'user' => [
'baseDn' => 'ou=Groups,dc=example,dc=com',
'filter' => '(attr0=value0)',
'attributes' => [
[
'ldap_attr' => 'uid',
'user_method' => 'setUsername',
],
],
],
];
$this->event = new SwitchParameterSetEvent($this->parameter);
}


public function testGetter()
{
$this->assertTrue(is_array($this->event->getParameterSet()));
$this->assertArraySubset($this->parameter,$this->event->getParameterSet());
}

}
20 changes: 19 additions & 1 deletion Tests/Ldap/LdapManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace DoL\LdapBundle\Tests\Ldap;

use DoL\LdapBundle\Event\SwitchParameterSetEvent;
use DoL\LdapBundle\Hydrator\HydratorInterface;
use DoL\LdapBundle\Ldap\LdapManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
Expand All @@ -24,6 +26,11 @@ class LdapManagerTest extends \PHPUnit_Framework_TestCase
*/
protected $hydrator;

/**
* @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventDispatcher;

/**
* @var LdapManager
*/
Expand Down Expand Up @@ -57,7 +64,9 @@ protected function setUp()

$this->hydrator = $this->getMock('DoL\LdapBundle\Hydrator\HydratorInterface');

$this->ldapManager = new LdapManager($this->driver, $this->hydrator, $this->paramSets);
$this->eventDispatcher = $this->getMock(EventDispatcherInterface::class);

$this->ldapManager = new LdapManager($this->driver, $this->hydrator, $this->eventDispatcher, $this->paramSets);
}

/**
Expand Down Expand Up @@ -142,6 +151,15 @@ public function testBind()
->method('bind')
->with($user, $this->equalTo($password))
->will($this->returnValue(true));
$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo('dol_ldap.manager.switch_parameter_set'),
$this->callback(function(SwitchParameterSetEvent $event){
$parameterSet = $event->getParameterSet();
return (is_array($parameterSet) AND isset($parameterSet['driver']));
})
);

self::assertTrue($this->ldapManager->bind($user, $password));
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"psr/log": "~1.0",
"symfony/config": "2.3 - 4",
"symfony/dependency-injection": "2.3 - 4",
"symfony/event-dispatcher": "2.3 - 4",
"symfony/polyfill-php56": "^1.6",
"symfony/security": "2.3 - 4",
"symfony/security-bundle": "2.3 - 4",
Expand Down

0 comments on commit b01b428

Please sign in to comment.