From 35fcbe1dca8f6a2452fa4084582b6446071e4175 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Metivier <jean-philippe.metivier@unicaen.fr>
Date: Fri, 16 Dec 2022 11:36:49 +0100
Subject: [PATCH] Ajout d'une interface pour visualiser le lien User/Agent
 (/agent/verifier-lien)

---
 config/autoload/unicaen-app.global.php        |  4 +-
 documentation/release/3.1.2.md                |  3 +-
 .../config/merged/agent.config.php            | 21 +++++
 .../Controller/AgentController.php            | 17 ++++
 .../Service/Agent/AgentService.php            | 11 +++
 .../application/agent/verifier-lien.phtml     | 80 +++++++++++++++++++
 6 files changed, 133 insertions(+), 3 deletions(-)
 create mode 100644 module/Application/view/application/agent/verifier-lien.phtml

diff --git a/config/autoload/unicaen-app.global.php b/config/autoload/unicaen-app.global.php
index 7220bc23d..8050d9a65 100644
--- a/config/autoload/unicaen-app.global.php
+++ b/config/autoload/unicaen-app.global.php
@@ -34,8 +34,8 @@ return [
         'app_infos' => [
             'nom'     => "EMC2",
             'desc'    => "Emploi Mobilité Carrière Compétences",
-            'version' => "3.1.1",
-            'date'    => "08/12/2022",
+            'version' => "3.1.2",
+            'date'    => "16/12/2022",
 
 //            'liens' => [
 //                'COMUE' => [
diff --git a/documentation/release/3.1.2.md b/documentation/release/3.1.2.md
index 40d5d8193..24e87bcde 100644
--- a/documentation/release/3.1.2.md
+++ b/documentation/release/3.1.2.md
@@ -1,10 +1,11 @@
-**CHANGES version 3.1.1**
+**CHANGES version 3.1.2**
 
 -----------
 
 **Changements**
 
 * Ajout d'un parametre indiquant le chamin d'installation pour la partie Vérification
+* Ajout d'une interface pour visualiser le lien User/Agent (/agent/verifier-lien)
 * [FIX] correction du choix de l'échelon (si plusieurs échelons actifs alors selection du plus récent "en terme de date de passage")
 * [FIX] propagation d'un renommage de variable sur l'interface des supérieurs hiérarchique
 
diff --git a/module/Application/config/merged/agent.config.php b/module/Application/config/merged/agent.config.php
index d263dc896..b1758b2d0 100644
--- a/module/Application/config/merged/agent.config.php
+++ b/module/Application/config/merged/agent.config.php
@@ -141,6 +141,17 @@ return [
                         AgentPrivileges::AGENT_GESTION_CCC,
                     ],
                 ],
+
+                /** VERIF */
+                [
+                    'controller' => AgentController::class,
+                    'action' => [
+                        'verifier-lien',
+                    ],
+                    'privileges' => [
+                        AgentPrivileges::AGENT_AFFICHER,
+                    ],
+                ],
             ],
         ],
     ],
@@ -158,6 +169,16 @@ return [
                 ],
                 'may_terminate' => true,
                 'child_routes' => [
+                    'verifier-lien' => [
+                        'type'  => Segment::class,
+                        'options' => [
+                            'route'    => '/verifier-lien[/:utilisateur]',
+                            'defaults' => [
+                                'controller' => AgentController::class,
+                                'action'     => 'verifier-lien',
+                            ],
+                        ],
+                    ],
                     /** Fonctions de recherche ************************************************************************/
                     'rechercher' => [
                         'type'  => Literal::class,
diff --git a/module/Application/src/Application/Controller/AgentController.php b/module/Application/src/Application/Controller/AgentController.php
index a108c2574..eac4bbc68 100644
--- a/module/Application/src/Application/Controller/AgentController.php
+++ b/module/Application/src/Application/Controller/AgentController.php
@@ -379,6 +379,23 @@ class AgentController extends AbstractActionController
         return $vm;
     }
 
+    /** Vérification lien Utilisateur <=> Agent **/
+
+    public function verifierLienAction() : ViewModel
+    {
+        $user = $this->getUserService()->getRequestedUser($this);
+        if ($user === null) $user = $this->getUserService()->getConnectedUser();
+
+        $agentByUser = $this->getAgentService()->getAgentByUser($user);
+        $agentByLogin = $this->getAgentService()->getAgentByLogin($user->getUsername());
+
+        return new ViewModel([
+            'utilisateur' => $user,
+            'agentByUser' => $agentByUser,
+            'agentByLogin' => $agentByLogin,
+        ]);
+    }
+
     /** Recherche d'agent  ********************************************************************************************/
 
     public function rechercherLargeAction() : JsonModel
diff --git a/module/Application/src/Application/Service/Agent/AgentService.php b/module/Application/src/Application/Service/Agent/AgentService.php
index 2fdee58e4..47fc404e3 100644
--- a/module/Application/src/Application/Service/Agent/AgentService.php
+++ b/module/Application/src/Application/Service/Agent/AgentService.php
@@ -681,4 +681,15 @@ class AgentService {
         return $fiches;
     }
 
+    public function getAgentByLogin(string $login) : ?Agent
+    {
+        $qb = $this->createQueryBuilder()
+            ->andWhere('agent.login = :login')->setParameter('login', $login);
+        try {
+            $result = $qb->getQuery()->getOneOrNullResult();
+        } catch (NonUniqueResultException $e) {
+            throw new RuntimeException("Plusieurs Agent partagent le même login [".$login."]",0, $e);
+        }
+        return $result;
+    }
 }
