Module.php 5.12 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;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
12
13
14
15
16
use Zend\ServiceManager\Exception\ServiceNotFoundException;
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
        // transmission des ACL aux aides de vue de navigation
73
        try {
74
75
            $authorizeService = $services->get('BjyAuthorize\Service\Authorize');
            /* @var $authorizeService \BjyAuthorize\Service\Authorize */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
76
77
78
            Navigation::setDefaultAcl($authorizeService->getAcl());
            Navigation::setDefaultRole($authorizeService->getIdentity());
        } catch (ServiceNotFoundException $snfe) {
79
80
            // pas de module BjyAuthorize : pas d'ACL
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
81

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

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

Bertrand Gauthier's avatar
Bertrand Gauthier committed
88
89
90
91
92
    /**
     * @param ServiceLocatorInterface $sl
     */
    private function reconfigureRoutesForAuth(ServiceLocatorInterface $sl)
    {
93
        /* @var $router \Zend\Router\Http\TreeRouteStack */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
94
95
96
97
98
99
100
101
102
        $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);
        }
103

Bertrand Gauthier's avatar
Bertrand Gauthier committed
104
105
106
107
108
109
110
111
112
        // 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);
        }
    }
113

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


128
            'factories' => [
129
130
131
132
133
134
135
                //========== 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());
                },
                //===========================================

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

143
144
                    return $form;
                },
145
146
            ],
        ];
147
    }
148
}