Module.php 4.82 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
6
7
8
9
10
<?php

namespace UnicaenAuth;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;

/**
 * Point d'entrée du module d'authentification Unicaen.
11
 *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
12
13
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
14
class Module implements AutoloaderProviderInterface, ConfigProviderInterface, ServiceProviderInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
15
16
{
    /**
17
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
18
19
20
21
22
23
24
25
     * @return array
     * @see ConfigProviderInterface
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

26
27


Bertrand Gauthier's avatar
Bertrand Gauthier committed
28
    /**
29
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
30
31
32
33
34
     * @return array
     * @see AutoloaderProviderInterface
     */
    public function getAutoloaderConfig()
    {
35
36
        return [
            'Zend\Loader\ClassMapAutoloader' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
37
                __DIR__ . '/autoload_classmap.php',
38
39
40
            ],
            'Zend\Loader\StandardAutoloader' => [
                'namespaces' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
41
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
42
43
44
                ],
            ],
        ];
Bertrand Gauthier's avatar
Bertrand Gauthier committed
45
46
    }

47
48


Bertrand Gauthier's avatar
Bertrand Gauthier committed
49
50
51
    /**
     * This method is called once the MVC bootstrapping is complete,
     * after the "loadModule.post" event, once $application->bootstrap() is called.
52
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
53
     * @param EventInterface $e
54
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
55
56
     * @see BootstrapListenerInterface
     */
57
58
    public function onBootstrap(\Zend\EventManager\EventInterface $e)
        /* @var \Zend\Mvc\MvcEvent $e */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
59
60
61
    {
        $application = $e->getApplication();
        /* @var $services \Zend\ServiceManager\ServiceManager */
62
        $services = $application->getServiceManager();
63

64
        // transmission des ACL aux aides de vue de navigation
65
        try {
66
67
            $authorizeService = $services->get('BjyAuthorize\Service\Authorize');
            /* @var $authorizeService \BjyAuthorize\Service\Authorize */
68
69
            \Zend\View\Helper\Navigation::setDefaultAcl($authorizeService->getAcl());
            \Zend\View\Helper\Navigation::setDefaultRole($authorizeService->getIdentity());
70
        } catch (\Zend\ServiceManager\Exception\ServiceNotFoundException $snfe) {
71
72
            // pas de module BjyAuthorize : pas d'ACL
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
73
74
75

        /* @var $options Options\ModuleOptions */
        $options = $services->get('unicaen-auth_module_options');
76

Bertrand Gauthier's avatar
Bertrand Gauthier committed
77
        // si l'auth CAS est demandée, modif de la route de connexion pour zapper le formulaire
78
        if ($options->getCas() && php_sapi_name() !== 'cli') {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
79
80
            /* @var $router \Zend\Mvc\Router\Http\TreeRouteStack */
            $router = $services->get('router');
81
            $router->addRoutes([
Bertrand Gauthier's avatar
Bertrand Gauthier committed
82
                // remplace les routes existantes (cf. config du module)
83
                'zfcuser' => [
84
85
86
87
                    'type'          => 'Literal',
                    'priority'      => 1000,
                    'options'       => [
                        'route'    => '/auth',
88
                        'defaults' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
89
90
                            'controller' => 'zfcuser',
                            'action'     => 'index',
91
92
                        ],
                    ],
Bertrand Gauthier's avatar
Bertrand Gauthier committed
93
                    'may_terminate' => true,
94
95
96
                    'child_routes'  => [
                        'login'  => [
                            'type'    => 'Literal',
97
                            'options' => [
98
                                'route'    => '/connexion',
99
                                'defaults' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
100
101
                                    'controller' => 'zfcuser',
                                    'action'     => 'authenticate', // zappe l'action 'login'
102
103
104
105
                                ],
                            ],
                        ],
                        'logout' => [
106
                            'type'    => 'Literal',
107
                            'options' => [
108
                                'route'    => '/deconnexion',
109
                                'defaults' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
110
111
                                    'controller' => 'zfcuser',
                                    'action'     => 'logout',
112
113
114
115
                                ],
                            ],
                        ],
                    ],
116
                ],
117
            ]);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
118
119
120
        }
    }

121
122


Bertrand Gauthier's avatar
Bertrand Gauthier committed
123
    /**
124
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
125
126
127
128
129
     * @return array
     * @see ServiceProviderInterface
     */
    public function getServiceConfig()
    {
130
131
        return [
            'factories' => [
132
                // verrue pour forcer le label de l'identifiant qqsoit l'options 'auth_identity_fields'
133
                'zfcuser_login_form' => function ($sm) {
134
                    $options = $sm->get('zfcuser_module_options');
135
                    $form    = new \ZfcUser\Form\Login(null, $options);
136
137
                    $form->setInputFilter(new \ZfcUser\Form\LoginFilter($options));
                    $form->get('identity')->setLabel("Username");
138

139
140
                    return $form;
                },
141
142
            ],
        ];
143
    }
144
}