LdapAdapterFactory.php 2.35 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

namespace UnicaenAuth\Authentication\Adapter;

use Interop\Container\ContainerInterface;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
9
use Laminas\Authentication\Storage\Session;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

class LdapAdapterFactory
{
    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param array|null $options
     * @return Ldap
     */
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null)
    {
        $adapter = new Ldap();
        $adapter->setStorage(new Session(Ldap::class));

        $this->injectDependencies($adapter, $container);

//        /** @var EventManager $eventManager */
//        $eventManager = $container->get(EventManager::class);
//        $adapter->setEventManager($eventManager);
//        $userService = $container->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
//        $eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100);
//        $eventManager->attach('clear', function() use ($adapter){
//            $adapter->getStorage()->clear();
//        });

        return $adapter;
    }

    /**
     * @param Ldap $adapter
     * @param ContainerInterface $container
     */
    private function injectDependencies(Ldap $adapter, ContainerInterface $container)
    {
        /** @var User $userService */
        $userService = $container->get('unicaen-auth_user_service');
        $adapter->setUserService($userService);

        /** @var LdapPeopleMapper $ldapPeopleMapper */
        $ldapPeopleMapper = $container->get('ldap_people_mapper');
        $adapter->setLdapPeopleMapper($ldapPeopleMapper);

        $options = array_merge(
            $container->get('zfcuser_module_options')->toArray(),
            $container->get('unicaen-auth_module_options')->toArray());
        $moduleOptions = new ModuleOptions($options);
        $adapter->setModuleOptions($moduleOptions);

        $substitut = $moduleOptions->getLdap()['type'] ?? null;
        if ($substitut !== null) {
            $adapter->setType($substitut);
        }

        /** @var \UnicaenApp\Options\ModuleOptions $appModuleOptions */
        $appModuleOptions = $container->get('unicaen-app_module_options');
        $adapter->setAppModuleOptions($appModuleOptions);
    }
}