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

namespace UnicaenAuth;

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

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

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

    /**
     * This method is called once the MVC bootstrapping is complete,
     * after the "loadModule.post" event, once $application->bootstrap() is called.
49
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
50
51
52
53
54
55
56
57
     * @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();
58

59
        // transmission des ACL aux aides de vue de navigation
60
61
62
63
64
65
66
67
        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
68
69
70

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

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

    /**
117
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
118
119
120
121
122
     * @return array
     * @see ViewHelperProviderInterface
     */
    public function getViewHelperConfig()
    {
123
124
        return [
            'factories' => [
125
126
127
128
129
130
                'userConnection'    => 'UnicaenAuth\View\Helper\UserConnectionFactory',
                'userCurrent'       => 'UnicaenAuth\View\Helper\UserCurrentFactory',
                'userStatus'        => 'UnicaenAuth\View\Helper\UserStatusFactory',
                'userProfile'       => 'UnicaenAuth\View\Helper\UserProfileFactory',
                'userInfo'          => 'UnicaenAuth\View\Helper\UserInfoFactory',
                'userProfileSelect' => 'UnicaenAuth\View\Helper\UserProfileSelectFactory',
131
                'userProfileSelectRadioItem' => 'UnicaenAuth\View\Helper\UserProfileSelectRadioItemFactory',
132
133
            ],
            'invokables' => [
134
                'appConnection' => 'UnicaenAuth\View\Helper\AppConnection',
135
136
            ],
        ];
Bertrand Gauthier's avatar
Bertrand Gauthier committed
137
138
139
    }

    /**
140
     *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
141
142
143
144
145
     * @return array
     * @see ServiceProviderInterface
     */
    public function getServiceConfig()
    {
146
147
        return [
            'factories' => [
148
149
150
151
152
153
154
155
                // 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;
                },
156
157
            ],
        ];
158
      }
159
}