Merge pull request #13507 from Godmartinz/ignore_encrypt_and_other_changes

Added eager loading for `changedinfo` and removed encrypted values from Asset history
This commit is contained in:
snipe 2023-08-31 13:04:39 +01:00 committed by GitHub
commit c51574099f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 15 deletions

View file

@ -3,6 +3,7 @@ namespace App\Http\Transformers;
use App\Helpers\Helper; use App\Helpers\Helper;
use App\Models\Actionlog; use App\Models\Actionlog;
use App\Models\CustomField;
use App\Models\Setting; use App\Models\Setting;
use App\Models\Company; use App\Models\Company;
use App\Models\Supplier; use App\Models\Supplier;
@ -42,6 +43,7 @@ class ActionlogsTransformer
public function transformActionlog (Actionlog $actionlog, $settings = null) public function transformActionlog (Actionlog $actionlog, $settings = null)
{ {
$icon = $actionlog->present()->icon(); $icon = $actionlog->present()->icon();
$custom_field = CustomField::all();
if ($actionlog->filename!='') { if ($actionlog->filename!='') {
$icon = e(\App\Helpers\Helper::filetype_icon($actionlog->filename)); $icon = e(\App\Helpers\Helper::filetype_icon($actionlog->filename));
} }
@ -54,12 +56,20 @@ class ActionlogsTransformer
if ($meta_array) { if ($meta_array) {
foreach ($meta_array as $fieldname => $fieldata) { foreach ($meta_array as $fieldname => $fieldata) {
$clean_meta[$fieldname]['old'] = $this->clean_field($fieldata->old); if( str_starts_with($fieldname, '_snipeit_')){
$clean_meta[$fieldname]['new'] = $this->clean_field($fieldata->new); if( $custom_field->where('db_column', '=', $fieldname)->where('field_encrypted', true)){
$clean_meta[$fieldname]['old'] = "encrypted";
$clean_meta[$fieldname]['new'] = "encrypted";
}
}
else {
$clean_meta[$fieldname]['old'] = $this->clean_field($fieldata->old);
$clean_meta[$fieldname]['new'] = $this->clean_field($fieldata->new);
}
} }
}
$clean_meta= $this->changedInfo($clean_meta); $clean_meta = $this->changedInfo($clean_meta);
}
} }
$file_url = ''; $file_url = '';
@ -123,6 +133,9 @@ class ActionlogsTransformer
'action_date' => ($actionlog->action_date) ? Helper::getFormattedDateObject($actionlog->action_date, 'datetime'): Helper::getFormattedDateObject($actionlog->created_at, 'datetime'), 'action_date' => ($actionlog->action_date) ? Helper::getFormattedDateObject($actionlog->action_date, 'datetime'): Helper::getFormattedDateObject($actionlog->created_at, 'datetime'),
]; ];
// \Log::info("Clean Meta is: ".print_r($clean_meta,true));
//dd($array);
return $array; return $array;
} }
@ -143,39 +156,55 @@ class ActionlogsTransformer
* @param array $clean_meta * @param array $clean_meta
* @return array * @return array
*/ */
public function changedInfo(array $clean_meta) public function changedInfo(array $clean_meta)
{ { $location = Location::withTrashed()->get();
$supplier = Supplier::withTrashed()->get();
$model = AssetModel::withTrashed()->get();
if(array_key_exists('rtd_location_id',$clean_meta)) { if(array_key_exists('rtd_location_id',$clean_meta)) {
$clean_meta['rtd_location_id']['old'] = $clean_meta['rtd_location_id']['old'] ? "[id: ".$clean_meta['rtd_location_id']['old']."] ". Location::find($clean_meta['rtd_location_id']['old'])->name : trans('general.unassigned'); $clean_meta['rtd_location_id']['old'] = $clean_meta['rtd_location_id']['old'] ? "[id: ".$clean_meta['rtd_location_id']['old']."] ". $location->find($clean_meta['rtd_location_id']['old'])->name : trans('general.unassigned');
$clean_meta['rtd_location_id']['new'] = $clean_meta['rtd_location_id']['new'] ? "[id: ".$clean_meta['rtd_location_id']['new']."] ". Location::find($clean_meta['rtd_location_id']['new'])->name : trans('general.unassigned'); $clean_meta['rtd_location_id']['new'] = $clean_meta['rtd_location_id']['new'] ? "[id: ".$clean_meta['rtd_location_id']['new']."] ". $location->find($clean_meta['rtd_location_id']['new'])->name : trans('general.unassigned');
$clean_meta['Default Location'] = $clean_meta['rtd_location_id']; $clean_meta['Default Location'] = $clean_meta['rtd_location_id'];
unset($clean_meta['rtd_location_id']); unset($clean_meta['rtd_location_id']);
} }
if(array_key_exists('location_id', $clean_meta)) { if(array_key_exists('location_id', $clean_meta)) {
$clean_meta['location_id']['old'] = $clean_meta['location_id']['old'] ? "[id: ".$clean_meta['location_id']['old']."] ".Location::find($clean_meta['location_id']['old'])->name : trans('general.unassigned'); $clean_meta['location_id']['old'] = $clean_meta['location_id']['old'] ? "[id: ".$clean_meta['location_id']['old']."] ".$location->find($clean_meta['location_id']['old'])->name : trans('general.unassigned');
$clean_meta['location_id']['new'] = $clean_meta['location_id']['new'] ? "[id: ".$clean_meta['location_id']['new']."] ".Location::find($clean_meta['location_id']['new'])->name : trans('general.unassigned'); $clean_meta['location_id']['new'] = $clean_meta['location_id']['new'] ? "[id: ".$clean_meta['location_id']['new']."] ".$location->find($clean_meta['location_id']['new'])->name : trans('general.unassigned');
$clean_meta['Current Location'] = $clean_meta['location_id']; $clean_meta['Current Location'] = $clean_meta['location_id'];
unset($clean_meta['location_id']); unset($clean_meta['location_id']);
} }
if(array_key_exists('model_id', $clean_meta)) { if(array_key_exists('model_id', $clean_meta)) {
$clean_meta['model_id']['old'] = "[id: ".$clean_meta['model_id']['old']."] ".AssetModel::withTrashed()->find($clean_meta['model_id']['old'])->name;
$clean_meta['model_id']['new'] = "[id: ".$clean_meta['model_id']['new']."] ".AssetModel::withTrashed()->find($clean_meta['model_id']['new'])->name; /* model is required at asset creation */ $clean_meta['model_id']['old'] = "[id: ".$clean_meta['model_id']['old']."] ".$model->find($clean_meta['model_id']['old'])->name;
$clean_meta['model_id']['new'] = "[id: ".$clean_meta['model_id']['new']."] ".$model->find($clean_meta['model_id']['new'])->name; /* model is required at asset creation */
$clean_meta['Model'] = $clean_meta['model_id']; $clean_meta['Model'] = $clean_meta['model_id'];
unset($clean_meta['model_id']); unset($clean_meta['model_id']);
} }
if(array_key_exists('company_id', $clean_meta)) { if(array_key_exists('company_id', $clean_meta)) {
$clean_meta['company_id']['old'] = $clean_meta['company_id']['old'] ? "[id: ".$clean_meta['company_id']['old']."]".Company::find($clean_meta['company_id']['old'])->name : trans('general.unassigned'); $oldCompany = Company::find($clean_meta['company_id']['old']);
$clean_meta['company_id']['new'] = $clean_meta['company_id']['new'] ? "[id: ".$clean_meta['company_id']['new']."] ".Company::find($clean_meta['company_id']['new'])->name : trans('general.unassigned'); $oldCompanyName = $oldCompany->name ?? trans('admin/companies/message.deleted');
$newCompany = Company::find($clean_meta['company_id']['new']);
$newCompanyName = $newCompany->name ?? trans('admin/companies/message.deleted');
$clean_meta['company_id']['old'] = $clean_meta['company_id']['old'] ? "[id: ".$clean_meta['company_id']['old']."] ". $oldCompanyName : trans('general.unassigned');
$clean_meta['company_id']['new'] = $clean_meta['company_id']['new'] ? "[id: ".$clean_meta['company_id']['new']."] ". $newCompanyName : trans('general.unassigned');
$clean_meta['Company'] = $clean_meta['company_id']; $clean_meta['Company'] = $clean_meta['company_id'];
unset($clean_meta['company_id']); unset($clean_meta['company_id']);
} }
if(array_key_exists('supplier_id', $clean_meta)) { if(array_key_exists('supplier_id', $clean_meta)) {
$clean_meta['supplier_id']['old'] = $clean_meta['supplier_id']['old'] ? "[id: ".$clean_meta['supplier_id']['old']."] ".Supplier::find($clean_meta['supplier_id']['old'])->name : trans('general.unassigned'); $clean_meta['supplier_id']['old'] = $clean_meta['supplier_id']['old'] ? "[id: ".$clean_meta['supplier_id']['old']."] ".$supplier->find($clean_meta['supplier_id']['old'])->name : trans('general.unassigned');
$clean_meta['supplier_id']['new'] = $clean_meta['supplier_id']['new'] ? "[id: ".$clean_meta['supplier_id']['new']."] ".Supplier::find($clean_meta['supplier_id']['new'])->name : trans('general.unassigned'); $clean_meta['supplier_id']['new'] = $clean_meta['supplier_id']['new'] ? "[id: ".$clean_meta['supplier_id']['new']."] ".$supplier->find($clean_meta['supplier_id']['new'])->name : trans('general.unassigned');
$clean_meta['Supplier'] = $clean_meta['supplier_id']; $clean_meta['Supplier'] = $clean_meta['supplier_id'];
unset($clean_meta['supplier_id']); unset($clean_meta['supplier_id']);
} }
if(array_key_exists('asset_eol_date', $clean_meta)) {
$clean_meta['EOL date'] = $clean_meta['asset_eol_date'];
unset($clean_meta['asset_eol_date']);
}
return $clean_meta; return $clean_meta;

View file

@ -2,6 +2,7 @@
return [ return [
'does_not_exist' => 'Company does not exist.', 'does_not_exist' => 'Company does not exist.',
'deleted' => 'Deleted company',
'assoc_users' => 'This company is currently associated with at least one model and cannot be deleted. Please update your models to no longer reference this company and try again. ', 'assoc_users' => 'This company is currently associated with at least one model and cannot be deleted. Please update your models to no longer reference this company and try again. ',
'create' => [ 'create' => [
'error' => 'Company was not created, please try again.', 'error' => 'Company was not created, please try again.',