Skip to content

Commit b0c1371

Browse files
committed
Auth: Add LDAP authenticator - refs BT#22300
1 parent 37676ab commit b0c1371

File tree

8 files changed

+376
-3
lines changed

8 files changed

+376
-3
lines changed

config/authentication.dist.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,27 @@ parameters:
6363
script_users_delta: false
6464
script_usergroups_delta: false
6565
group_filter_regex: ''
66+
67+
ldap:
68+
enabled: false
69+
title: 'LDAP'
70+
connection_string: 'ldap://localhost:389'
71+
protocol_version: 3
72+
referrals: false
73+
dn_string: '{username}'
74+
query_string: null
75+
base_dn: 'DC=cblue,DC=be'
76+
search_dn: 'CN=admin,dc=cblue,dc=be'
77+
search_password: 'pass'
78+
filter: null
79+
uid_key: 'uid'
80+
password_attribute: 'userPassword'
81+
data_correspondence:
82+
firstname: 'givenName'
83+
lastname: 'sn'
84+
email: 'mail'
85+
locale: null
86+
role: null
87+
phone: null
88+
active: null
89+
admin: null

config/packages/security.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ security:
120120
- Chamilo\CoreBundle\Security\Authenticator\OAuth2\FacebookAuthenticator
121121
- Chamilo\CoreBundle\Security\Authenticator\OAuth2\KeycloakAuthenticator
122122
- Chamilo\CoreBundle\Security\Authenticator\OAuth2\AzureAuthenticator
123+
- Chamilo\CoreBundle\Security\Authenticator\Ldap\LdapAuthenticator
123124

124125
access_control:
125126
- { path: ^/login/token/check, roles: PUBLIC_ACCESS }

config/services.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ services:
162162
arguments:
163163
- '%env(AZURE_STORAGE_CONNECTION_STRING)%'
164164

165+
Chamilo\CoreBundle\Security\Authenticator\Ldap\ExtAdapter: ~
166+
167+
Symfony\Component\Ldap\Ldap:
168+
arguments: [ '@Chamilo\CoreBundle\Security\Authenticator\Ldap\ExtAdapter' ]
169+
tags:
170+
- ldap
171+
172+
Symfony\Component\Ldap\Security\CheckLdapCredentialsListener:
173+
arguments: [ '@security.ldap_locator' ]
174+
tags:
175+
- { name: kernel.event_subscriber, dispatcher: security.event_dispatcher.main }
176+
165177
cocur_slugify:
166178
lowercase: true
167179

src/CoreBundle/Controller/SecurityController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
1717
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
1818
use Chamilo\CoreBundle\Repository\TrackELoginRecordRepository;
19+
use Chamilo\CoreBundle\Security\Authenticator\Ldap\LdapAuthenticator;
1920
use Chamilo\CoreBundle\Security\Authenticator\LoginTokenAuthenticator;
2021
use Chamilo\CoreBundle\Settings\SettingsManager;
2122
use DateTime;
@@ -219,6 +220,16 @@ public function loginTokenCheck(): Response
219220
return new Response(null, Response::HTTP_NO_CONTENT);
220221
}
221222

223+
/**
224+
* @see LdapAuthenticator
225+
*/
226+
#[Route('/login/ldap/check', name: 'login_ldap_check', methods: ['POST'])]
227+
public function ldapLoginCheck(): Response
228+
{
229+
// this response was managed in LdapAuthenticator class
230+
return new Response(null, Response::HTTP_NO_CONTENT);
231+
}
232+
222233
/**
223234
* Validates the provided TOTP code for the given user.
224235
*

src/CoreBundle/Helpers/AuthenticationConfigHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,19 @@ public function getOAuthProviderOptions(string $providerType, array $config): ar
159159

160160
return array_filter($defaults, fn ($value) => null !== $value);
161161
}
162+
163+
164+
/**
165+
* @return array<string, mixed>
166+
*/
167+
public function getLdapConfig(?AccessUrl $url): array
168+
{
169+
$authentication = $this->getAuthSources($url);
170+
171+
if (isset($authentication['ldap'])) {
172+
return $authentication['ldap'];
173+
}
174+
175+
return [];
176+
}
162177
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
declare(strict_types=1);
6+
7+
namespace Chamilo\CoreBundle\Security\Authenticator\Ldap;
8+
9+
use Chamilo\CoreBundle\Helpers\AuthenticationConfigHelper;
10+
use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
11+
12+
class ExtAdapter extends Adapter
13+
{
14+
public function __construct(
15+
private readonly AuthenticationConfigHelper $authConfigHelper,
16+
) {
17+
$params = $this->authConfigHelper->getLdapConfig(null);
18+
19+
$config = [
20+
'connection_string' => $params['connection_string'] ?? 'ldap://localhost:389',
21+
'options' => [
22+
'protocol_version' => $params['protocol_version'] ?? 3,
23+
'referrals' => $params['referrals'] ?? false,
24+
],
25+
];
26+
27+
parent::__construct($config);
28+
}
29+
}

0 commit comments

Comments
 (0)