DirectoryTree / LdapRecord-Laravel

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

[Support] How to load all Ldap Users and make the load faster? #397

Closed kresnasatya closed 2 years ago

kresnasatya commented 2 years ago

Hi @stevebauman, In my OpenLDAP company we have more than 100k users. I want to load all users in a datatable using Livewire. So far, I use select() method to select which property of users that I want to load and paginate() method.

Here's my code.

public function render()
    {
        $users = User::select(['cn', 'givenname', 'mail', 'uid', 'unudusertypeid'])
            ->search($this->search)
            ->withType($this->searchUserType)
            ->paginate();

        $users_collection = collect($users)->sortBy([
            [$this->sortField, $this->sortAsc ? 'asc': 'desc']
        ])->paginate($this->perPage);

        return view('livewire.users-table', [
            'users' => $users_collection,
            'user_types' => getUserTypes()
        ]);
    }

I also use spatie/laravel-collection-macros to convert Ldap Users to collection and setpaginate() method from that package. Sometimes, all users load in datatable and sometimes the page is blank. Is it possible to use database auth strategy to cover that case. So, instead I load users from OpenLDAP, it will load from database? If yes, the next question is if I update or add users data and store it to database will LdapRecord provide like "sync" method? For authentication we use SSO with Keycloak platform and we use ldap federation to sync users and groups.

Thanks.

stevebauman commented 2 years ago

Hi @satyakresna,

Is it possible to use database auth strategy to cover that case. So, instead I load users from OpenLDAP, it will load from database?

Yes it's entirely possible, and if you're needing to view that many users on one page, I would definitely agree that sync'ing them to your local database would be a great option to keep your app feeling fast, since paginating that many users probably takes more than a couple seconds.

You don't actually need to use the LdapRecord-Laravel authentication driver to synchronize or import users. The sync functionality has been abstracted into it's own utility:

https://ldaprecord.com/docs/laravel/v2/importing

You could create an ldap_objects database table to house these synchronized users, add a guid and domain database column, define sync attributes, and you're all set.

However, if you do need LDAP authentication into your application and/or need to attach data to those users as you would any Laravel application, it may make sense to configure the auth driver for this sync process.

Let me know if you have any further questions and I'd be happy to help 😄

kresnasatya commented 2 years ago

Hi @stevebauman, thanks for your info. I will try and give feedback few days later.