\ No newline at end of file
diff --git a/module/Application/view/application/agent/verifier-lien.phtml b/module/Application/view/application/agent/verifier-lien.phtml
new file mode 100644
index 000000000..925e6512b
--- /dev/null
+++ b/module/Application/view/application/agent/verifier-lien.phtml
@@ -0,0 +1,80 @@
+<?php
+
+use Application\Entity\Db\Agent;
+use UnicaenUtilisateur\Entity\Db\User;
+
+/** @see \Application\Controller\AgentController::verifierLienAction()
+ * @var User|null $utilisateur
+ * @var Agent|null $agentByLogin
+ * @var Agent|null $agentByUser
+ */
+
+?>
+
+<h1 class="page-header">
+    Vérification du lien
+</h1>
+
+<div class="card card-default">
+    <div class="card-header">
+        Information sur l'utilisateur·trice
+    </div>
+    <div class="card-body">
+        <?php if ($utilisateur === null) : ?>
+            Aucun·e utilisateur·trice
+        <?php else : ?>
+            <dl class="row">
+                <dt class="col-md-4">Id </dt>
+                <dd class="col-md-8"><?php echo $utilisateur->getId(); ?></dd>
+                <dt class="col-md-4">Username </dt>
+                <dd class="col-md-8"><?php echo $utilisateur->getUsername(); ?></dd>
+                <dt class="col-md-4">DisplayName </dt>
+                <dd class="col-md-8"><?php echo $utilisateur->getDisplayName(); ?></dd>
+                <dt class="col-md-4">Email </dt>
+                <dd class="col-md-8"><?php echo $utilisateur->getEmail(); ?></dd>
+            </dl>
+        <?php endif; ?>
+    </div>
+</div>
+
+
+<div class="card card-default">
+    <div class="card-header">
+        Agent (récupérer by User [exploité pour la récupération des dossiers])
+    </div>
+    <div class="card-body">
+        <?php if ($agentByUser === null) : ?>
+            Aucun agent
+        <?php else : ?>
+            <dl class="row">
+                <dt class="col-md-4">Id </dt>
+                <dd class="col-md-8"><?php echo $agentByUser->getId(); ?></dd>
+                <dt class="col-md-4">Dénomination </dt>
+                <dd class="col-md-8"><?php echo $agentByUser->getDenomination(); ?></dd>
+                <dt class="col-md-4">Email </dt>
+                <dd class="col-md-8"><?php echo $agentByUser->getEmail(); ?></dd>
+            </dl>
+        <?php endif; ?>
+    </div>
+</div>
+
+<div class="card card-default">
+    <div class="card-header">
+        Agent (récupéré by login [pas exploiter pour la récupération des dossiers])
+    </div>
+    <div class="card-body">
+        <?php if ($agentByLogin === null) : ?>
+            Aucun agent
+        <?php else : ?>
+            <dl class="row">
+                <dt class="col-md-4">Id </dt>
+                <dd class="col-md-8"><?php echo $agentByLogin->getId(); ?></dd>
+                <dt class="col-md-4">Dénomination </dt>
+                <dd class="col-md-8"><?php echo $agentByLogin->getDenomination(); ?></dd>
+                <dt class="col-md-4">Email </dt>
+                <dd class="col-md-8"><?php echo $agentByLogin->getEmail(); ?></dd>
+            </dl>
+        <?php endif; ?>
+    </div>
+</div>
+
-- 
GitLab