Commit 5172c3bd authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Jeton d'authentification : création et envoi à l'utilisateur déléguée au module unicaen/auth-token

parent abbe4633
Loading
Loading
Loading
Loading
+4 −3
Changes for composer.lock: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -3494,11 +3494,11 @@
        },
        {
            "name": "unicaen/auth",
            "version": "3.2.3",
            "version": "3.2.4",
            "source": {
                "type": "git",
                "url": "https://git.unicaen.fr/lib/unicaen/auth.git",
                "reference": "4fa0c9dc8a76829ea7f95efbcf55aa5495bf0948"
                "reference": "66a0e5409388a2379bd16065199056eedd184281"
            },
            "require": {
                "jasig/phpcas": "^1.3",
@@ -3520,7 +3520,7 @@
                ]
            },
            "description": "Module d'authentification pour les applications Unicaen",
            "time": "2021-06-24T09:16:46+00:00"
            "time": "2021-06-28T09:05:28+00:00"
        },
        {
            "name": "unicaen/auth-token",
@@ -3532,6 +3532,7 @@
            },
            "require": {
                "ramsey/uuid": "^3.7",
                "unicaen/app": "^3.2",
                "unicaen/auth": ">= 3.2.1"
            },
            "require-dev": {
+1 −1
Changes for module/Application/config/others/jetons.config.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ return [
            /**
             * Description facultative de ce mode d'authentification qui apparaîtra sur la page de connexion.
             */
            'description' => "Description ????",
//            'description' => "Description ????",
        ],
    ],

+2 −2
Changes for module/Application/config/others/utilisateur.config.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ return [
                        'ajouterFromIndividu',
                        'lier-individu',
                        'delier-individu',

                        'ajouter-token'
                    ],
                    'privileges' => UtilisateurPrivileges::UTILISATEUR_MODIFICATION,
@@ -221,7 +220,8 @@ return [
                    'ajouter-token' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/ajouter-token/:utilisateur',
                            /** @see \Application\Controller\UtilisateurController::ajouterTokenAction() */
                            'route'       => '/ajouter-token/:user',
                            'defaults'    => [
                                'action' => 'ajouter-token',
                            ],
+6 −1
Changes for module/Application/src/Application/Controller/Factory/UtilisateurControllerFactory.php: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\User as UserService;
use UnicaenAuth\Service\UserContext;
use UnicaenAuthToken\Controller\TokenController;
use UnicaenAuthToken\Service\TokenService;
use Zend\Authentication\AuthenticationService;
use ZfcUser\Mapper\UserInterface;
@@ -31,7 +32,7 @@ class UtilisateurControllerFactory
{
    use IndividuServiceLocateTrait;

    public function __invoke(ContainerInterface $container)
    public function __invoke(ContainerInterface $container): UtilisateurController
    {
        /**
         * @var ActeurService $acteurService
@@ -111,6 +112,10 @@ class UtilisateurControllerFactory
        $sourceCodeHelper = $container->get(SourceCodeStringHelper::class);
        $controller->setSourceCodeStringHelper($sourceCodeHelper);

        /** @var TokenController $tokenController */
        $tokenController = $container->get('ControllerManager')->get(TokenController::class);
        $controller->listenEventsOf($tokenController);

        return $controller;
    }
}
+34 −13
Changes for module/Application/src/Application/Controller/UtilisateurController.php: 34 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ use Application\Service\UniteRecherche\UniteRechercheServiceAwareTrait;
use Application\Service\UserContextServiceAwareTrait;
use Application\Service\Utilisateur\UtilisateurServiceAwareTrait;
use Application\SourceCodeStringHelperAwareTrait;
use DateTime;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query\Expr;
use UnicaenApp\Exception\RuntimeException;
@@ -31,9 +30,10 @@ use UnicaenAuth\Entity\Shibboleth\ShibUser;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\Traits\UserServiceAwareTrait;
use UnicaenAuthToken\Controller\TokenController;
use UnicaenAuthToken\Service\TokenServiceAwareTrait;
use UnicaenAuthToken\Service\TokenServiceException;
use Zend\Authentication\AuthenticationService;
use Zend\EventManager\EventInterface;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Paginator\Paginator as ZendPaginator;
@@ -508,21 +508,42 @@ class UtilisateurController extends \UnicaenAuth\Controller\UtilisateurControlle
        return $this->redirect()->toRoute('utilisateur/voir', ['utilisateur' => $utilisateur->getId()], [], true);
    }

    /**
     * Demande de création d'un jeton d'authentification.
     *
     * NB : à l'issue de la création, l'utilisateur reçoit automatiquement son jeton, cf {@see envoyerToken()}.
     *
     * @return ViewModel|Response
     */
    public function ajouterTokenAction()
    {
        $utilisateur = $this->getUtilisateurService()->getRequestedUtilisateur($this);
        $date = (new DateTime())->modify('+ 2 months');
        $token = null;
        try {
            $token = $this->tokenService->createUserToken($date);
            $token->setUser($utilisateur);
            $this->tokenService->saveUserToken($token);
        } catch (TokenServiceException $e) {
            throw new RuntimeException("Un problème est survenu lors de la création du token", 0,$e);

        // on délègue au module unicaen/auth-token
        return $this->forward()->dispatch(TokenController::class, [
            'action' => 'creer',
            'user' => $utilisateur->getId(),
        ]);
    }

        $this->tokenService->injectLogInUriInUserTokens([ $token ]);
        $this->notifierService->triggerCreationTokenAuthentification($utilisateur, $token, $token->getLogInUri()->toString());
        return $this->redirect()->toRoute('utilisateur/voir', ['utilisateur' => $utilisateur->getId()], [], true);
    public function listenEventsOf(TokenController $tokenController)
    {
        // écoute pour envoyer automatiquement tout jeton nouvellement créé à l'utlisateur
        $tokenController->getEventManager()->attach(
            $tokenController::EVENT_TOKEN_CREATE_AFTER_SAVE,
            [$this, 'envoyerToken']
        );
    }

    public function envoyerToken(EventInterface $event)
    {
        /** @var \Application\Entity\Db\UtilisateurToken $utilisateurToken */
        $utilisateurToken = $event->getParam('userToken');

        // on délègue au module unicaen/auth-token
        $this->forward()->dispatch(TokenController::class, [
            'action' => 'envoyer',
            'userToken' => $utilisateurToken->getId(),
        ]);
    }
}
 No newline at end of file
Loading