DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
492 stars 51 forks source link

[Support] `.env` configuration isn't loading #520

Closed vicmarconi closed 1 year ago

vicmarconi commented 1 year ago

Environment:

Describe the bug: I think the .ENV file configurations are not being properly loaded.

LDAP_LOGGING=true
LDAP_CONNECTION=default
LDAP_CONNECTIONS=default
LDAP_DEFAULT_HOSTS=[company host]
LDAP_DEFAULT_USERNAME=[my user]
LDAP_DEFAULT_PASSWORD=[my password]
LDAP_DEFAULT_PORT=636
LDAP_DEFAULT_BASE_DN=[my company dn]
LDAP_DEFAULT_TIMEOUT=30
LDAP_DEFAULT_SSL=true
LDAP_DEFAULT_TLS=false
LDAP_DEFAULT_OPT_X_TLS_CERTFILE=0
LDAP_DEFAULT_OPT_REFERRALS=0
LDAP_DEFAULT_OPT_PROTOCOL_VERSION=3

The output of artisan ldap:test was: ldap_bind(): Unable to bind to server: Can't contact LDAP server. Error Code: [-1] Diagnostic Message: null (it also showed my username - so I think it might be reading something from the .env)

But when I published the ldap.php config file and "hardcoded" the values there, it connected successfully.

Perhaps it's not a bug, but the documentation says that we can use either a published config or the .env

stevebauman commented 1 year ago

Hi @vicmarconi,

Loading from the .env file should be working, as there's tests ensuring this is the case and I have my own app loading connections from my .env file.

Delete your config/ldap.php file and try testing again. If the test displays your username from your .env file, then it's successfully loading the connection. Ensure that you've wrapped your LDAP_DEFAULT_USERNAME and LDAP_DEFAULT_BASE_DN and LDAP_DEFAULT_PASSWORD inside of quotes, otherwise the equals signs (and other special characters) may break parsing the .env file. For example:

LDAP_DEFAULT_USERNAME="cn=admin,dc=local,dc=com"

For another test, jump into the vendor directory and dd() here:

https://github.com/DirectoryTree/LdapRecord-Laravel/blob/5f36d4ba6edbf56d97211fb892188aa6f9aceabc/src/LdapServiceProvider.php#L152-L154

For example:

$connections = array_filter( 
    array_map('trim', explode(',', env('LDAP_CONNECTIONS', ''))) 
); 

dd($connections);

Then run php artisan ldap:test again. You should see your .env connection printed:

> php artisan ldap:test
array:1 [
  0 => "default"
]

If not, then your .env file is not being properly loaded.

stevebauman commented 1 year ago

Closing due to inactivity.