DirectoryTree / LdapRecord-Laravel

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

Group filtering orWhere* not working #518

Closed extraric closed 1 year ago

extraric commented 1 year ago

Hello!

I try to get the groups that a user is a member of and filter for two possible beginning:

use LdapRecord\Models\ActiveDirectory\User

$groups = $user->groups()
    ->whereStartsWith('cn', 'QkApp')
    ->orWhereStartsWith('cn', 'Quentin')
    ->get()->pluck('cn')->flatten()->all();
// return []

$groups = $user->groups()
    ->whereStartsWith('cn', 'QkApp')
    ->get()->pluck('cn')->flatten()->all();
// return ['QkApp_admin']

If I just use one where filter, it works, but with orWhere it returns empty array; Am I writing something wrong?

extraric commented 1 year ago

Ok, I just get to work with:

$groups = $user->groups()
    ->orFilter(function ($q) {
        $q->whereStartsWith('cn', 'QkApp_')
          ->whereStartsWith('cn', 'Quentin_');
    })
    ->get()->pluck('cn')->flatten()->all();
stevebauman commented 1 year ago

Thanks for posting your solution @extraric. This is intended, as the $user->groups() call creates an underlying query with where clauses already attached. Adding an orWhere() will negate these clauses. Adding a nested orFilter with your clauses inside will properly construct the query: (&{clauses}(|(cn=QkApp_*)(cn=Quentin_*)))