DirectoryTree / LdapRecord-Laravel

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

[Bug] Field 'password' doesn't have a default value #483

Closed le-pepe closed 1 year ago

le-pepe commented 1 year ago

Environment:

Describe the bug:

I am trying to import users without the import command, because I want my clients to choose their users to import.

I'm doing this and I can't solve it, at least in the documentation I haven't found anything

$users_ad = UserLdap::whereHas($scheme_attribute);
foreach ($request->get("users") as $u)
{
    $a = json_decode($u, true);
    $users_ad = $users_ad->orWhere($scheme_attribute, '=', $a[0]);
}
$users_ad = $users_ad->get();

$synchronizer = new Synchronizer(User::class, $config = [
                'sync_attributes' => [
                    'email' => $scheme_attribute,
                    'name' => 'cn'
                ],
                'sync_passwords' => true,
                'password_column' => 'password',
            ]
);
foreach ($users_ad as $u) {
    $synchronizer->run($u)->save();
}

This code returns this error

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into `users` (`guid`, `domain`, `email`, `name`, `updated_at`, `created_at`) values .....

I also want to add more data to the models in custom fields.

I found a method to add a callback but I don't really understand how to use it because I can't find it in the documentation

stevebauman commented 1 year ago

Hi @le-pepe,

You’re looking for UserSynchronizer:

https://github.com/DirectoryTree/LdapRecord-Laravel/blob/master/src/Import/UserSynchronizer.php

le-pepe commented 1 year ago

@stevebauman Thanks it worked.

stevebauman commented 1 year ago

Happy to help @le-pepe! Glad you’re up and running.