BorisMorel/LdapBundle — pull request #57 by johnkary (2013-02-22 07:21:12+00:00)
Title: Only connect to LDAP server when attempting to interact with server

This attempts to fix the issue @lngphp was having in the comments of #53.

I also ran into the same problem when developing offline (no internet connection) and relying on a remote LDAP server for my authentication. When attempting to clear the cache and being offline would would throw this Exception:

The credentials you have configured are not valid

Instead, I have moved the connection code from the `LdapConnection::__construct()` and instead moved it into each method that actually queries against the LDAP server. This also now checks if a valid connection resource and returns early if a connection has already been previously made.

The error message from before does not accurately describe the problem. The problem was the connection to the server could not be made--not that my credentials were invalid. This error is not thrown until attempting to [bind to the server](https://github.com/BorisMorel/LdapBundle/blob/ca21a5c5c2931e6c0bc383336b1662a892c4d086/Manager/LdapConnection.php#L131). Why not check if `ldap_connect()` [returns false](https://github.com/BorisMorel/LdapBundle/blob/ca21a5c5c2931e6c0bc383336b1662a892c4d086/Manager/LdapConnection.php#L110) and throw an Exception if unable to connect?

If you would like me to add a check for failed connection and throw an Exception I can add code to this PR.

Thanks!
:rocket: