Skip to content

A free open source IT asset/license management system

Userimport fixes/improvements (#5884)

Daniel Meltzer92671823d863
diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php
index 7ed405b17b..c660038af0 100644
--- a/app/Importer/Importer.php
+++ b/app/Importer/Importer.php
@@ -60,7 +60,12 @@ abstract class Importer
'warranty_months' => 'warranty',
'full_name' => 'full name',
'email' => 'email',
- 'username' => 'username'
+ 'username' => 'username',
+ 'jobtitle' => 'job title',
+ 'employee_num' => 'employee number',
+ 'phone_number' => 'phone number',
+ 'first_name' => 'first name',
+ 'last_name' => 'last name',
];
/**
* Map of item fields->csv names
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 258f339060..0e3ff01b83 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -4,6 +4,7 @@ namespace App\Importer;
use App\Helpers\Helper;
use App\Models\User;
+use App\Notifications\WelcomeNotification;
class UserImporter extends ItemImporter
{
@@ -26,6 +27,7 @@ class UserImporter extends ItemImporter
*
* @author Daniel Melzter
* @since 4.0
+ * @param array $row
*/
public function createUserIfNotExists(array $row)
{
@@ -55,14 +57,24 @@ class UserImporter extends ItemImporter
$this->log("No matching user, creating one");
$user = new User();
$user->fill($this->sanitizeItemForStoring($user));
-
if ($user->save()) {
// $user->logCreate('Imported using CSV Importer');
$this->log("User " . $this->item["name"] . ' was created');
+ if($user->email) {
+ $data = [
+ 'email' => $user->email,
+ 'username' => $user->username,
+ 'first_name' => $user->first_name,
+ 'last_name' => $user->last_name,
+ 'password' => $this->tempPassword,
+ ];
+ $user->notify(new WelcomeNotification($data));
+ }
$user = null;
$this->item = null;
return;
}
+
$this->logError($user, 'User');
return;
}
diff --git a/tests/unit/ImporterTest.php b/tests/unit/ImporterTest.php
index ae80239d0b..37bb137b79 100644
--- a/tests/unit/ImporterTest.php
+++ b/tests/unit/ImporterTest.php
@@ -3,6 +3,7 @@ use App\Importer\AccessoryImporter;
use App\Importer\AssetImporter;
use App\Importer\ConsumableImporter;
use App\Importer\LicenseImporter;
+use App\Importer\UserImporter;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetModel;
@@ -14,6 +15,7 @@ use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Notification;
class ImporterTest extends BaseTest
{
@@ -681,6 +683,36 @@ EOT;
$this->tester->seeNumRecords(80, 'license_seats');
}
+
+ public function testDefaultUserImport()
+ {
+ Notification::fake();
+ $this->signIn();
+ $csv = <<<'EOT'
+First Name,Last Name,email,Username,Location,Phone Number,Job Title,Employee Number,Company
+Blanche,O'Collopy,[email protected],bocollopy0,Hinapalanan,63-(199)661-2186,Clinical Specialist,7080919053,Morar-Ward
+Jessie,Primo,,jprimo1,Korenovsk,7-(885)578-0266,Paralegal,6284292031,Jast-Stiedemann
+
+EOT;
+ $this->import(new UserImporter($csv));
+
+ $this->tester->seeRecord('users', [
+ 'first_name' => 'Blanche',
+ 'last_name' => "O'Collopy",
+ 'email' => '[email protected]',
+ 'username' => 'bocollopy0',
+ 'phone' => '63-(199)661-2186',
+ 'jobtitle' => 'Clinical Specialist',
+ 'employee_num' => '7080919053'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Morar-Ward'
+ ]);
+
+ Notification::assertSentTo(User::find(2), \App\Notifications\WelcomeNotification::class);
+ Notification::assertNotSentTo(User::find(3), \App\Notifications\WelcomeNotification::class);
+ }
private function import($importer, $mappings = null)
{
if ($mappings) {