AuthController.php 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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;

/**
 * Classe ajoutée lors de l'implémentation de l'auth Shibboleth.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class AuthController extends AbstractActionController
{
    use ShibServiceAwareTrait;
    use UserServiceAwareTrait;

    /**
     * @return Response|array
     */
    public function shibbolethAction()
    {
        $shibUser = $this->shibService->getAuthenticatedUser();

        if ($shibUser === null) {
            return []; // la page d'aide s'affiche
        }

        /** @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");
        }

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

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

        return $this->redirect()->toUrl($redirectUrl);
    }
}