How to sync group memberships #555
-
Hi, from the docs: Now this setup:
Importing users and groups is described in the docs, but what about memberships? How can I sync group memberships to my intermediate table? It seems like this requires some custom logic and is not directly possible using this package, right? For me the workflow would be something like:
Is there a built-in way of doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @nilskretschmer,
You're correct -- there is no built-in way to do this. You will have to implement this yourself. Something like this may work for you after you've run a successful import of LDAP users: use App\Models\User as EloquentUser;
use App\Models\Group as EloquentGroup;
use LdapRecord\Laravel\Import\Synchronizer;
use LdapRecord\Models\ActiveDirectory\Group as LdapGroup;
$config = [
'sync_attributes' => [
'name' => 'cn'
],
];
$synchronizer = new Synchronizer(EloquentGroup::class, $config);
foreach (LdapGroup::get() as $ldapGroup) {
// Sync the group into the local database.
$eloquentGroup = $synchronizer->run($ldapGroup);
$eloquentGroup->save();
// Retrieve all of the LDAP group member's object GUIDs.
$memberObjectGuids = $ldapGroup->members()->get()->pluck('objectguid');
// Sync the local group's users with the matching object GUIDs from the already imported users.
$eloquentGroup->users()->sync(
EloquentUser::whereIn('objectguid', $memberObjectGuids)->get()
);
} |
Beta Was this translation helpful? Give feedback.
Hi @nilskretschmer,
You're correct -- there is no built-in way to do this. You will have to implement this yourself.
Something like this may work for you after you've run a successful import of LDAP users: