diff --git a/sources/patches/main-00-ldap-auth.patch b/sources/patches/main-00-ldap-auth.patch index 1f263cf..cccd7d8 100644 --- a/sources/patches/main-00-ldap-auth.patch +++ b/sources/patches/main-00-ldap-auth.patch @@ -4,10 +4,10 @@ Date: Sun Jan 7 16:25:06 2024 +0100 YNH LDAP -diff --git a/app/config/packages/security.yaml b/app/config/packages/security.yaml +diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 6c4457f1..e716ba39 100644 ---- a/app/config/packages/security.yaml -+++ b/app/config/packages/security.yaml +--- a/config/packages/security.yaml ++++ b/config/packages/security.yaml @@ -11,6 +11,11 @@ security: entity: class: App\Entity\User @@ -33,10 +33,10 @@ index 6c4457f1..e716ba39 100644 logout: path: security.logout target: security.login -diff --git a/app/config/services.yaml b/app/config/services.yaml +diff --git a/config/services.yaml b/config/services.yaml index 3e770913..83fbec0d 100644 ---- a/app/config/services.yaml -+++ b/app/config/services.yaml +--- a/config/services.yaml ++++ b/config/services.yaml @@ -104,3 +104,21 @@ services: Aeneria\GrdfAdictApi\Client\GrdfAdictClientInterface: @@ -59,11 +59,11 @@ index 3e770913..83fbec0d 100644 + class: Symfony\Component\Ldap\Adapter\ExtLdap\Adapter + arguments: + - host: "localhost" -diff --git a/app/src/Security/YnhLdapUserProvider.php b/app/src/Security/YnhLdapUserProvider.php +diff --git a/src/Security/YnhLdapUserProvider.php b/src/Security/YnhLdapUserProvider.php new file mode 100755 index 00000000..eb8b1149 --- /dev/null -+++ b/app/src/Security/YnhLdapUserProvider.php ++++ b/src/Security/YnhLdapUserProvider.php @@ -0,0 +1,79 @@ +loadUserByIdentifier($username); -+ } -+ -+ /** -+ * {@inheritdoc} -+ */ -+ public function loadUserByIdentifier(string $identifier): UserInterface -+ { -+ try { -+ $this->ldap->bind($this->searchDn, $this->searchPassword); -+ $username = $this->ldap->escape($identifier, '', LdapInterface::ESCAPE_FILTER); -+ $query = str_replace('{username}', $identifier, $this->defaultSearch); -+ $search = $this->ldap->query($this->baseDn, $query); -+ } catch (ConnectionException $e) { -+ throw new UserNotFoundException(sprintf('User "%s" not found.', $identifier), 0, $e); -+ } -+ -+ $entries = $search->execute(); -+ $count = \count($entries); -+ -+ if ($count > 1) { -+ throw new UserNotFoundException('More than one user found.'); -+ } -+ -+ $entry = $entries[0]; -+ -+ $identifier = $this->getAttributeValue($entry, 'mail'); -+ -+ return $this->userRepository->findOneBy(['username' => $identifier]); -+ } -+ -+ /** -+ * {@inheritdoc} -+ */ -+ public function refreshUser(UserInterface $user) -+ { -+ if (!$user instanceof User) { -+ throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); -+ } -+ -+ return $this->userRepository->findOneBy(['username' => $user->getUsername()]); -+ } -+ -+ /** -+ * {@inheritdoc} -+ */ -+ public function supportsClass(string $class) -+ { -+ return User::class === $class; -+ } -+ -+ private function getAttributeValue(Entry $entry, string $attribute) -+ { -+ if (!$entry->hasAttribute($attribute)) { -+ throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $attribute, $entry->getDn())); -+ } -+ -+ $values = $entry->getAttribute($attribute); -+ -+ return $values[0]; -+ } -+}