diff --git a/app/Importer/AccessoryImporter.php b/app/Importer/AccessoryImporter.php index 9901fb70d75d..eb17c5acadc0 100644 --- a/app/Importer/AccessoryImporter.php +++ b/app/Importer/AccessoryImporter.php @@ -46,10 +46,9 @@ public function createAccessoryIfNotExists($row) $this->item['min_amt'] = $this->findCsvMatch($row, "min_amt"); $accessory->fill($this->sanitizeItemForStoring($accessory)); - //FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything. - // $accessory->unsetEventDispatcher(); + // This sets an attribute on the Loggable trait for the action log + $accessory->setImported(true); if ($accessory->save()) { - $accessory->logCreate('Imported using CSV Importer'); $this->log('Accessory '.$this->item['name'].' was created'); return; diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index cf762a8fd44d..e001a383ad6f 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -135,10 +135,10 @@ public function createAssetIfNotExists(array $row) $asset->{$custom_field} = $val; } } - + // This sets an attribute on the Loggable trait for the action log + $asset->setImported(true); if ($asset->save()) { - $asset->logCreate(trans('general.importer.import_note')); $this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created'); // If we have a target to checkout to, lets do so. diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.php index 71ded1b0e5c4..f72d4cbfd7e6 100644 --- a/app/Importer/ComponentImporter.php +++ b/app/Importer/ComponentImporter.php @@ -48,10 +48,10 @@ public function createComponentIfNotExists() $this->log('No matching component, creating one'); $component = new Component; $component->fill($this->sanitizeItemForStoring($component)); - //FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything. - $component->unsetEventDispatcher(); + + // This sets an attribute on the Loggable trait for the action log + $component->setImported(true); if ($component->save()) { - $component->logCreate('Imported using CSV Importer'); $this->log('Component '.$this->item['name'].' was created'); // If we have an asset tag, checkout to that asset. diff --git a/app/Importer/ConsumableImporter.php b/app/Importer/ConsumableImporter.php index 5a65514deb61..9e7019b0861e 100644 --- a/app/Importer/ConsumableImporter.php +++ b/app/Importer/ConsumableImporter.php @@ -45,10 +45,10 @@ public function createConsumableIfNotExists($row) $this->item['item_no'] = trim($this->findCsvMatch($row, 'item_number')); $this->item['min_amt'] = trim($this->findCsvMatch($row, "min_amt")); $consumable->fill($this->sanitizeItemForStoring($consumable)); - //FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything. - $consumable->unsetEventDispatcher(); + + // This sets an attribute on the Loggable trait for the action log + $consumable->setImported(true); if ($consumable->save()) { - $consumable->logCreate('Imported using CSV Importer'); $this->log('Consumable '.$this->item['name'].' was created'); return; diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.php index 393d00367d49..b7c55cdba6c2 100644 --- a/app/Importer/LicenseImporter.php +++ b/app/Importer/LicenseImporter.php @@ -85,10 +85,10 @@ public function createLicenseIfNotExists(array $row) } else { $license->fill($this->sanitizeItemForStoring($license)); } - //FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything. - // $license->unsetEventDispatcher(); + + // This sets an attribute on the Loggable trait for the action log + $license->setImported(true); if ($license->save()) { - $license->logCreate('Imported using csv importer'); $this->log('License '.$this->item['name'].' with serial number '.$this->item['serial'].' was created'); // Lets try to checkout seats if the fields exist and we have seats. diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index bc08aa800a89..90e0e884f8b5 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -19,6 +19,9 @@ class Actionlog extends SnipeModel { use HasFactory; + // This is to manually set the source (via setActionSource()) for determineActionSource() + protected ?string $source = null; + protected $presenter = \App\Presenters\ActionlogPresenter::class; use SoftDeletes; use Presentable; @@ -341,7 +344,12 @@ public function getListingOfActionLogsChronologicalOrder() * @since v6.3.0 * @return string */ - public function determineActionSource() { + public function determineActionSource(): string + { + // This is a manually set source + if($this->source) { + return $this->source; + } // This is an API call if (((request()->header('content-type') && (request()->header('accept'))=='application/json')) @@ -358,4 +366,10 @@ public function determineActionSource() { return 'cli/unknown'; } + + // Manually sets $this->source for determineActionSource() + public function setActionSource($source = null): void + { + $this->source = $source; + } } diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index ce3a07f159ee..9e9355ea745d 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -8,6 +8,9 @@ trait Loggable { + // an attribute for setting whether or not the item was imported + public ?bool $imported = false; + /** * @author Daniel Meltzer * @since [v3.4] @@ -18,6 +21,11 @@ public function log() return $this->morphMany(Actionlog::class, 'item'); } + public function setImported(bool $bool): void + { + $this->imported = $bool; + } + /** * @author Daniel Meltzer * @since [v3.4] diff --git a/app/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.php index f97d166b173f..ddf29681be74 100644 --- a/app/Observers/AccessoryObserver.php +++ b/app/Observers/AccessoryObserver.php @@ -38,6 +38,9 @@ public function created(Accessory $accessory) $logAction->item_id = $accessory->id; $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); + if($accessory->imported) { + $logAction->setActionSource('importer'); + } $logAction->logaction('create'); } diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 2b0955fde6bb..8fcf2485a36e 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -109,6 +109,9 @@ public function created(Asset $asset) $logAction->item_id = $asset->id; $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); + if($asset->imported) { + $logAction->setActionSource('importer'); + } $logAction->logaction('create'); } diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.php index 57792022bb7e..a8bb386e8e85 100644 --- a/app/Observers/ComponentObserver.php +++ b/app/Observers/ComponentObserver.php @@ -38,6 +38,9 @@ public function created(Component $component) $logAction->item_id = $component->id; $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); + if($component->imported) { + $logAction->setActionSource('importer'); + } $logAction->logaction('create'); } diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php index b945196e20d0..1f0c777dcc54 100644 --- a/app/Observers/ConsumableObserver.php +++ b/app/Observers/ConsumableObserver.php @@ -38,6 +38,9 @@ public function created(Consumable $consumable) $logAction->item_id = $consumable->id; $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); + if($consumable->imported) { + $logAction->setActionSource('importer'); + } $logAction->logaction('create'); } diff --git a/app/Observers/LicenseObserver.php b/app/Observers/LicenseObserver.php index 1478aba113dc..06280209652b 100644 --- a/app/Observers/LicenseObserver.php +++ b/app/Observers/LicenseObserver.php @@ -38,6 +38,9 @@ public function created(License $license) $logAction->item_id = $license->id; $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); + if($license->imported) { + $logAction->setActionSource('importer'); + } $logAction->logaction('create'); } diff --git a/sample_csvs/components-sample.csv b/sample_csvs/components-sample.csv new file mode 100644 index 000000000000..0861abc8dd1d --- /dev/null +++ b/sample_csvs/components-sample.csv @@ -0,0 +1,2 @@ +Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Serial number,Category,Quantity +RTX 4080,2024-01-01,5000.00,Austin,Grokability,2790,123456789,GPU,10 \ No newline at end of file