DirectoryTree / LdapRecord-Laravel

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

[Question] Multi-domain setup with dynamic connection. #568

Closed eafarooqi closed 11 months ago

eafarooqi commented 11 months ago

Hi,

i have setup the package and using Jetstream Authentication. Below is the code form AuthServiceProvider.

Fortify::authenticateUsing(function ($request) {
            $validated = Auth::validate([
                'samaccountname' => $request->username,
                'password' => $request->password
            ]);

            return $validated ? Auth::getLastAttempted() : null;
        });

Works very good. Now i need to implement multi-domain.

i have tried to follow the multi domain documentation but unfortunately its not working. https://ldaprecord.com/docs/laravel/v3/auth/multi-domain

i need to change connection on runtime. its actually just base_dn. I have checked all the issues find one with the exact requirements. https://github.com/DirectoryTree/LdapRecord-Laravel/issues/558

how can i inject connections or switch connections at runtime in the AuthServiceProvider. The login form is already sending domain so is it possible to use the specific connection in AuthServiceProvider?

i am using Laravel 10 and ldaprecord-laravel v 3.0.6

Thanks.

stevebauman commented 11 months ago

Hi @eafarooqi,

i have tried to follow the multi domain documentation but unfortunately its not working.

Can you expand on this? Did you receive exceptions, errors, or was something not clear?

If you've setup multi-domain as shown in the docs, you don't need to change connections at runtime, as the authentication guard (which will be unique per LDAP domain) is saved into the user's session.

eafarooqi commented 11 months ago

Hi @stevebauman

i am using Eloquent Model Binding as below as per documentation.

use LdapRecord\Laravel\Auth\HasLdapUser;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LdapAuthenticatable
{
    use Notifiable, AuthenticatesWithLdap, HasLdapUser;
}

and defining the property connection on this model will refer to database connection not the ldap connection. Thats why ldap authentication is always using the default connection and looking for database connection bravo.

    protected ?string $connection = 'bravo';
stevebauman commented 11 months ago

@eafarooqi You've applied the $connection property to the wrong model. You must apply it to an LdapRecord model, not your Eloquent user model. This is shown in the multi-domain authentication, as you must create a unique LdapRecord User model for each of your configured connections:

https://ldaprecord.com/docs/laravel/v3/auth/multi-domain/#configuring-the-ldap-connections

Screenshot 2023-09-25 at 4 33 30 PM

I'll update the docs so it's more explicit 👍