|
3 | 3 | namespace Adldap\Laravel\Commands;
|
4 | 4 |
|
5 | 5 | use Adldap\Laravel\Traits\ImportsUsers;
|
| 6 | +use Adldap\Models\User; |
6 | 7 | use Illuminate\Support\Facades\Log;
|
7 | 8 | use Illuminate\Support\Facades\Auth;
|
8 | 9 | use Illuminate\Console\Command;
|
@@ -43,27 +44,43 @@ public function handle()
|
43 | 44 | // Retrieve all users.
|
44 | 45 | $users = $adldap->search()->users()->get();
|
45 | 46 |
|
| 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 | + { |
46 | 62 | $imported = 0;
|
47 | 63 |
|
48 |
| - /** @var \Adldap\Models\User $user */ |
49 | 64 | 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); |
53 | 69 |
|
54 |
| - $this->saveModel($model); |
| 70 | + $this->saveModel($model); |
55 | 71 |
|
56 |
| - $imported++; |
| 72 | + $imported++; |
57 | 73 |
|
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 | + } |
63 | 80 | }
|
64 | 81 | }
|
65 | 82 |
|
66 |
| - $this->info("Successfully imported {$imported} user(s)."); |
| 83 | + return $imported; |
67 | 84 | }
|
68 | 85 |
|
69 | 86 | /**
|
|
0 commit comments