Module.php 4.73 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 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
26
     * @return array
     * @see ConfigProviderInterface
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

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

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

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

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

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

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