Module.php 5.05 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
<?php

namespace UnicaenAuth;

Bertrand Gauthier's avatar
Bertrand Gauthier committed
5
6
7
8
use UnicaenAuth\Authentication\Adapter\Cas as CasAdapter;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\ShibService;
use Zend\EventManager\EventInterface;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
9
10
11
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
12
use Zend\ModuleManager\ModuleManager;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
16
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Helper\Navigation;
use ZfcUser\Form\Login;
use ZfcUser\Form\LoginFilter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
17
18
19

/**
 * Point d'entrée du module d'authentification Unicaen.
20
 *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
21
22
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
23
class Module implements AutoloaderProviderInterface, ConfigProviderInterface, ServiceProviderInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
24
25
{
    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
26
27
28
29
30
     * @var ModuleOptions
     */
    private $options;

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
     * @return array
     * @see ConfigProviderInterface
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /**
     * @return array
     * @see AutoloaderProviderInterface
     */
    public function getAutoloaderConfig()
    {
45
46
        return [
            'Zend\Loader\ClassMapAutoloader' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
47
                __DIR__ . '/autoload_classmap.php',
48
49
50
            ],
            'Zend\Loader\StandardAutoloader' => [
                'namespaces' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
51
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
52
53
54
                ],
            ],
        ];
Bertrand Gauthier's avatar
Bertrand Gauthier committed
55
56
57
58
59
    }

    /**
     * This method is called once the MVC bootstrapping is complete,
     * after the "loadModule.post" event, once $application->bootstrap() is called.
60
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
61
     * @param EventInterface $e
62
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
63
64
     * @see BootstrapListenerInterface
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
65
    public function onBootstrap(EventInterface $e)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
66
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
67
        /* @var \Zend\Mvc\MvcEvent $e */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
68
69
        $application = $e->getApplication();
        /* @var $services \Zend\ServiceManager\ServiceManager */
70
        $services = $application->getServiceManager();
71

72
73
        if ($services->has('BjyAuthorize\Service\Authorize')) {
            // transmission des ACL aux aides de vue de navigation
74
75
            $authorizeService = $services->get('BjyAuthorize\Service\Authorize');
            /* @var $authorizeService \BjyAuthorize\Service\Authorize */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
76
77
            Navigation::setDefaultAcl($authorizeService->getAcl());
            Navigation::setDefaultRole($authorizeService->getIdentity());
78
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
79

Bertrand Gauthier's avatar
Bertrand Gauthier committed
80
81
        /* @var $options ModuleOptions */
        $this->options = $services->get('unicaen-auth_module_options');
82

Bertrand Gauthier's avatar
Bertrand Gauthier committed
83
        $this->reconfigureRoutesForAuth($services);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
84
85
    }

Bertrand Gauthier's avatar
Bertrand Gauthier committed
86
87
88
89
90
    /**
     * @param ServiceLocatorInterface $sl
     */
    private function reconfigureRoutesForAuth(ServiceLocatorInterface $sl)
    {
91
        /* @var $router \Zend\Router\Http\TreeRouteStack */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
92
93
94
95
96
97
98
99
100
        $router = $sl->get('router');

        // si l'auth CAS est activée, modif de la route de connexion pour zapper le formulaire d'auth maison.
        $isCasEnable = (bool) $this->options->getCas();
        if ($isCasEnable && php_sapi_name() !== 'cli') {
            /** @var CasAdapter $casAdapter */
            $casAdapter = $sl->get('UnicaenAuth\Authentication\Adapter\Cas');
            $casAdapter->reconfigureRoutesForCasAuth($router);
        }
101

Bertrand Gauthier's avatar
Bertrand Gauthier committed
102
103
104
105
106
107
108
109
110
        // si l'auth Shibboleth est activée, modif de la route de déconnexion pour réaliser la déconnexion Shibboleth.
        $shibOptions = $this->options->getShibboleth();
        $isShibEnable = array_key_exists('enable', $shibOptions) && (bool) $shibOptions['enable'];
        if ($isShibEnable && php_sapi_name() !== 'cli') {
            /** @var ShibService $shibService */
            $shibService = $sl->get(ShibService::class);
            $shibService->reconfigureRoutesForShibAuth($router);
        }
    }
111

Bertrand Gauthier's avatar
Bertrand Gauthier committed
112
113
114
115
116
117
    /**
     * @return array
     * @see ServiceProviderInterface
     */
    public function getServiceConfig()
    {
118
        return [
119
120
121
122
123
124
125
            //========== repris du module zf-commons/zfc-user-doctrine-orm abandonné =========
            'aliases' => array(
                'zfcuser_doctrine_em' => 'Doctrine\ORM\EntityManager',
            ),
            //===========================================


126
            'factories' => [
127
128
129
130
131
132
133
                //========== repris du module zf-commons/zfc-user-doctrine-orm abandonné =========
                'zfcuser_module_options' => function ($sm) {
                    $config = $sm->get('Configuration');
                    return new Options\ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());
                },
                //===========================================

134
                // verrue pour forcer le label de l'identifiant qqsoit l'options 'auth_identity_fields'
135
                'zfcuser_login_form' => function ($sm) {
136
                    $options = $sm->get('zfcuser_module_options');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
137
138
                    $form    = new Login(null, $options);
                    $form->setInputFilter(new LoginFilter($options));
139
                    $form->get('identity')->setLabel("Username");
140

141
142
                    return $form;
                },
143
144
            ],
        ];
145
    }
146
}