From bb1e9370de45beeeb213e1082a0342d626e4f500 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Metivier <jean-philippe.metivier@unicaen.fr>
Date: Wed, 8 Mar 2023 16:15:11 +0100
Subject: [PATCH] modificaiton uid

---
 src/UnicaenAuthentification/Entity/Ldap/People.php        | 4 +++-
 src/UnicaenAuthentification/Provider/Identity/Ldap.php    | 8 +++++++-
 .../Provider/Identity/LdapServiceFactory.php              | 2 ++
 src/UnicaenAuthentification/Service/UserContext.php       | 8 +++++++-
 .../Service/UserContextFactory.php                        | 3 ++-
 5 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/UnicaenAuthentification/Entity/Ldap/People.php b/src/UnicaenAuthentification/Entity/Ldap/People.php
index 4f07eac..cfc8e73 100644
--- a/src/UnicaenAuthentification/Entity/Ldap/People.php
+++ b/src/UnicaenAuthentification/Entity/Ldap/People.php
@@ -63,8 +63,10 @@ class People extends BasePeople implements UserInterface, ProviderInterface
     /**
      * {@inheritDoc}
      */
-    public function getUsername()
+    public function getUsername(?string $attribut = "supannAliasLogin")
     {
+        if ($attribut === 'supannAliasLogin') return $this->supannaliaslogin;
+        if ($attribut === 'uid') return $this->uid;
         return $this->supannaliaslogin;
     }
 
diff --git a/src/UnicaenAuthentification/Provider/Identity/Ldap.php b/src/UnicaenAuthentification/Provider/Identity/Ldap.php
index bdbd312..51e8e38 100644
--- a/src/UnicaenAuthentification/Provider/Identity/Ldap.php
+++ b/src/UnicaenAuthentification/Provider/Identity/Ldap.php
@@ -18,6 +18,12 @@ use ZfcUser\Entity\UserInterface;
  */
 class Ldap extends AuthenticationIdentityProvider implements ChainableProvider
 {
+    protected $usernamefield;
+
+    public function setUsernamefield(?string $value = null)
+    {
+        $this->usernamefield=$value;
+    }
     /**
      * {@inheritDoc}
      */
@@ -50,7 +56,7 @@ class Ldap extends AuthenticationIdentityProvider implements ChainableProvider
         }
 
         if ($identity instanceof UserInterface) {
-            $roles[] = $identity->getUsername();
+            $roles[] = $identity->getUsername($this->usernamefield);
         }
 
         return $roles;
diff --git a/src/UnicaenAuthentification/Provider/Identity/LdapServiceFactory.php b/src/UnicaenAuthentification/Provider/Identity/LdapServiceFactory.php
index f387f55..a5defb1 100644
--- a/src/UnicaenAuthentification/Provider/Identity/LdapServiceFactory.php
+++ b/src/UnicaenAuthentification/Provider/Identity/LdapServiceFactory.php
@@ -30,6 +30,8 @@ class LdapServiceFactory implements FactoryInterface
 
         $simpleIdentityProvider->setDefaultRole($config['default_role']);
         $simpleIdentityProvider->setAuthenticatedRole($config['authenticated_role']);
+        $usernamefield = $config['unicaen-auth']['local']['ldap']['username'];
+        $simpleIdentityProvider->setUsernamefield($usernamefield);
 
         return $simpleIdentityProvider;
     }
diff --git a/src/UnicaenAuthentification/Service/UserContext.php b/src/UnicaenAuthentification/Service/UserContext.php
index cc7e05f..59dce65 100644
--- a/src/UnicaenAuthentification/Service/UserContext.php
+++ b/src/UnicaenAuthentification/Service/UserContext.php
@@ -44,6 +44,12 @@ class UserContext extends AbstractService implements EventManagerAwareInterface
      */
     protected $identityRoles;
 
+    private $usernamefield;
+
+    public function setUsernamefield(?string $value = null)
+    {
+        $this->usernamefield = $value;
+    }
     /**
      * @var AuthenticationService
      */
@@ -132,7 +138,7 @@ class UserContext extends AbstractService implements EventManagerAwareInterface
             return $user->getUsername();
         }
         if ($user = $this->getLdapUser()) {
-            return $user->getUsername();
+            return $user->getUsername($this->usenamefield);
         }
         if ($user = $this->getDbUser()) {
             return $user->getUsername();
diff --git a/src/UnicaenAuthentification/Service/UserContextFactory.php b/src/UnicaenAuthentification/Service/UserContextFactory.php
index 5f3427c..5f857a1 100644
--- a/src/UnicaenAuthentification/Service/UserContextFactory.php
+++ b/src/UnicaenAuthentification/Service/UserContextFactory.php
@@ -32,7 +32,8 @@ class UserContextFactory
         // mais c'est impossible pour l'instant car il y a un cycle dans les dépendances entre services qui
         // provoque une boucle infinie.
         //
-
+        $usernamefield = $config['unicaen-auth']['local']['ldap']['username'];
+        $service->setUsernamefield($usernamefield);
         return $service;
     }
 }
\ No newline at end of file
-- 
GitLab