diff --git a/src/UnicaenUtilisateur/Service/User/UserService.php b/src/UnicaenUtilisateur/Service/User/UserService.php index bea3d1a83ecc58448b9dfbb1379c12d2797c1c73..dcd8782ab7010396e8976c8682e76ea6f95375f9 100644 --- a/src/UnicaenUtilisateur/Service/User/UserService.php +++ b/src/UnicaenUtilisateur/Service/User/UserService.php @@ -3,8 +3,8 @@ namespace UnicaenUtilisateur\Service\User; use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\ORMException; +use InvalidArgumentException; use Ramsey\Uuid\Uuid; use UnicaenApp\Entity\Ldap\People; use UnicaenApp\Service\EntityManagerAwareTrait; @@ -29,19 +29,24 @@ class UserService implements RechercheIndividuServiceInterface use RoleServiceAwareTrait; use MailServiceAwareTrait; - /** - * @var string - */ - private $userEntityClass; + private ?string $userEntityClass = null; + private ?array $authentificationConfig = null; - /** - * @var AuthenticationService - */ - private $authenticationService; + public function getAuthentificationConfig(): ?array + { + return $this->authentificationConfig; + } + + public function setAuthentificationConfig(?array $authentificationConfig): void + { + $this->authentificationConfig = $authentificationConfig; + } + + private ?AuthenticationService $authenticationService; private $renderer; public function setRenderer($renderer) { $this->renderer = $renderer;} - private $appname; + private ?string $appname = null; public function setAppname($appname) { $this->appname = $appname;} @@ -49,19 +54,18 @@ class UserService implements RechercheIndividuServiceInterface * @param string $userEntityClass * @return void */ - public function setUserEntityClass($userEntityClass) + public function setUserEntityClass(string $userEntityClass) : void { if (! class_exists($userEntityClass) || ! in_array(UserInterface::class, class_implements($userEntityClass))) { throw new InvalidArgumentException("L'entité associée aux utilisateurs doit implémenter " . UserInterface::class); } - $this->userEntityClass = $userEntityClass; } /** * @return string */ - public function getEntityClass() + public function getEntityClass() : ?string { return $this->userEntityClass; } @@ -71,7 +75,7 @@ class UserService implements RechercheIndividuServiceInterface * * @return UserInterface */ - public function getEntityInstance() + public function getEntityInstance() : UserInterface { return new $this->userEntityClass; } @@ -346,19 +350,25 @@ class UserService implements RechercheIndividuServiceInterface return $qb->getQuery()->getResult(); } - /** - * @return UserInterface - */ - public function getConnectedUser() + public function getConnectedUser() : ?UserInterface { $identity = $this->authenticationService->getIdentity(); if ($identity) { - $username = null; if (isset($identity['ldap'])) { /** @var People $userIdentity */ $userIdentity = $identity['ldap']; - $username = $userIdentity->getSupannAliasLogin(); + switch ($this->getAuthentificationConfig()['local']['ldap']['username']) { + case 'uid' : + $username = $userIdentity->getUid(); + break; + case 'supannaliaslogin' : + $username = $userIdentity->getSupannAliasLogin(); + break; + default : + throw new RuntimeException("La clef de config [unicaen-auth][loca][ldap][username] n'est pas défini ou à une valeur non attendue."); + } + $user = $this->findByUsername($username); return $user; diff --git a/src/UnicaenUtilisateur/Service/User/UserServiceFactory.php b/src/UnicaenUtilisateur/Service/User/UserServiceFactory.php index dbd9a06762ec31f6633ae2e6dd43dd6c68468483..fd0cb2c12b6c972c77c2a1626c439f5885cf1958 100644 --- a/src/UnicaenUtilisateur/Service/User/UserServiceFactory.php +++ b/src/UnicaenUtilisateur/Service/User/UserServiceFactory.php @@ -5,6 +5,8 @@ namespace UnicaenUtilisateur\Service\User; use Doctrine\ORM\EntityManager; use Interop\Container\ContainerInterface; use Laminas\View\Renderer\PhpRenderer; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use UnicaenAuthentification\Service\UserContext; use UnicaenMail\Service\Mail\MailService; use UnicaenUtilisateur\Entity\Db\User; @@ -20,8 +22,10 @@ class UserServiceFactory * @param string $requestedName * @param array|null $options * @return UserService + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) : UserService { /** * @var EntityManager $entityManager @@ -43,6 +47,7 @@ class UserServiceFactory $service->setRoleService($roleService); $service->setServiceUserContext($userContext); $service->setUserEntityClass($allConfig['zfcuser']['user_entity_class'] ?? User::class); + $service->setAuthentificationConfig($allConfig['unicaen-auth']); $service->setAuthenticationService($authenticationService); /* @var PhpRenderer $renderer */