|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Adldap\Laravel\Commands; |
| 4 | + |
| 5 | +use Adldap\Laravel\Traits\ImportsUsers; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Illuminate\Support\Facades\Auth; |
| 9 | + |
| 10 | +class Import extends Command |
| 11 | +{ |
| 12 | + use ImportsUsers; |
| 13 | + |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'adldap:import'; |
| 20 | + |
| 21 | + /** |
| 22 | + * The console command description. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Imports users into the local database with a random 16 character hashed password.'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Execute the console command. |
| 30 | + * |
| 31 | + * @return mixed |
| 32 | + */ |
| 33 | + public function handle() |
| 34 | + { |
| 35 | + // Retrieve the Adldap instance. |
| 36 | + $adldap = $this->getAdldap(); |
| 37 | + |
| 38 | + if (!$adldap->getConnection()->isBound()) { |
| 39 | + // If the connection isn't bound yet, we'll connect to the server manually. |
| 40 | + $adldap->connect(); |
| 41 | + } |
| 42 | + |
| 43 | + // Retrieve all users. |
| 44 | + $users = $adldap->search()->users()->get(); |
| 45 | + |
| 46 | + $imported = 0; |
| 47 | + |
| 48 | + /** @var \Adldap\Models\User $user */ |
| 49 | + foreach ($users as $user) { |
| 50 | + try { |
| 51 | + // Import the user and then save the model. |
| 52 | + $model = $this->getModelFromAdldap($user); |
| 53 | + |
| 54 | + $this->saveModel($model); |
| 55 | + |
| 56 | + $imported++; |
| 57 | + |
| 58 | + Log::info("Imported user {$user->getCommonName()}"); |
| 59 | + } catch (\Exception $e) { |
| 60 | + Log::error("Unable to import user {$user->getCommonName()}. {$e->getMessage()}"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $this->info("Successfully imported {$imported} user(s)."); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function createModel() |
| 71 | + { |
| 72 | + $model = Auth::getProvider()->getModel(); |
| 73 | + |
| 74 | + return new $model(); |
| 75 | + } |
| 76 | +} |
0 commit comments