I'm using Symfony framework v 2.4.2 to update an existing website which performs a double check to log users in:
ldap_bind()
function);How can I replicate this auth process in Symfony2?
So far, I got stuck with FOSUserBundle and FR3DLdapBundle: I managed to use chained providers (ldap
and db
), but seemingly LDAP credentials are completely ignored: users can login with the credentials stored in the DB, even if the ldap_bind()
fails - which is the exact opposite of points 1 & 2.
Besides, when using FOSUserBundle, it seems to be mandatory to store passwords inside the DB.
Please pay attention to point no. 2: users must be free to change their LDAP password from outside the website (that is, from Active Directory), and then log in with the new credentials- without any update to the website's users database.
Any solution is welcome, I'm not so much in love with FOSUserBundle and even less with FR3DLdapBundle.
I worked it out like this:
providers
section in app/config/security.yml
;You need to define a CustomUserProvider for LDAP which will find the user in AD and in your security.yml you need to set this provider (Custom User Provider)
providers:
ldap_provider:
id: myprovider.security.user.provider # as you know when you create a custom web service provider you need to define it as a service and use the ID of the service here #
You also need to enable your service in your firewall
Next step is to create custom Authentication Provider for LDAP which authenticate the user and password through LDAP and return the user object from DB (Custom Authentication Provider)
Base on this process first it will try to find user in LDAP if it exists it will continue authenticating through LDAP and if user is authenticated it will return the User Object from DB
Here is a sample for custom user provider
public function loadUserByUsername($username)
{
$ldap_obj = new adLDAP('exampl.ldapdircetory.com', 'prot', 'Base_DN', 'ldap_admin_user', 'ldap_admin_pass'));
$filter = "( &(objectclass=person)( |(uid=".$username.")(cn=".$username.")(mail=".$username.") ) )";
$info = $ldap_obj->search_user($filter);
if ($info['count'] != 0) {
// create temp User instance of UserInterface base on the returned info
$user = new User();
...
return $user
}
else {
throw new UsernameNotFoundException('No match username was found.');
}
I assume you have adLDAP class to authenticate user(s) through LDAP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With