From 58062ff9f502008c9d8ce964654b872e02e37688 Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 17:56:38 +0200 Subject: [PATCH 01/35] Update AssetModelsController.php if EOL of model has been changed and value is >0 assets will be updates with new EOL rate, date if EOL of model has been changes and value is null or 0 then assets will be updates by null EOL rate, date *asset with set expilicity market will not be updated --- app/Http/Controllers/AssetModelsController.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index d7a135dd76..1783b33921 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -179,9 +179,14 @@ class AssetModelsController extends Controller if ($model->save()) { if ($model->wasChanged('eol')) { - $newEol = $model->eol; - $model->assets()->whereNotNull('purchase_date')->where('eol_explicit', false) - ->update(['asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL ' . $newEol . ' MONTH)')]); + if ($model->eol > 0) { + $newEol = $model->eol; + $model->assets()->whereNotNull('purchase_date')->where('eol_explicit', false) + ->update(['asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL ' . $newEol . ' MONTH)')]); + } elseif ($model->eol == 0) { + $model->assets()->whereNotNull('purchase_date')->where('eol_explicit', false) + ->update(['asset_eol_date' => DB::raw('null')]); + } } return redirect()->route('models.index')->with('success', trans('admin/models/message.update.success')); } From bd195a691161932941e8a4258e9b3a137c498bf2 Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:11:19 +0200 Subject: [PATCH 02/35] Update AssetsController.php when asset is updated control EOL date change sand set explicit marker depends to setup of model EOL rate --- app/Http/Controllers/Assets/AssetsController.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index 92922c4cdf..8a12d744ae 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -137,7 +137,7 @@ class AssetsController extends Controller $asset->warranty_months = request('warranty_months', null); $asset->purchase_cost = request('purchase_cost'); $asset->purchase_date = request('purchase_date', null); - $asset->asset_eol_date = request('asset_eol_date', $asset->present()->eol_date()); + $asset->asset_eol_date = request('asset_eol_date', null); $asset->assigned_to = request('assigned_to', null); $asset->supplier_id = request('supplier_id', null); $asset->requestable = request('requestable', 0); @@ -309,14 +309,15 @@ class AssetsController extends Controller $asset->warranty_months = $request->input('warranty_months', null); $asset->purchase_cost = $request->input('purchase_cost', null); $asset->purchase_date = $request->input('purchase_date', null); - if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && $asset->model->eol) { + if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) { $asset->purchase_date = $request->input('purchase_date', null); $asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d'); + $asset->eol_explicit = false; } elseif ($request->filled('asset_eol_date')) { $asset->asset_eol_date = $request->input('asset_eol_date', null); $months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); if($asset->model->eol) { - if($months != $asset->model->eol) { + if($months != $asset->model->eol > 0) { $asset->eol_explicit = true; } else { $asset->eol_explicit = false; @@ -324,6 +325,9 @@ class AssetsController extends Controller } else { $asset->eol_explicit = true; } + } elseif (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) { + $asset->asset_eol_date = null; + $asset->eol_explicit = false; } $asset->supplier_id = $request->input('supplier_id', null); $asset->expected_checkin = $request->input('expected_checkin', null); From 5ae09b791f32896bd7a91b5c4a897b332873db7d Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:16:49 +0200 Subject: [PATCH 03/35] Update AssetsTransformer.php on list of assets display EOL rate in months if purchase date and eol date are set, otherwise null (not display anything) --- app/Http/Transformers/AssetsTransformer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 68dc731f07..70b0cfe8d2 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -7,6 +7,7 @@ use App\Models\Asset; use App\Models\Setting; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; +use Carbon\Carbon; class AssetsTransformer @@ -38,7 +39,7 @@ class AssetsTransformer 'byod' => ($asset->byod ? true : false), 'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null, - 'eol' => (($asset->model) && ($asset->model->eol != '')) ? $asset->model->eol : null, + 'eol' => (($asset->asset_eol_date != '') && ($asset->purchase_date != '')) ? Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date).' months' : null, 'asset_eol_date' => ($asset->asset_eol_date != '') ? Helper::getFormattedDateObject($asset->asset_eol_date, 'date') : null, 'status_label' => ($asset->assetstatus) ? [ 'id' => (int) $asset->assetstatus->id, From 30cc498a163ca869717c72de9a6b42b337aeef9e Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:23:30 +0200 Subject: [PATCH 04/35] Update AssetObserver.php update exlicit marker depends if model EOL > 0 , EOL = 0 --- app/Observers/AssetObserver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index c15c54a568..a440dbf4ff 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -137,14 +137,14 @@ class AssetObserver public function saving(Asset $asset) { // determine if calculated eol and then calculate it - this should only happen on a new asset - if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && !is_null($asset->model->eol)){ + if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && ($asset->model->eol > 0)){ $asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d'); $asset->eol_explicit = false; } // determine if explicit and set eol_explicit to true if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) { - if($asset->model->eol) { + if($asset->model->eol > 0) { $months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); if($months != $asset->model->eol) { $asset->eol_explicit = true; @@ -153,7 +153,7 @@ class AssetObserver } elseif (!is_null($asset->asset_eol_date) && is_null($asset->purchase_date)) { $asset->eol_explicit = true; } - if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model->eol))) { + if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model->eol) || ($asset->model->eol == 0))) { $asset->eol_explicit = true; } From 4e20a241ceb41c5910af4382573712dc58240894 Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:26:19 +0200 Subject: [PATCH 05/35] Update AssetPresenter.php display name 'EOL rate' instead of 'EOL' --- app/Presenters/AssetPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index c570c568da..de7c2c7709 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -173,7 +173,7 @@ class AssetPresenter extends Presenter 'searchable' => false, 'sortable' => true, 'visible' => false, - 'title' => trans('general.eol'), + 'title' => trans('admin/hardware/form.eol_rate'), ], [ 'field' => 'asset_eol_date', From e62636b3a550dbd528795d26451821beb255e9be Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:27:53 +0200 Subject: [PATCH 06/35] Update AssetModelPresenter.php display name 'EOL rate' instead of 'EOL' --- app/Presenters/AssetModelPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Presenters/AssetModelPresenter.php b/app/Presenters/AssetModelPresenter.php index e661b1ab80..85a0fa58ec 100644 --- a/app/Presenters/AssetModelPresenter.php +++ b/app/Presenters/AssetModelPresenter.php @@ -104,7 +104,7 @@ class AssetModelPresenter extends Presenter 'searchable' => false, 'sortable' => true, 'switchable' => true, - 'title' => trans('general.eol'), + 'title' => trans('admin/hardware/form.eol_rate'), 'visible' => true, ], [ From 04a867d12b81e80211e7280b63a4562c6979d64d Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:32:20 +0200 Subject: [PATCH 07/35] Update eol_date.blade.php read EOL date from database instead calculate on the base model EOL rate --- resources/views/partials/forms/edit/eol_date.blade.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/views/partials/forms/edit/eol_date.blade.php b/resources/views/partials/forms/edit/eol_date.blade.php index 88055cfc0e..fb461cf44e 100644 --- a/resources/views/partials/forms/edit/eol_date.blade.php +++ b/resources/views/partials/forms/edit/eol_date.blade.php @@ -1,9 +1,9 @@ - +
- +
{!! $errors->first('asset_eol_date', '') !!} From 309f30f630218f4901ca8d8529c2dfd050513e75 Mon Sep 17 00:00:00 2001 From: Robert-Azelis <82208283+Robert-Azelis@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:38:59 +0200 Subject: [PATCH 08/35] Update view.blade.php display marker if date of EOL is expired and show real EOL rate as diff between purchase date and eol date --- resources/views/hardware/view.blade.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index 7609feb35f..5a043ffc40 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -631,7 +631,7 @@
@endif - @if (($asset->model) && ($asset->model->eol)) + @if (($asset->asset_eol_date) && ($asset->purchase_date))
@@ -639,7 +639,7 @@
- {{ $asset->model->eol }} + {{ Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date) }} {{ trans('admin/hardware/form.months') }}
@@ -650,6 +650,9 @@
{{ trans('admin/hardware/form.eol_date') }} + @if ($asset->purchase_date) + {!! $asset->asset_eol_date < date("Y-m-d") ? '' : '' !!} + @endif
From 7ed58a177b0361c73e74ae18ba19792bc55603bd Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 25 Oct 2023 16:31:23 -0600 Subject: [PATCH 09/35] Evaluate if the asset was assigned to a different user than current target to log a checkin event --- app/Importer/AssetImporter.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 76eae0739a..61cdd3c7b7 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -5,6 +5,9 @@ namespace App\Importer; use App\Models\Asset; use App\Models\AssetModel; use App\Models\Statuslabel; +use App\Models\User; +use App\Events\CheckoutableCheckedIn; +use Illuminate\Support\Facades\Auth; use Carbon\Carbon; class AssetImporter extends ItemImporter @@ -142,6 +145,10 @@ class AssetImporter extends ItemImporter //-- user_id is a property of the abstract class Importer, which this class inherits from and it's setted by //-- the class that needs to use it (command importer or GUI importer inside the project). if (isset($target)) { + if ($asset->assigned_to != $target->id){ + event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), $asset->notes, date('Y-m-d H:i:s'))); + } + $asset->fresh()->checkOut($target, $this->user_id, date('Y-m-d H:i:s'), null, $asset->notes, $asset->name); } From ca1845efd777d3eb23bb474309a17607253d024e Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 25 Oct 2023 16:41:31 -0600 Subject: [PATCH 10/35] Only log checkin event if the assigned_to property is not empty --- app/Importer/AssetImporter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 61cdd3c7b7..5014efac12 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -145,8 +145,10 @@ class AssetImporter extends ItemImporter //-- user_id is a property of the abstract class Importer, which this class inherits from and it's setted by //-- the class that needs to use it (command importer or GUI importer inside the project). if (isset($target)) { - if ($asset->assigned_to != $target->id){ - event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), $asset->notes, date('Y-m-d H:i:s'))); + if (!is_null($asset->assigned_to)){ + if ($asset->assigned_to != $target->id){ + event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), $asset->notes, date('Y-m-d H:i:s'))); + } } $asset->fresh()->checkOut($target, $this->user_id, date('Y-m-d H:i:s'), null, $asset->notes, $asset->name); From 4a759f0a204dceb596b305d15b4485601572c15c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 01:49:01 +0100 Subject: [PATCH 11/35] Added fingerprint icon for unique Signed-off-by: snipe --- resources/views/custom_fields/index.blade.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/views/custom_fields/index.blade.php b/resources/views/custom_fields/index.blade.php index 7b0c4965d8..08f2fb3845 100644 --- a/resources/views/custom_fields/index.blade.php +++ b/resources/views/custom_fields/index.blade.php @@ -151,6 +151,7 @@ + {{ trans('admin/custom_fields/general.unique') }} {{ trans('admin/custom_fields/general.field_element_short') }} {{ trans('admin/custom_fields/general.fieldsets') }} {{ trans('button.actions') }} @@ -176,6 +177,7 @@ {!! ($field->display_in_user_view=='1' ? '' : '') !!} {!! ($field->show_in_email=='1') ? '' : '' !!} {!! ($field->show_in_requestable_list=='1') ? '' : '' !!} + {!! ($field->is_unique=='1') ? '' : '' !!} {{ $field->element }} @foreach($field->fieldset as $fieldset) From 4b6b36c6397f789978c5c2bff870f4fd5fbfdf39 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 01:49:14 +0100 Subject: [PATCH 12/35] Removed semicolon Signed-off-by: snipe --- resources/lang/en/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index e1a5cc0e7a..fba828839f 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -368,7 +368,7 @@ return [ 'consumables_count' => 'Consumables Count', 'components_count' => 'Components Count', 'licenses_count' => 'Licenses Count', - 'notification_error' => 'Error:', + 'notification_error' => 'Error', 'notification_error_hint' => 'Please check the form below for errors', 'notification_bulk_error_hint' => 'The following fields had validation errors and were not edited:', 'notification_success' => 'Success', From 6ebc01ca50c65a8fe6d6621dca80fb7be2f3dd5e Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 01:49:23 +0100 Subject: [PATCH 13/35] Bulk edit refactor Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 140 ++++++++++++------ 1 file changed, 92 insertions(+), 48 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index bd234cb9a6..fe151b4278 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -120,8 +120,9 @@ class BulkAssetsController extends Controller $bulk_back_url = $request->session()->pull('bulk_back_url'); } + \Log::debug(print_r($request->all(), true)); - $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); + $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); if(Session::exists('ids')) { $assets = Session::get('ids'); @@ -156,13 +157,14 @@ class BulkAssetsController extends Controller ) { foreach ($assets as $assetId) { + \Log::debug('Asset ID is: '.$assetId); $this->update_array = []; $this->conditionallyAddItem('purchase_date') ->conditionallyAddItem('expected_checkin') - ->conditionallyAddItem('model_id') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') + ->conditionallyAddItem('model_id') ->conditionallyAddItem('status_id') ->conditionallyAddItem('supplier_id') ->conditionallyAddItem('warranty_months') @@ -187,6 +189,7 @@ class BulkAssetsController extends Controller $this->update_array['purchase_cost'] = $request->input('purchase_cost'); } + if ($request->filled('company_id')) { $this->update_array['company_id'] = $request->input('company_id'); if ($request->input('company_id') == 'clear') { @@ -208,61 +211,101 @@ class BulkAssetsController extends Controller } $changed = []; - $assetCollection = Asset::where('id' ,$assetId)->get(); + $asset = Asset::find($assetId); + + //dd($this->update_array); foreach ($this->update_array as $key => $value) { - if ($this->update_array[$key] != $assetCollection->toArray()[0][$key]) { - $changed[$key]['old'] = $assetCollection->toArray()[0][$key]; + if ($this->update_array[$key] != $asset->{$key}) { + $changed[$key]['old'] = $asset->{$key}; $changed[$key]['new'] = $this->update_array[$key]; } } + \Log::debug('$this->update_array:'); + \Log::debug(print_r($this->update_array, true)); - $logAction = new Actionlog(); - $logAction->item_type = Asset::class; - $logAction->item_id = $assetId; - $logAction->created_at = date("Y-m-d H:i:s"); - $logAction->user_id = Auth::id(); - $logAction->log_meta = json_encode($changed); - $logAction->logaction('update'); + \Log::debug('$changed'); + \Log::debug(print_r($changed, true)); - if($custom_fields_present) { - $asset = Asset::find($assetId); - $assetCustomFields = $asset->model()->first()->fieldset; - if($assetCustomFields && $assetCustomFields->fields) { - foreach ($assetCustomFields->fields as $field) { - if (array_key_exists($field->db_column, $this->update_array)) { - $asset->{$field->db_column} = $this->update_array[$field->db_column]; - $saved = $asset->save(); - if(!$saved) { - $error_bag[] = $asset->getErrors(); - } - continue; - } else { - $array = $this->update_array; - array_except($array, $field->db_column); - $asset->save($array); - } - if (!$asset->save()) { - $error_bag[] = $asset->getErrors(); - } - } - } - } else { - Asset::find($assetId)->update($this->update_array); - } - } - if(!empty($error_bag)) { - $errors = []; - //find the customfield name from the name of the messagebag items - foreach ($error_bag as $key => $bag) { - foreach($bag->keys() as $key => $value) { - CustomField::where('db_column', $value)->get()->map(function($item) use (&$errors) { - $errors[] = $item->name; - }); + if ($custom_fields_present) { + \Log::debug('Custom fields are present'); + + $model = $asset->model()->first(); + // Make sure this model is valid + $assetCustomFields = ($model) ? $asset->model()->first()->fieldset : null; + + if ($assetCustomFields && $assetCustomFields->fields) { + + \Log::debug('There are custom fields on '.$assetCustomFields->name); + + foreach ($assetCustomFields->fields as $field) { + + if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted=='1')) { + \Log::debug('The custom field is encrypted!'); + $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); + + // Check if the decrypted existing value is different than one we just submitted + // and if not, pull it out of the object + if ($decrypted_old != $this->update_array[$field->db_column]) { + \Log::debug('The decrypted existing value is different than the one submitted'); + $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); + } else { + \Log::debug('The decrypted existing value is the same as the one submitted - unset it'); + unset($this->update_array[$field->db_column]); + unset($asset->{$field->db_column}); + } + + // These fields aren't encrypted, just carry on as usual + } else { + if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { + \Log::debug('The custom field value is different than the original one'); + $asset->{$field->db_column} = $this->update_array[$field->db_column]; + } + } + + } // endforeach + } // end custom field check + } // end custom fields handler + + + // Check if it passes validation, and then try to save + $has_errors = 0; + $error_array = array(); + + if ($asset->isValid()) { + if ($asset->update($this->update_array)) { + $logAction = new Actionlog(); + $logAction->item_type = Asset::class; + $logAction->item_id = $assetId; + $logAction->created_at = date("Y-m-d H:i:s"); + $logAction->user_id = Auth::id(); + $logAction->log_meta = json_encode($changed); + $logAction->logaction('update'); + } + + \Log::debug('New asset info:'); + \Log::debug('Model name: '.$asset->model->name); + \Log::debug('Asset ID '.$asset->id.' with custom fields bulk edited successfully'); + + } else { + $has_errors++; + \Log::debug(print_r($asset->getErrors(), true)); + // Build the error array + foreach ($asset->getErrors()->toArray() as $key => $message) { + for ($x = 0; $x < count($message); $x++) { + $error_array[][] = trans('general.asset').' '.$asset->id.': '.$message[$x]; + } } } - return redirect($bulk_back_url)->with('bulk_errors', array_unique($errors)); - } + + } // end asset foreach + + if ($has_errors > 0) { + \Log::debug('Ooops!'); + dd($error_array); + return redirect($bulk_back_url)->with('bulk_errors', array_unique($error_array)); + } + return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.update.success')); } // no values given, nothing to update @@ -284,6 +327,7 @@ class BulkAssetsController extends Controller { if (request()->filled($field)) { $this->update_array[$field] = request()->input($field); + \Log::debug('Conditionally adding '.$field); } return $this; From 60e0e899bc0a203bbc1439e1afcff3e8d4c6fcaf Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 02:35:33 +0100 Subject: [PATCH 14/35] Added show_in_requestable_list to factory Signed-off-by: snipe --- database/factories/CustomFieldFactory.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/factories/CustomFieldFactory.php b/database/factories/CustomFieldFactory.php index 2dfa07d61a..937c66fb0b 100644 --- a/database/factories/CustomFieldFactory.php +++ b/database/factories/CustomFieldFactory.php @@ -26,6 +26,7 @@ class CustomFieldFactory extends Factory 'format' => '', 'element' => 'text', 'auto_add_to_fieldsets' => '0', + 'show_in_requestable_list' => '0', ]; } @@ -66,6 +67,7 @@ class CustomFieldFactory extends Factory return [ 'name' => 'CPU', 'help_text' => 'The speed of the processor on this device.', + 'show_in_requestable_list' => '1', ]; }); } From b87879f8e7f4f074fdca7a22b8eb05322d292616 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 02:35:53 +0100 Subject: [PATCH 15/35] Removed debugging Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index fe151b4278..02119edf6e 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -112,7 +112,8 @@ class BulkAssetsController extends Controller public function update(Request $request) { $this->authorize('update', Asset::class); - $error_bag = []; + $has_errors = 0; + $error_array = array(); // Get the back url from the session and then destroy the session $bulk_back_url = route('hardware.index'); @@ -120,11 +121,9 @@ class BulkAssetsController extends Controller $bulk_back_url = $request->session()->pull('bulk_back_url'); } - \Log::debug(print_r($request->all(), true)); - $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); - if(Session::exists('ids')) { + if (Session::exists('ids')) { $assets = Session::get('ids'); } elseif (! $request->filled('ids') || count($request->input('ids')) <= 0) { return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected')); @@ -157,7 +156,6 @@ class BulkAssetsController extends Controller ) { foreach ($assets as $assetId) { - \Log::debug('Asset ID is: '.$assetId); $this->update_array = []; $this->conditionallyAddItem('purchase_date') @@ -221,14 +219,13 @@ class BulkAssetsController extends Controller $changed[$key]['new'] = $this->update_array[$key]; } } - \Log::debug('$this->update_array:'); - \Log::debug(print_r($this->update_array, true)); - - \Log::debug('$changed'); - \Log::debug(print_r($changed, true)); +// \Log::debug('$this->update_array:'); +// \Log::debug(print_r($this->update_array, true)); +// +// \Log::debug('$changed'); +// \Log::debug(print_r($changed, true)); if ($custom_fields_present) { - \Log::debug('Custom fields are present'); $model = $asset->model()->first(); // Make sure this model is valid @@ -236,18 +233,15 @@ class BulkAssetsController extends Controller if ($assetCustomFields && $assetCustomFields->fields) { - \Log::debug('There are custom fields on '.$assetCustomFields->name); foreach ($assetCustomFields->fields as $field) { if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted=='1')) { - \Log::debug('The custom field is encrypted!'); $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); // Check if the decrypted existing value is different than one we just submitted // and if not, pull it out of the object if ($decrypted_old != $this->update_array[$field->db_column]) { - \Log::debug('The decrypted existing value is different than the one submitted'); $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); } else { \Log::debug('The decrypted existing value is the same as the one submitted - unset it'); @@ -258,7 +252,6 @@ class BulkAssetsController extends Controller // These fields aren't encrypted, just carry on as usual } else { if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { - \Log::debug('The custom field value is different than the original one'); $asset->{$field->db_column} = $this->update_array[$field->db_column]; } } @@ -269,41 +262,37 @@ class BulkAssetsController extends Controller // Check if it passes validation, and then try to save - $has_errors = 0; - $error_array = array(); - - if ($asset->isValid()) { - if ($asset->update($this->update_array)) { - $logAction = new Actionlog(); - $logAction->item_type = Asset::class; - $logAction->item_id = $assetId; - $logAction->created_at = date("Y-m-d H:i:s"); - $logAction->user_id = Auth::id(); - $logAction->log_meta = json_encode($changed); - $logAction->logaction('update'); - } - - \Log::debug('New asset info:'); - \Log::debug('Model name: '.$asset->model->name); - \Log::debug('Asset ID '.$asset->id.' with custom fields bulk edited successfully'); + if ($asset->update($this->update_array)) { + $logAction = new Actionlog(); + $logAction->item_type = Asset::class; + $logAction->item_id = $assetId; + $logAction->created_at = date("Y-m-d H:i:s"); + $logAction->user_id = Auth::id(); + $logAction->log_meta = json_encode($changed); + $logAction->logaction('update'); } else { - $has_errors++; + + \Log::debug('Error bag:'); \Log::debug(print_r($asset->getErrors(), true)); // Build the error array foreach ($asset->getErrors()->toArray() as $key => $message) { for ($x = 0; $x < count($message); $x++) { - $error_array[][] = trans('general.asset').' '.$asset->id.': '.$message[$x]; + $error_array[$key][] = trans('general.asset') . ' ' . $asset->id . ': ' . $message[$x]; + $has_errors++; } } - } + + } // end if saved } // end asset foreach + \Log::debug($has_errors.' errors'); if ($has_errors > 0) { - \Log::debug('Ooops!'); - dd($error_array); - return redirect($bulk_back_url)->with('bulk_errors', array_unique($error_array)); + \Log::debug('Error array:'); + \Log::debug(print_r($error_array, true)); + //dd($error_array); + return redirect($bulk_back_url)->with('bulk_asset_errors', $error_array); } return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.update.success')); @@ -327,7 +316,6 @@ class BulkAssetsController extends Controller { if (request()->filled($field)) { $this->update_array[$field] = request()->input($field); - \Log::debug('Conditionally adding '.$field); } return $this; From 522aa96fcc016a50c70f14c1c5e6c428d5f50a88 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 02:36:18 +0100 Subject: [PATCH 16/35] Changed error variable name Signed-off-by: snipe --- resources/views/notifications.blade.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/resources/views/notifications.blade.php b/resources/views/notifications.blade.php index eca773e219..9dbb547515 100755 --- a/resources/views/notifications.blade.php +++ b/resources/views/notifications.blade.php @@ -115,17 +115,19 @@ @endif -@if ($messages = Session::get('bulk_errors')) +@if ($messages = Session::get('bulk_asset_errors'))
{{ trans('general.notification_error') }}: {{ trans('general.notification_bulk_error_hint') }} - @foreach($messages as $message) + @foreach($messages as $key => $message) + @for ($x = 0; $x < count($message); $x++)
    -
  • {{ $message }}
  • -
+
  • {{ $message[$x] }}
  • + + @endfor @endforeach
    From 436192b8362ddedf84da1a91d4db09b048cf8b5f Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 02:56:34 +0100 Subject: [PATCH 17/35] Fixed seeder Signed-off-by: snipe --- database/factories/ActionlogFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/factories/ActionlogFactory.php b/database/factories/ActionlogFactory.php index c25fdcc70a..1a4007888c 100644 --- a/database/factories/ActionlogFactory.php +++ b/database/factories/ActionlogFactory.php @@ -38,7 +38,7 @@ class ActionlogFactory extends Factory { return $this->state(function () { $target = User::inRandomOrder()->first(); - $asset = Asset::RTD()->inRandomOrder()->first(); + $asset = Asset::inRandomOrder()->RTD()->first(); $asset->update( [ From 77d141d19c005c3a1487a8b10e3847568bceb35b Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 03:56:02 +0100 Subject: [PATCH 18/35] =?UTF-8?q?Don=E2=80=99t=20check=20the=20delete=20ex?= =?UTF-8?q?pected=20checkin=20by=20detault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: snipe --- resources/views/hardware/bulk.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index e26b957057..a7e52dfa56 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -62,7 +62,7 @@
    From 541ba0c0ba406caa58e137e2a09385acf0e0a5a2 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 04:20:52 +0100 Subject: [PATCH 19/35] Removed conditional add for model_id Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 02119edf6e..1ec73e5390 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -162,7 +162,6 @@ class BulkAssetsController extends Controller ->conditionallyAddItem('expected_checkin') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') - ->conditionallyAddItem('model_id') ->conditionallyAddItem('status_id') ->conditionallyAddItem('supplier_id') ->conditionallyAddItem('warranty_months') From f25ddee8571b53e11a6a6e06c9c530d0d53e3e92 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 04:21:14 +0100 Subject: [PATCH 20/35] Removed debugging, use new model rules if model has changed Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 1ec73e5390..44d2498edf 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -210,35 +210,35 @@ class BulkAssetsController extends Controller $changed = []; $asset = Asset::find($assetId); - //dd($this->update_array); - foreach ($this->update_array as $key => $value) { if ($this->update_array[$key] != $asset->{$key}) { $changed[$key]['old'] = $asset->{$key}; $changed[$key]['new'] = $this->update_array[$key]; } } -// \Log::debug('$this->update_array:'); -// \Log::debug(print_r($this->update_array, true)); -// -// \Log::debug('$changed'); -// \Log::debug(print_r($changed, true)); if ($custom_fields_present) { $model = $asset->model()->first(); + + // Use the rules of the new model fieldsets if the model changed + if ($request->filled('model_id')) { + $this->update_array['model_id'] = $request->input('model_id'); + $model = \App\Models\AssetModel::find($request->input('model_id')); + } + + // Make sure this model is valid - $assetCustomFields = ($model) ? $asset->model()->first()->fieldset : null; + $assetCustomFields = ($model) ? $model->fieldset : null; if ($assetCustomFields && $assetCustomFields->fields) { - foreach ($assetCustomFields->fields as $field) { if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted=='1')) { $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); - // Check if the decrypted existing value is different than one we just submitted + // Check if the decrypted existing value is different from one we just submitted // and if not, pull it out of the object if ($decrypted_old != $this->update_array[$field->db_column]) { $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); @@ -252,6 +252,10 @@ class BulkAssetsController extends Controller } else { if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { $asset->{$field->db_column} = $this->update_array[$field->db_column]; + if (is_array($this->update_array[$field->db_column])) { + $asset->{$field->db_column} = implode(', ', $this->update_array[$field->db_column]); + } + } } From 269557696879fa958aedeec822436d43bdc15ef7 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 04:21:20 +0100 Subject: [PATCH 21/35] Removed debugging Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 44d2498edf..bd41c7582f 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -290,11 +290,7 @@ class BulkAssetsController extends Controller } // end asset foreach - \Log::debug($has_errors.' errors'); if ($has_errors > 0) { - \Log::debug('Error array:'); - \Log::debug(print_r($error_array, true)); - //dd($error_array); return redirect($bulk_back_url)->with('bulk_asset_errors', $error_array); } From 7042260871927f3a24c11c2b0f771e8df00e824b Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 04:23:11 +0100 Subject: [PATCH 22/35] More style polishing on custom fields in bulk edit Signed-off-by: snipe --- .../custom_fields_form_bulk_edit.blade.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/resources/views/models/custom_fields_form_bulk_edit.blade.php b/resources/views/models/custom_fields_form_bulk_edit.blade.php index e974c3f6eb..f30c60d331 100644 --- a/resources/views/models/custom_fields_form_bulk_edit.blade.php +++ b/resources/views/models/custom_fields_form_bulk_edit.blade.php @@ -37,21 +37,20 @@ @elseif ($field->element=='checkbox') @foreach ($field->formatFieldValuesAsArray() as $key => $value) -
    - -
    + + @endforeach @elseif ($field->element=='radio') @foreach ($field->formatFieldValuesAsArray() as $value) -
    -
    + @endforeach @endif From 6c996ac8faeddf1c8bdd182345b32ef8da89348d Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 05:03:59 +0100 Subject: [PATCH 23/35] Changed the seeder order to accomodate actionlog factory Signed-off-by: snipe --- database/seeders/DatabaseSeeder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 1429604139..5e26a9a257 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -38,12 +38,13 @@ class DatabaseSeeder extends Seeder $this->call(DepreciationSeeder::class); $this->call(StatuslabelSeeder::class); $this->call(AccessorySeeder::class); + $this->call(CustomFieldSeeder::class); $this->call(AssetSeeder::class); $this->call(LicenseSeeder::class); $this->call(ComponentSeeder::class); $this->call(ConsumableSeeder::class); $this->call(ActionlogSeeder::class); - $this->call(CustomFieldSeeder::class); + Artisan::call('snipeit:sync-asset-locations', ['--output' => 'all']); $output = Artisan::output(); From 3717d60170640e201a85b2d272f7aa48789ec9cf Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 05:04:30 +0100 Subject: [PATCH 24/35] Added two more custom fields/fieldsets to seeder Signed-off-by: snipe --- database/factories/CustomFieldFactory.php | 22 +++++++++++++++++ database/seeders/CustomFieldSeeder.php | 30 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/database/factories/CustomFieldFactory.php b/database/factories/CustomFieldFactory.php index 937c66fb0b..53f23a205f 100644 --- a/database/factories/CustomFieldFactory.php +++ b/database/factories/CustomFieldFactory.php @@ -81,4 +81,26 @@ class CustomFieldFactory extends Factory ]; }); } + + public function testEncrypted() + { + return $this->state(function () { + return [ + 'name' => 'Test Encrypted', + 'help_text' => 'This is a test encrypted field.', + ]; + }); + } + + public function testCheckbox() + { + return $this->state(function () { + return [ + 'name' => 'Test Checkbox', + 'help_text' => 'This is a test checkbox.', + 'field_values' => "One\nTwo\nThree", + 'element' => 'checkbox', + ]; + }); + } } diff --git a/database/seeders/CustomFieldSeeder.php b/database/seeders/CustomFieldSeeder.php index 551e05f40f..e51ca510f9 100644 --- a/database/seeders/CustomFieldSeeder.php +++ b/database/seeders/CustomFieldSeeder.php @@ -33,6 +33,9 @@ class CustomFieldSeeder extends Seeder CustomField::factory()->count(1)->ram()->create(); CustomField::factory()->count(1)->cpu()->create(); CustomField::factory()->count(1)->macAddress()->create(); + CustomField::factory()->count(1)->testEncrypted()->create(); + CustomField::factory()->count(1)->testCheckbox()->create(); + DB::table('custom_field_custom_fieldset')->insert([ [ @@ -66,6 +69,33 @@ class CustomFieldSeeder extends Seeder 'required' => 0, ], + [ + 'custom_field_id' => '6', + 'custom_fieldset_id' => '2', + 'order' => 0, + 'required' => 0, + ], + + [ + 'custom_field_id' => '6', + 'custom_fieldset_id' => '1', + 'order' => 0, + 'required' => 0, + ], + + [ + 'custom_field_id' => '7', + 'custom_fieldset_id' => '2', + 'order' => 0, + 'required' => 0, + ], + [ + 'custom_field_id' => '7', + 'custom_fieldset_id' => '1', + 'order' => 0, + 'required' => 0, + ], + ]); } } From c3b3aa4de630dfd1e7d3c83d9966734c3ec0cd30 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 06:25:51 +0100 Subject: [PATCH 25/35] Set one field to encrypted in the seeder Signed-off-by: snipe --- database/factories/CustomFieldFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/factories/CustomFieldFactory.php b/database/factories/CustomFieldFactory.php index 53f23a205f..142adacdcf 100644 --- a/database/factories/CustomFieldFactory.php +++ b/database/factories/CustomFieldFactory.php @@ -87,6 +87,7 @@ class CustomFieldFactory extends Factory return $this->state(function () { return [ 'name' => 'Test Encrypted', + 'field_encrypted' => '1', 'help_text' => 'This is a test encrypted field.', ]; }); From 0cebccac95ee04689cf992acfc49b8a768ac0c6c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 06:51:07 +0100 Subject: [PATCH 26/35] Removed extra logging Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index bd41c7582f..d17dbcb3bb 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -250,12 +250,16 @@ class BulkAssetsController extends Controller // These fields aren't encrypted, just carry on as usual } else { + + if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { - $asset->{$field->db_column} = $this->update_array[$field->db_column]; + + // Check if this is an array, and if so, flatten it if (is_array($this->update_array[$field->db_column])) { $asset->{$field->db_column} = implode(', ', $this->update_array[$field->db_column]); + } else { + $asset->{$field->db_column} = $this->update_array[$field->db_column]; } - } } @@ -264,20 +268,10 @@ class BulkAssetsController extends Controller } // end custom fields handler + // Check if it passes validation, and then try to save - if ($asset->update($this->update_array)) { - $logAction = new Actionlog(); - $logAction->item_type = Asset::class; - $logAction->item_id = $assetId; - $logAction->created_at = date("Y-m-d H:i:s"); - $logAction->user_id = Auth::id(); - $logAction->log_meta = json_encode($changed); - $logAction->logaction('update'); + if (!$asset->update($this->update_array)) { - } else { - - \Log::debug('Error bag:'); - \Log::debug(print_r($asset->getErrors(), true)); // Build the error array foreach ($asset->getErrors()->toArray() as $key => $message) { for ($x = 0; $x < count($message); $x++) { @@ -286,7 +280,7 @@ class BulkAssetsController extends Controller } } - } // end if saved + } // end if saved } // end asset foreach From c4c47f2e8d3f8e7f61064bb8871875a783b5bb0b Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 06:51:14 +0100 Subject: [PATCH 27/35] Updated comment Signed-off-by: snipe --- app/Observers/AssetObserver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index c15c54a568..1e137d34c4 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -11,7 +11,7 @@ use Carbon\Carbon; class AssetObserver { /** - * Listen to the User created event. + * Listen to the User updating event. This fires automatically every time an asset is saved. * * @param Asset $asset * @return void From 9d887484c8c40faad442b0b163c0adb93b935a18 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 07:41:25 +0100 Subject: [PATCH 28/35] Added encrypted custom field to seeder Signed-off-by: snipe --- database/factories/CustomFieldFactory.php | 14 ++++++++++++-- database/seeders/CustomFieldSeeder.php | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/database/factories/CustomFieldFactory.php b/database/factories/CustomFieldFactory.php index 142adacdcf..7cbc2210c9 100644 --- a/database/factories/CustomFieldFactory.php +++ b/database/factories/CustomFieldFactory.php @@ -88,7 +88,7 @@ class CustomFieldFactory extends Factory return [ 'name' => 'Test Encrypted', 'field_encrypted' => '1', - 'help_text' => 'This is a test encrypted field.', + 'help_text' => 'This is a sample encrypted field.', ]; }); } @@ -98,10 +98,20 @@ class CustomFieldFactory extends Factory return $this->state(function () { return [ 'name' => 'Test Checkbox', - 'help_text' => 'This is a test checkbox.', + 'help_text' => 'This is a sample checkbox.', 'field_values' => "One\nTwo\nThree", 'element' => 'checkbox', ]; }); } + + public function testRequired() + { + return $this->state(function () { + return [ + 'name' => 'Test Required', + 'help_text' => 'This is a sample required field.', + ]; + }); + } } diff --git a/database/seeders/CustomFieldSeeder.php b/database/seeders/CustomFieldSeeder.php index e51ca510f9..5d8d44e4b8 100644 --- a/database/seeders/CustomFieldSeeder.php +++ b/database/seeders/CustomFieldSeeder.php @@ -35,6 +35,7 @@ class CustomFieldSeeder extends Seeder CustomField::factory()->count(1)->macAddress()->create(); CustomField::factory()->count(1)->testEncrypted()->create(); CustomField::factory()->count(1)->testCheckbox()->create(); + CustomField::factory()->count(1)->testRequired()->create(); DB::table('custom_field_custom_fieldset')->insert([ @@ -96,6 +97,13 @@ class CustomFieldSeeder extends Seeder 'required' => 0, ], + [ + 'custom_field_id' => '8', + 'custom_fieldset_id' => '1', + 'order' => 0, + 'required' => 1, + ], + ]); } } From ff72c4fbaa510f71846eaf35e4795a3375449aa5 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 09:17:28 +0100 Subject: [PATCH 29/35] Added nicer formatting for fields in log meta Signed-off-by: snipe --- .../Transformers/ActionlogsTransformer.php | 8 +++++ .../views/partials/bootstrap-table.blade.php | 29 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index d3af3bb753..61ae1af9a2 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Helpers\Helper; use App\Models\Actionlog; +use App\Models\Asset; use App\Models\CustomField; use App\Models\Setting; use App\Models\Company; @@ -12,6 +13,7 @@ use App\Models\AssetModel; use Illuminate\Database\Eloquent\Collection; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Support\Facades\Crypt; +use Illuminate\Support\Facades\Gate; class ActionlogsTransformer { @@ -98,6 +100,12 @@ class ActionlogsTransformer \Log::debug('custom fields do not match'); $clean_meta[$fieldname]['old'] = "************"; $clean_meta[$fieldname]['new'] = "************"; + + if (Gate::allows('admin')) { + $clean_meta[$fieldname]['old'] = unserialize($enc_old); + $clean_meta[$fieldname]['new'] = unserialize($enc_new); + } + } diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index 78f3d152d2..a7c2084adb 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -530,15 +530,42 @@ function changeLogFormatter(value) { + + console.dir(value); var result = ''; + var pretty_index = ''; + + console.error('first the formatter'); + for (var index in value) { - result += index + ': ' + value[index].old + ' ' + value[index].new + '
    ' + + + // Check if it's a custom field + if (index.startsWith('_snipeit_')) { + console.error('It is a custom field'); + pretty_index = index.replace("_snipeit_", "Custom:_"); + } else { + console.error('Not a custom field'); + pretty_index = index; + } + + extra_pretty_index = prettyLog(pretty_index); + + result += extra_pretty_index + ': ' + value[index].old + ' ' + value[index].new + '
    ' } return result; } + function prettyLog(str) { + let frags = str.split('_'); + for (let i = 0; i < frags.length; i++) { + frags[i] = frags[i].charAt(0).toUpperCase() + frags[i].slice(1); + } + return frags.join(' '); + } + // Create a linked phone number in the table list function phoneFormatter(value) { From c1213e0abc364a981d4f89c87d4ba6bc9ae80f9c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 09:41:44 +0100 Subject: [PATCH 30/35] Nicer handling for empty custom fields Signed-off-by: snipe --- app/Http/Transformers/ActionlogsTransformer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index 61ae1af9a2..db7105fc9a 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -101,9 +101,10 @@ class ActionlogsTransformer $clean_meta[$fieldname]['old'] = "************"; $clean_meta[$fieldname]['new'] = "************"; + // Display the changes if the user is an admin or superadmin if (Gate::allows('admin')) { - $clean_meta[$fieldname]['old'] = unserialize($enc_old); - $clean_meta[$fieldname]['new'] = unserialize($enc_new); + $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old): '(blank)'; + $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new): '(blank)'; } } From 9c90f031423461dfba22bba2aa33f06b74494fb8 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 14:34:18 +0100 Subject: [PATCH 31/35] Updated comment Signed-off-by: snipe --- app/Observers/AssetObserver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 1e137d34c4..5117f74d3c 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -11,7 +11,7 @@ use Carbon\Carbon; class AssetObserver { /** - * Listen to the User updating event. This fires automatically every time an asset is saved. + * Listen to the Asset updating event. This fires automatically every time an existing asset is saved. * * @param Asset $asset * @return void From 1ce95b6eac52a394b996def1a3177116d4688c44 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 14:34:27 +0100 Subject: [PATCH 32/35] Removed debugging Signed-off-by: snipe --- resources/views/partials/bootstrap-table.blade.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index a7c2084adb..0122096f80 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -531,21 +531,16 @@ function changeLogFormatter(value) { - console.dir(value); var result = ''; var pretty_index = ''; - console.error('first the formatter'); - for (var index in value) { // Check if it's a custom field if (index.startsWith('_snipeit_')) { - console.error('It is a custom field'); pretty_index = index.replace("_snipeit_", "Custom:_"); } else { - console.error('Not a custom field'); pretty_index = index; } From 9bba0b764ab25fe154be5097e4961a145c2bf267 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 14:38:39 +0100 Subject: [PATCH 33/35] Added clearer comments Signed-off-by: snipe --- .../Controllers/Assets/BulkAssetsController.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index d17dbcb3bb..45ca5bab7c 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -238,17 +238,26 @@ class BulkAssetsController extends Controller if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted=='1')) { $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); - // Check if the decrypted existing value is different from one we just submitted - // and if not, pull it out of the object + /* + * Check if the decrypted existing value is different from one we just submitted + * and if not, pull it out of the object since it shouldn't really be updating at all. + * If we don't do this, it will try to re-encrypt it, and the same value encrypted two + * different times will have different values, so it will *look* like it was updated + * but it wasn't. + */ if ($decrypted_old != $this->update_array[$field->db_column]) { $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); } else { - \Log::debug('The decrypted existing value is the same as the one submitted - unset it'); + /* + * Remove the encrypted custom field from the update_array, since nothing changed + */ unset($this->update_array[$field->db_column]); unset($asset->{$field->db_column}); } - // These fields aren't encrypted, just carry on as usual + /* + * These custom fields aren't encrypted, just carry on as usual + */ } else { From 305cd9b0b81d35d3eb10edfec263394f6880053d Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 14:42:49 +0100 Subject: [PATCH 34/35] Use null instead of blank Signed-off-by: snipe --- app/Http/Transformers/ActionlogsTransformer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index db7105fc9a..e5837666ac 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -103,8 +103,8 @@ class ActionlogsTransformer // Display the changes if the user is an admin or superadmin if (Gate::allows('admin')) { - $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old): '(blank)'; - $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new): '(blank)'; + $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old): 'null'; + $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new): 'null'; } } From 7b2f49644ee86947021ece76d6036fb2442cc386 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Oct 2023 14:44:17 +0100 Subject: [PATCH 35/35] Use nothing instead of null to make it consistent Signed-off-by: snipe --- app/Http/Transformers/ActionlogsTransformer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index e5837666ac..1de9143329 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -103,8 +103,8 @@ class ActionlogsTransformer // Display the changes if the user is an admin or superadmin if (Gate::allows('admin')) { - $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old): 'null'; - $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new): 'null'; + $clean_meta[$fieldname]['old'] = ($enc_old) ? unserialize($enc_old): ''; + $clean_meta[$fieldname]['new'] = ($enc_new) ? unserialize($enc_new): ''; } }