From c9101f4d9789030936440d8f5c3b0ce2f6c4d339 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 25 Jan 2024 18:18:24 -0600 Subject: [PATCH 1/6] initial work, not working yet --- app/Http/Controllers/Api/AssetsController.php | 7 ++++++- app/Importer/AssetImporter.php | 3 ++- app/Models/Loggable.php | 12 ++++++++++++ app/Observers/AssetObserver.php | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index f5168a5914ac..3347f6dabcb0 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -599,7 +599,12 @@ public function store(StoreAssetRequest $request): JsonResponse $target = Location::find(request('assigned_location')); } if (isset($target)) { - $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name'))); + $asset->checkOut($target, + Auth::user(), + date('Y-m-d H:i:s'), + '', + 'Checked out on asset creation', + e($request->get('name'))); } if ($asset->image) { diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index cf762a8fd44d..870bf6d16213 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -138,7 +138,8 @@ public function createAssetIfNotExists(array $row) if ($asset->save()) { - $asset->logCreate(trans('general.importer.import_note')); + //$asset->logCreate(trans('general.importer.import_note')); + $asset->setImported(true); $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/Models/Loggable.php b/app/Models/Loggable.php index ce3a07f159ee..8d63d1988207 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -8,6 +8,8 @@ trait Loggable { + public ?bool $imported = false; // Import note attribute + /** * @author Daniel Meltzer * @since [v3.4] @@ -18,6 +20,16 @@ public function log() return $this->morphMany(Actionlog::class, 'item'); } + public function setImported(bool $bool): void + { + $this->imported = $bool; + } + + public function getImported(): bool + { + return $this->imported; + } + /** * @author Daniel Meltzer * @since [v3.4] diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 2b0955fde6bb..55cd05471d4d 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -109,6 +109,10 @@ 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(); + $logAction->note = 'poop'; + if($asset->getImported()) { + $logAction->note = "this asset was imported"; + } $logAction->logaction('create'); } From 4a7df470f04ae667a87ba3d0d4e65fa894690744 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 25 Jan 2024 19:34:41 -0600 Subject: [PATCH 2/6] this works --- app/Importer/AssetImporter.php | 5 ++--- app/Models/Loggable.php | 8 ++------ app/Observers/AssetObserver.php | 5 ++--- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 870bf6d16213..312c9b44b237 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -135,11 +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')); - $asset->setImported(true); $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/Models/Loggable.php b/app/Models/Loggable.php index 8d63d1988207..9e9355ea745d 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -8,7 +8,8 @@ trait Loggable { - public ?bool $imported = false; // Import note attribute + // an attribute for setting whether or not the item was imported + public ?bool $imported = false; /** * @author Daniel Meltzer @@ -25,11 +26,6 @@ public function setImported(bool $bool): void $this->imported = $bool; } - public function getImported(): bool - { - return $this->imported; - } - /** * @author Daniel Meltzer * @since [v3.4] diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 55cd05471d4d..c94a7046e003 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -109,9 +109,8 @@ 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(); - $logAction->note = 'poop'; - if($asset->getImported()) { - $logAction->note = "this asset was imported"; + if($asset->imported) { + $logAction->note = trans('general.importer.import_note'); } $logAction->logaction('create'); } From abf13f1619e1ac292b7f1f82996225ee43443125 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 25 Jan 2024 19:37:59 -0600 Subject: [PATCH 3/6] revert spacing --- app/Http/Controllers/Api/AssetsController.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 3347f6dabcb0..f5168a5914ac 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -599,12 +599,7 @@ public function store(StoreAssetRequest $request): JsonResponse $target = Location::find(request('assigned_location')); } if (isset($target)) { - $asset->checkOut($target, - Auth::user(), - date('Y-m-d H:i:s'), - '', - 'Checked out on asset creation', - e($request->get('name'))); + $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name'))); } if ($asset->image) { From 3574ef5bb9a4a929d709b65b78f6579935475c02 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 25 Jan 2024 19:54:53 -0600 Subject: [PATCH 4/6] a few more imports, component sample --- app/Importer/AccessoryImporter.php | 5 ++--- app/Importer/AssetImporter.php | 2 +- app/Importer/ComponentImporter.php | 6 +++--- app/Observers/AccessoryObserver.php | 3 +++ app/Observers/ComponentObserver.php | 3 +++ sample_csvs/components-sample.csv | 2 ++ 6 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 sample_csvs/components-sample.csv 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 312c9b44b237..e001a383ad6f 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -135,7 +135,7 @@ public function createAssetIfNotExists(array $row) $asset->{$custom_field} = $val; } } - //this sets an attribute on the Loggable trait for the action log + // This sets an attribute on the Loggable trait for the action log $asset->setImported(true); if ($asset->save()) { 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/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.php index f97d166b173f..99a594c5bcc7 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->note = trans('general.importer.import_note'); + } $logAction->logaction('create'); } diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.php index 57792022bb7e..e89006ad8529 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->note = trans('general.importer.import_note'); + } $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 From 2deba17d9105fa584da743020d36f126cf1394f2 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 25 Jan 2024 20:04:02 -0600 Subject: [PATCH 5/6] that's all of 'em --- app/Importer/ConsumableImporter.php | 6 +++--- app/Importer/LicenseImporter.php | 6 +++--- app/Observers/ConsumableObserver.php | 3 +++ app/Observers/LicenseObserver.php | 3 +++ 4 files changed, 12 insertions(+), 6 deletions(-) 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/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php index b945196e20d0..265e64d73eda 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->note = trans('general.importer.import_note'); + } $logAction->logaction('create'); } diff --git a/app/Observers/LicenseObserver.php b/app/Observers/LicenseObserver.php index 1478aba113dc..2d0f60a62593 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->note = trans('general.importer.import_note'); + } $logAction->logaction('create'); } From 63e733f0d6b553d2776f0ff6ca6cdd256f646a0c Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 30 Jan 2024 15:14:59 -0600 Subject: [PATCH 6/6] changes from a note to a source --- app/Models/Actionlog.php | 16 +++++++++++++++- app/Observers/AccessoryObserver.php | 2 +- app/Observers/AssetObserver.php | 2 +- app/Observers/ComponentObserver.php | 2 +- app/Observers/ConsumableObserver.php | 2 +- app/Observers/LicenseObserver.php | 2 +- 6 files changed, 20 insertions(+), 6 deletions(-) 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/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.php index 99a594c5bcc7..ddf29681be74 100644 --- a/app/Observers/AccessoryObserver.php +++ b/app/Observers/AccessoryObserver.php @@ -39,7 +39,7 @@ public function created(Accessory $accessory) $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); if($accessory->imported) { - $logAction->note = trans('general.importer.import_note'); + $logAction->setActionSource('importer'); } $logAction->logaction('create'); } diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index c94a7046e003..8fcf2485a36e 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -110,7 +110,7 @@ public function created(Asset $asset) $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); if($asset->imported) { - $logAction->note = trans('general.importer.import_note'); + $logAction->setActionSource('importer'); } $logAction->logaction('create'); } diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.php index e89006ad8529..a8bb386e8e85 100644 --- a/app/Observers/ComponentObserver.php +++ b/app/Observers/ComponentObserver.php @@ -39,7 +39,7 @@ public function created(Component $component) $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); if($component->imported) { - $logAction->note = trans('general.importer.import_note'); + $logAction->setActionSource('importer'); } $logAction->logaction('create'); } diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php index 265e64d73eda..1f0c777dcc54 100644 --- a/app/Observers/ConsumableObserver.php +++ b/app/Observers/ConsumableObserver.php @@ -39,7 +39,7 @@ public function created(Consumable $consumable) $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); if($consumable->imported) { - $logAction->note = trans('general.importer.import_note'); + $logAction->setActionSource('importer'); } $logAction->logaction('create'); } diff --git a/app/Observers/LicenseObserver.php b/app/Observers/LicenseObserver.php index 2d0f60a62593..06280209652b 100644 --- a/app/Observers/LicenseObserver.php +++ b/app/Observers/LicenseObserver.php @@ -39,7 +39,7 @@ public function created(License $license) $logAction->created_at = date('Y-m-d H:i:s'); $logAction->user_id = Auth::id(); if($license->imported) { - $logAction->note = trans('general.importer.import_note'); + $logAction->setActionSource('importer'); } $logAction->logaction('create'); }