Skip to content

A free open source IT asset/license management system

Importer: Switch to accessor for created_by

snipe2478cf25d411
diff --git a/app/Importer/AccessoryImporter.php b/app/Importer/AccessoryImporter.php
index d600e68924..a74fa5e355 100644
--- a/app/Importer/AccessoryImporter.php
+++ b/app/Importer/AccessoryImporter.php
@@ -50,7 +50,7 @@ class AccessoryImporter extends ItemImporter
}
$this->log('No Matching Accessory, Creating a new one');
$accessory = new Accessory;
- $accessory->created_by = auth()->id();
+ $accessory->created_by = $this->created_by;
$this->item['model_number'] = $this->findCsvMatch($row, 'model_number');
$this->item['min_amt'] = $this->findCsvMatch($row, 'min_amt');
$accessory->fill($this->sanitizeItemForStoring($accessory));
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php
index 3988ba3f9d..2a1f005474 100644
--- a/app/Importer/AssetImporter.php
+++ b/app/Importer/AssetImporter.php
@@ -102,6 +102,14 @@ class AssetImporter extends ItemImporter
} else {
$this->log('No Matching Asset, Creating a new one');
$asset = new Asset;
+ // created_by is not in Asset's $fillable, so the value that
+ // ItemImporter::handle() puts on $this->item is stripped by
+ // sanitizeItemForStoring() before it reaches the model. Set
+ // it directly. Use $this->created_by (the property set by
+ // setCreatedBy() from both ItemImportRequest and
+ // ObjectImportCommand) rather than auth()->id() so CLI-run
+ // imports get the --user_id option value instead of null.
+ $asset->created_by = $this->created_by;
}
// If no status ID is found
diff --git a/app/Importer/AssetModelImporter.php b/app/Importer/AssetModelImporter.php
index 7a78cd1655..7344905b51 100644
--- a/app/Importer/AssetModelImporter.php
+++ b/app/Importer/AssetModelImporter.php
@@ -122,7 +122,7 @@ class AssetModelImporter extends ItemImporter
} else {
Log::debug('Creating model');
$assetModel->fill($this->sanitizeItemForStoring($assetModel));
- $assetModel->created_by = auth()->id();
+ $assetModel->created_by = $this->created_by;
}
if ($assetModel->save()) {
diff --git a/app/Importer/CategoryImporter.php b/app/Importer/CategoryImporter.php
index c547b4ea89..f2695017e3 100644
--- a/app/Importer/CategoryImporter.php
+++ b/app/Importer/CategoryImporter.php
@@ -60,7 +60,7 @@ class CategoryImporter extends ItemImporter
} else {
$this->log('No Matching Category, Create a new one');
$category = new Category;
- $category->created_by = auth()->id();
+ $category->created_by = $this->created_by;
}
// Pull the records from the CSV to determine their values
diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.php
index 0145997745..22573ecd21 100644
--- a/app/Importer/ComponentImporter.php
+++ b/app/Importer/ComponentImporter.php
@@ -49,7 +49,7 @@ class ComponentImporter extends ItemImporter
}
$this->log('No matching component, creating one');
$component = new Component;
- $component->created_by = auth()->id();
+ $component->created_by = $this->created_by;
$component->fill($this->sanitizeItemForStoring($component));
// This sets an attribute on the Loggable trait for the action log
@@ -68,7 +68,7 @@ class ComponentImporter extends ItemImporter
} else {
$component->assets()->attach($component->id, [
'component_id' => $component->id,
- 'created_by' => auth()->id(),
+ 'created_by' => $this->created_by,
'created_at' => date('Y-m-d H:i:s'),
'assigned_qty' => 1, // Only assign the first one to the asset
'asset_id' => $asset->id,
diff --git a/app/Importer/ConsumableImporter.php b/app/Importer/ConsumableImporter.php
index 7681c5b3dc..9b098cd0b9 100644
--- a/app/Importer/ConsumableImporter.php
+++ b/app/Importer/ConsumableImporter.php
@@ -52,7 +52,7 @@ class ConsumableImporter extends ItemImporter
$this->log('No matching consumable, creating one');
$consumable = new Consumable;
- $consumable->created_by = auth()->id();
+ $consumable->created_by = $this->created_by;
$consumable->fill($this->sanitizeItemForStoring($consumable));
// This sets an attribute on the Loggable trait for the action log
diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php
index 1ca53f7043..07a82f8d43 100644
--- a/app/Importer/Importer.php
+++ b/app/Importer/Importer.php
@@ -347,7 +347,11 @@ abstract class Importer
'display_name' => $this->findCsvMatch($row, 'display_name'),
'email' => $this->findCsvMatch($row, 'email'),
'manager_id' => '',
- 'department_id' => '',
+ // ItemImporter::handle() has already created the Department (if
+ // the CSV row had one) and stored its id on $this->item so it
+ // can flow through to the user record. Previously this value
+ // was hard-coded to '' and the Department was orphaned.
+ 'department_id' => $this->item['department_id'] ?? '',
'username' => $this->findCsvMatch($row, 'username'),
'activated' => $this->fetchHumanBoolean($this->findCsvMatch($row, 'activated')),
'remote' => $this->fetchHumanBoolean(($this->findCsvMatch($row, 'remote'))),
@@ -434,6 +438,13 @@ abstract class Importer
$user->department_id = $user_array['department_id'] ?? null;
$user->activated = 1;
$user->password = $this->tempPassword;
+ // Use $this->created_by (set by setCreatedBy() from both
+ // ItemImportRequest for web imports and ObjectImportCommand for
+ // CLI imports) rather than auth()->id() so CLI-run imports get
+ // the --user_id option value instead of null. Without this,
+ // users minted as checkout targets during asset import land in
+ // the DB with a null created_by, breaking blame in the user list.
+ $user->created_by = $this->created_by;
Log::debug('Creating a user with the following attributes: '.print_r($user_array, true));
@@ -576,25 +587,34 @@ abstract class Importer
*/
public function createOrFetchDepartment($user_department_name)
{
- if ($user_department_name != '') {
- $department = Department::where('name', '=', $user_department_name)->first();
+ // Explicit is_null check before the loose equality guard so a null
+ // input doesn't trigger PHP 8.x null-to-string deprecation warnings
+ // (the previous form was `!= ''` which coerces null to '' first).
+ if (is_null($user_department_name) || $user_department_name === '') {
+ return null;
+ }
- if ($department) {
- $this->log('A matching Department '.$user_department_name.' already exists');
+ $department = Department::where('name', $user_department_name)->first();
- return $department->id;
- }
+ if ($department) {
+ $this->log('A matching Department '.$user_department_name.' already exists');
- $department = new Department;
- $department->name = $user_department_name;
+ return $department->id;
+ }
- if ($department->save()) {
- $this->log('Department '.$user_department_name.' was created');
+ $department = new Department;
+ $department->name = $user_department_name;
+ // $this->created_by, not auth()->id(), so CLI-run imports
+ // attribute created_by to the --user_id option value rather
+ // than null.
+ $department->created_by = $this->created_by;
- return $department->id;
- }
- $this->logError($department, 'Department');
+ if ($department->save()) {
+ $this->log('Department '.$user_department_name.' was created');
+
+ return $department->id;
}
+ $this->logError($department, 'Department');
return null;
}
diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php
index aaaa6ea28f..12b452ac36 100644
--- a/app/Importer/ItemImporter.php
+++ b/app/Importer/ItemImporter.php
@@ -82,7 +82,7 @@ class ItemImporter extends Importer
$this->item['min_amt'] = $this->findCsvMatch($row, 'min_amt');
$this->item['qty'] = $this->findCsvMatch($row, 'quantity');
$this->item['requestable'] = $this->findCsvMatch($row, 'requestable');
- $this->item['created_by'] = auth()->id();
+ $this->item['created_by'] = $this->created_by;
$this->item['asset_tag'] = $this->findCsvMatch($row, 'asset_tag');
$this->item['serial'] = $this->findCsvMatch($row, 'serial');
$this->item['item_no'] = trim($this->findCsvMatch($row, 'item_no'));
@@ -256,7 +256,7 @@ class ItemImporter extends Importer
$this->log('No Matching Model, Creating a new one');
$asset_model = new AssetModel;
- $asset_model->created_by = auth()->id();
+ $asset_model->created_by = $this->created_by;
$item = $this->sanitizeItemForStoring($asset_model, $editingModel);
$item['name'] = $asset_model_name;
$item['model_number'] = $asset_modelNumber;
@@ -313,7 +313,7 @@ class ItemImporter extends Importer
}
$category = new Category;
- $category->created_by = auth()->id();
+ $category->created_by = $this->created_by;
$category->name = $asset_category;
$category->category_type = $item_type;
@@ -353,7 +353,7 @@ class ItemImporter extends Importer
return $company->id;
}
$company = new Company;
- $company->created_by = auth()->id();
+ $company->created_by = $this->created_by;
$company->name = $asset_company_name;
if ($company->save()) {
@@ -425,7 +425,7 @@ class ItemImporter extends Importer
}
$this->log('Creating a new status');
$status = new Statuslabel;
- $status->created_by = auth()->id();
+ $status->created_by = $this->created_by;
$status->name = trim($asset_statuslabel_name);
$status->deployable = 1;
@@ -469,7 +469,7 @@ class ItemImporter extends Importer
// Otherwise create a manufacturer.
$manufacturer = new Manufacturer;
$manufacturer->name = trim($item_manufacturer);
- $manufacturer->created_by = auth()->id();
+ $manufacturer->created_by = $this->created_by;
if ($manufacturer->save()) {
$this->log('Manufacturer '.$manufacturer->name.' was created');
@@ -520,7 +520,7 @@ class ItemImporter extends Importer
$location->city = '';
$location->state = '';
$location->country = '';
- $location->created_by = auth()->id();
+ $location->created_by = $this->created_by;
if ($location->save()) {
$this->log('Location '.$asset_location.' was created');
@@ -558,7 +558,7 @@ class ItemImporter extends Importer
$supplier = new Supplier;
$supplier->name = $item_supplier;
- $supplier->created_by = auth()->id();
+ $supplier->created_by = $this->created_by;
if ($supplier->save()) {
$this->log('Supplier '.$item_supplier.' was created');
diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.php
index 0d7639fd85..9ea29d9f56 100644
--- a/app/Importer/LicenseImporter.php
+++ b/app/Importer/LicenseImporter.php
@@ -85,7 +85,7 @@ class LicenseImporter extends ItemImporter
$license->update($this->sanitizeItemForUpdating($license));
} else {
$license->fill($this->sanitizeItemForStoring($license));
- $license->created_by = auth()->id();
+ $license->created_by = $this->created_by;
}
// This sets an attribute on the Loggable trait for the action log
@@ -116,7 +116,7 @@ class LicenseImporter extends ItemImporter
]));
} else {
$targetLicense->assigned_to = $checkout_target->id;
- $targetLicense->created_by = auth()->id();
+ $targetLicense->created_by = $this->created_by;
if ($asset) {
$targetLicense->asset_id = $asset->id;
}
@@ -130,7 +130,7 @@ class LicenseImporter extends ItemImporter
'target' => trans('general.asset').' "'.$asset->display_name.'"',
]));
} else {
- $targetLicense->created_by = auth()->id();
+ $targetLicense->created_by = $this->created_by;
$targetLicense->asset_id = $asset->id;
$targetLicense->save();
}
diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php
index f6c72efe77..4464c09f2c 100644
--- a/app/Importer/LocationImporter.php
+++ b/app/Importer/LocationImporter.php
@@ -60,7 +60,7 @@ class LocationImporter extends ItemImporter
} else {
$this->log('No Matching Location, Create a new one');
$location = new Location;
- $location->created_by = auth()->id();
+ $location->created_by = $this->created_by;
}
// Pull the records from the CSV to determine their values
diff --git a/app/Importer/ManufacturerImporter.php b/app/Importer/ManufacturerImporter.php
index e4f277f698..15497ba90a 100644
--- a/app/Importer/ManufacturerImporter.php
+++ b/app/Importer/ManufacturerImporter.php
@@ -60,7 +60,7 @@ class ManufacturerImporter extends ItemImporter
} else {
$this->log('No Matching Manufacturer, Create a new one');
$manufacturer = new Manufacturer;
- $manufacturer->created_by = auth()->id();
+ $manufacturer->created_by = $this->created_by;
}
// Pull the records from the CSV to determine their values
diff --git a/app/Importer/SupplierImporter.php b/app/Importer/SupplierImporter.php
index ccc1a97a60..f99d0c7a6b 100644
--- a/app/Importer/SupplierImporter.php
+++ b/app/Importer/SupplierImporter.php
@@ -60,7 +60,7 @@ class SupplierImporter extends ItemImporter
} else {
$this->log('No Matching Supplier, Create a new one');
$supplier = new Supplier;
- $supplier->created_by = auth()->id();
+ $supplier->created_by = $this->created_by;
}
// Pull the records from the CSV to determine their values
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index cca4c333b3..4c29189085 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -4,7 +4,6 @@ namespace App\Importer;
use App\Models\Asset;
use App\Models\Company;
-use App\Models\Department;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
@@ -232,7 +231,7 @@ class UserImporter extends ItemImporter
}
$user = new User;
- $user->created_by = auth()->id();
+ $user->created_by = $this->created_by;
$user->fill($this->sanitizeItemForStoring($user));
@@ -271,44 +270,6 @@ class UserImporter extends ItemImporter
$this->logError($user, 'User');
}
- /**
- * Fetch an existing department, or create new if it doesn't exist
- *
- * @author Daniel Melzter
- *
- * @since 5.0
- *
- * @param $department_name string
- * @return int id of department created/found
- */
- public function createOrFetchDepartment($department_name)
- {
- if (is_null($department_name) || $department_name == '') {
- return null;
- }
-
- $department = Department::where(['name' => $department_name])->first();
- if ($department) {
- $this->log('A matching department '.$department_name.' already exists');
-
- return $department->id;
- }
-
- $department = new Department;
- $department->name = $department_name;
- $department->created_by = $this->created_by;
-
- if ($department->save()) {
- $this->log('department '.$department_name.' was created');
-
- return $department->id;
- }
-
- $this->logError($department, 'Department');
-
- return null;
- }
-
public function sendWelcome($send = true)
{
$this->send_welcome = $send;