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

namespace UnicaenAuth;

5
use UnicaenAuth\Guard\PrivilegeController;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
7
8
9
10
11
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;

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

27
28


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

48
49


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

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

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

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

122
123


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

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