Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 76c70f2

Browse files
committed
Separate import of users.
1 parent 93baebc commit 76c70f2

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/Commands/Import.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Adldap\Laravel\Commands;
44

55
use Adldap\Laravel\Traits\ImportsUsers;
6+
use Adldap\Models\User;
67
use Illuminate\Support\Facades\Log;
78
use Illuminate\Support\Facades\Auth;
89
use Illuminate\Console\Command;
@@ -43,27 +44,43 @@ public function handle()
4344
// Retrieve all users.
4445
$users = $adldap->search()->users()->get();
4546

47+
$imported = $this->import($users);
48+
49+
$this->info("Successfully imported {$imported} user(s).");
50+
}
51+
52+
/**
53+
* Imports the specified users and returns the total
54+
* number of users successfully imported.
55+
*
56+
* @param mixed $users
57+
*
58+
* @return int
59+
*/
60+
public function import($users = [])
61+
{
4662
$imported = 0;
4763

48-
/** @var \Adldap\Models\User $user */
4964
foreach ($users as $user) {
50-
try {
51-
// Import the user and then save the model.
52-
$model = $this->getModelFromAdldap($user);
65+
if ($user instanceof User) {
66+
try {
67+
// Import the user and then save the model.
68+
$model = $this->getModelFromAdldap($user);
5369

54-
$this->saveModel($model);
70+
$this->saveModel($model);
5571

56-
$imported++;
72+
$imported++;
5773

58-
// Log the successful import.
59-
Log::info("Imported user {$user->getCommonName()}");
60-
} catch (\Exception $e) {
61-
// Log the unsuccessful import.
62-
Log::error("Unable to import user {$user->getCommonName()}. {$e->getMessage()}");
74+
// Log the successful import.
75+
Log::info("Imported user {$user->getCommonName()}");
76+
} catch (\Exception $e) {
77+
// Log the unsuccessful import.
78+
Log::error("Unable to import user {$user->getCommonName()}. {$e->getMessage()}");
79+
}
6380
}
6481
}
6582

66-
$this->info("Successfully imported {$imported} user(s).");
83+
return $imported;
6784
}
6885

6986
/**

0 commit comments

Comments
 (0)