DirectoryTree / LdapRecord-Laravel

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

[Question] Can I set up a multi domain from a crud? #558

Closed smd-adrian closed 1 year ago

smd-adrian commented 1 year ago

Hi devs, I am using this cool package to authenticate my users with Active Directory which works great. I need to connect multiple domains for which I have read the documentation, but I am wondering if I can configure those domains from a crud where I store the credentials for each domain. The idea is to make it dynamic or at least be able to change the value of the LDAP_BASE_DN environment variable. I have tried several things but none seem to work.

image

Thanks to the creator and all the contributors for the excellent work on this package. Greetings...

stevebauman commented 1 year ago

Hi @smd-adrian,

I personally wouldn't dynamically inject values into the config, as the LdapRecord\LdapServiceProvider is likely loading before your code you've screenshotted above is loading, which is preventing it from detecting the new changes and instantiating the connection.

Instead of inserting config and env parameters dynamically, try inserting your connections that you have from the database into the LdapRecord\Container instead:

use LdapRecord\Container;
use LdapRecord\Connection;

$ldap = Ldap::findOrFail('...');

$connection = new Connection([
    'hosts' => [$ldap->host],
    'timeout' => 5,
    'use_ssl' => false,
    // ... other config params
]);

Container::addConnection($connection);

Keep in mind, PHP is stateless, so the code above will have to be executed upon every single request to your application where you expect the LDAP connection to exist.

smd-adrian commented 1 year ago

Hello @stevebauman , thank you very much for your help, I really didn't know which way to go and with your answer I was able to implement the function. Now everything is solved. Really, thank you very much again for this wonderful package.
Greetings from Colombia.