AuthController.php 2.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace UnicaenAuth\Controller;

use UnicaenApp\Exception\RuntimeException;
use UnicaenAuth\Service\Traits\ShibServiceAwareTrait;
use UnicaenAuth\Service\Traits\UserServiceAwareTrait;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Exception\ExceptionInterface;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
12
use ZfcUser\Controller\Plugin\ZfcUserAuthentication;
13
14
15
16

/**
 * Classe ajoutée lors de l'implémentation de l'auth Shibboleth.
 *
Bertrand Gauthier's avatar
Bertrand Gauthier committed
17
 * @method ZfcUserAuthentication zfcUserAuthentication()
18
19
20
21
22
23
24
25
26
27
28
29
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class AuthController extends AbstractActionController
{
    use ShibServiceAwareTrait;
    use UserServiceAwareTrait;

    /**
     * @return Response|array
     */
    public function shibbolethAction()
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
30
31
32
        $operation = $this->params()->fromRoute('operation');

        if ($operation === 'deconnexion') {
33
34
35
36
37
38
39
40
41
42
43
44
45
            // déconnexion applicative quoiqu'il arrive
            $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
            $this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
            $this->zfcUserAuthentication()->getAuthService()->clearIdentity();

            // déconnexion Shibboleth le cas échéant
            if ($this->shibService->isShibbolethEnable()) {
                $homeUrl = $this->url()->fromRoute('home', [], ['force_canonical' => true]);
                $returnAbsoluteUrl = $this->params()->fromQuery('return', $homeUrl);
                return $this->redirect()->toUrl($this->shibService->getLogoutUrl($returnAbsoluteUrl));
            } else {
                return []; // une page d'aide s'affichera
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
46
        }
47

Bertrand Gauthier's avatar
Bertrand Gauthier committed
48
        $shibUser = $this->shibService->getAuthenticatedUser();
49

50
        if ($shibUser === null) {
51
            return []; // une page d'aide s'affichera
Bertrand Gauthier's avatar
Bertrand Gauthier committed
52
53
        }

54
55
56
57
58
59
60
        /** @var AuthenticationService $authService */
        $authService = $this->getServiceLocator()->get('zfcuser_auth_service');
        try {
            $authService->getStorage()->write($shibUser->getId());
        } catch (ExceptionInterface $e) {
            throw new RuntimeException("Impossible d'écrire dans le storage");
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
61
62
63

        $this->userService->userAuthenticated($shibUser);

64
65
        $redirectUrl = $this->params()->fromQuery('redirect', '/');

Bertrand Gauthier's avatar
Bertrand Gauthier committed
66
67
68
        return $this->redirect()->toUrl($redirectUrl);
    }

69
    public function shibboleth()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
70
    {
71
72
73

    }
}