From 5df6575c3769d7d4c552b23f4528970d58f38a3c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 00:11:02 +0000 Subject: [PATCH 01/13] Fixed undefinted $search_base Signed-off-by: snipe --- app/Console/Commands/LdapSync.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php index 594f6f064f..e861b9af37 100755 --- a/app/Console/Commands/LdapSync.php +++ b/app/Console/Commands/LdapSync.php @@ -66,6 +66,7 @@ class LdapSync extends Command $ldap_result_dept = Setting::getSettings()->ldap_dept; $ldap_result_manager = Setting::getSettings()->ldap_manager; $ldap_default_group = Setting::getSettings()->ldap_default_group; + $search_base = Setting::getSettings()->ldap_base_dn; try { $ldapconn = Ldap::connectToLdap(); @@ -83,26 +84,35 @@ class LdapSync extends Command $summary = []; try { + + /** + * if a location ID has been specified, use that OU + */ if ( $this->option('location_id') != '') { foreach($this->option('location_id') as $location_id){ - $location_ou= Location::where('id', '=', $location_id)->value('ldap_ou'); + $location_ou = Location::where('id', '=', $location_id)->value('ldap_ou'); $search_base = $location_ou; Log::debug('Importing users from specified location OU: \"'.$search_base.'\".'); } - } - else if ($this->option('base_dn') != '') { + /** + * Otherwise if a manual base DN has been specified, use that + */ + } elseif ($this->option('base_dn') != '') { $search_base = $this->option('base_dn'); Log::debug('Importing users from specified base DN: \"'.$search_base.'\".'); - } else { - $search_base = null; } + + /** + * If a filter has been specified, use that + */ if ($this->option('filter') != '') { $results = Ldap::findLdapUsers($search_base, -1, $this->option('filter')); } else { $results = Ldap::findLdapUsers($search_base); } + } catch (\Exception $e) { if ($this->option('json_summary')) { $json_summary = ['error' => true, 'error_message' => $e->getMessage(), 'summary' => []]; From 4dac3712a66c074e5463f2bbe28d708f81a37987 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:32:26 +0000 Subject: [PATCH 02/13] Added DB migration for remote_ip, action_source, user_agent in logs Signed-off-by: snipe --- ...te_ip_and_action_source_to_action_logs.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 database/migrations/2023_12_14_032522_add_remote_ip_and_action_source_to_action_logs.php diff --git a/database/migrations/2023_12_14_032522_add_remote_ip_and_action_source_to_action_logs.php b/database/migrations/2023_12_14_032522_add_remote_ip_and_action_source_to_action_logs.php new file mode 100644 index 0000000000..70616b9f7f --- /dev/null +++ b/database/migrations/2023_12_14_032522_add_remote_ip_and_action_source_to_action_logs.php @@ -0,0 +1,42 @@ +string('action_source')->nullable()->default(null); + $table->ipAddress('remote_ip')->nullable()->default(null); + $table->string('user_agent')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('action_logs', function (Blueprint $table) { + if (Schema::hasColumn('action_logs', 'action_source')) { + $table->dropColumn('action_source'); + } + if (Schema::hasColumn('action_logs', 'remote_ip')) { + $table->dropColumn('remote_ip'); + } + if (Schema::hasColumn('action_logs', 'user_agent')) { + $table->dropColumn('user_agent'); + } + }); + } +} From 70303c2b2de953acd4e1bbf6712daa7f40da37a5 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:33:25 +0000 Subject: [PATCH 03/13] Added searchable fields, improved formatting Signed-off-by: snipe --- app/Models/Actionlog.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 7e24b839ec..9f87ffd8c2 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -25,7 +25,17 @@ class Actionlog extends SnipeModel protected $table = 'action_logs'; public $timestamps = true; - protected $fillable = ['created_at', 'item_type', 'user_id', 'item_id', 'action_type', 'note', 'target_id', 'target_type', 'stored_eula']; + protected $fillable = [ + 'created_at', + 'item_type', + 'user_id', + 'item_id', + 'action_type', + 'note', + 'target_id', + 'target_type', + 'stored_eula' + ]; use Searchable; @@ -34,7 +44,15 @@ class Actionlog extends SnipeModel * * @var array */ - protected $searchableAttributes = ['action_type', 'note', 'log_meta','user_id']; + protected $searchableAttributes = [ + 'action_type', + 'note', + 'log_meta', + 'user_id', + 'remote_ip', + 'user_agent', + 'action_source' + ]; /** * The relations and their attributes that should be included when searching the model. From 40052e99a798b9eb5e0dcfda0f8ade40a7177461 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:33:39 +0000 Subject: [PATCH 04/13] Added user agent to log method Signed-off-by: snipe --- app/Models/Actionlog.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 9f87ffd8c2..de00eefeb7 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -266,6 +266,9 @@ class Actionlog extends SnipeModel public function logaction($actiontype) { $this->action_type = $actiontype; + $this->remote_ip = request()->ip(); + $this->user_agent = request()->header('User-Agent'); + $this->action_source = $this->determineActionSource(); if ($this->save()) { return true; @@ -330,4 +333,22 @@ class Actionlog extends SnipeModel ->orderBy('created_at', 'asc') ->get(); } + + public function determineActionSource() { + + // This is an API call + if (((request()->header('content-type') && (request()->header('accept'))=='application/json')) + && (starts_with(request()->header('authorization'), 'Bearer '))) { + return 'api'; + } + + // This is probably NOT an API call + if (request()->filled('_token')) { + return 'gui'; + } + + // We're not sure, probably cli + return 'cli/unknown'; + + } } From 93c7108eda867a143f045fb3dade2aaa3a2d7525 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:33:49 +0000 Subject: [PATCH 05/13] Added action source to translations Signed-off-by: snipe --- resources/lang/en/general.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 023e9c052c..5e1ad742e3 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -498,5 +498,6 @@ return [ 'action_permission_denied' => 'You do not have permission to :action :item_type ID :id', 'action_permission_generic' => 'You do not have permission to :action this :item_type', 'edit' => 'edit', + 'action_source' => 'Action Source', ]; From a90b04c9057ab14f060f69bea832d350f2a7d7bc Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:34:08 +0000 Subject: [PATCH 06/13] Allowed sorting on user_agent, etc Signed-off-by: snipe --- app/Http/Controllers/Api/ReportsController.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.php index 7335e7d8e0..5c6eaebf50 100644 --- a/app/Http/Controllers/Api/ReportsController.php +++ b/app/Http/Controllers/Api/ReportsController.php @@ -40,6 +40,14 @@ class ReportsController extends Controller $actionlogs = $actionlogs->where('action_type', '=', $request->input('action_type'))->orderBy('created_at', 'desc'); } + if ($request->filled('action_source')) { + $actionlogs = $actionlogs->where('action_source', '=', $request->input('action_source'))->orderBy('created_at', 'desc'); + } + + if ($request->filled('remote_ip')) { + $actionlogs = $actionlogs->where('remote_ip', '=', $request->input('remote_ip'))->orderBy('created_at', 'desc'); + } + if ($request->filled('uploads')) { $actionlogs = $actionlogs->whereNotNull('filename')->orderBy('created_at', 'desc'); } @@ -52,6 +60,9 @@ class ReportsController extends Controller 'accept_signature', 'action_type', 'note', + 'remote_ip', + 'user_agent', + 'action_source', ]; From f5f136b9b569ff6f7b17647692955033b6b89964 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:34:31 +0000 Subject: [PATCH 07/13] Added fields to history views Signed-off-by: snipe --- app/Http/Transformers/ActionlogsTransformer.php | 3 +++ resources/views/users/view.blade.php | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index 1de9143329..627045acdb 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -181,6 +181,9 @@ class ActionlogsTransformer 'note' => ($actionlog->note) ? Helper::parseEscapedMarkedownInline($actionlog->note): null, 'signature_file' => ($actionlog->accept_signature) ? route('log.signature.view', ['filename' => $actionlog->accept_signature ]) : null, 'log_meta' => ((isset($clean_meta)) && (is_array($clean_meta))) ? $clean_meta: null, + 'remote_ip' => ($actionlog->remote_ip) ?? null, + 'user_agent' => ($actionlog->user_agent) ?? null, + 'action_source' => ($actionlog->action_source) ?? null, 'action_date' => ($actionlog->action_date) ? Helper::getFormattedDateObject($actionlog->action_date, 'datetime'): Helper::getFormattedDateObject($actionlog->created_at, 'datetime'), ]; diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 5de2d754fb..1b9bc303eb 100755 --- a/resources/views/users/view.blade.php +++ b/resources/views/users/view.blade.php @@ -1003,7 +1003,9 @@ @endif {{ trans('admin/hardware/table.serial') }} {{ trans('general.admin') }} - + {{ trans('admin/settings/general.login_ip') }} + {{ trans('admin/settings/general.login_user_agent') }} + {{ trans('general.action_source') }} From 8fc470c9783534b5d9e7eaf562d6297b73065207 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:34:44 +0000 Subject: [PATCH 08/13] Added fields to activity report Signed-off-by: snipe --- resources/views/reports/activity.blade.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/views/reports/activity.blade.php b/resources/views/reports/activity.blade.php index 0922bf6b06..c0c259a950 100644 --- a/resources/views/reports/activity.blade.php +++ b/resources/views/reports/activity.blade.php @@ -54,6 +54,9 @@ {{ trans('general.to') }} {{ trans('general.notes') }} {{ trans('general.changed') }} + {{ trans('admin/settings/general.login_ip') }} + {{ trans('admin/settings/general.login_user_agent') }} + {{ trans('general.action_source') }} From 14c70ee6c4f1df29146bc4c12a0c7aef5025ef33 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:37:47 +0000 Subject: [PATCH 09/13] Removed BS th classes Signed-off-by: snipe --- resources/views/hardware/view.blade.php | 5 ++++- resources/views/reports/activity.blade.php | 4 ++-- resources/views/users/view.blade.php | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index badb4b1e6b..d039f7d164 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -1214,7 +1214,10 @@ {{ trans('general.notes') }} {{ trans('general.signature') }} {{ trans('general.download') }} - {{ trans('admin/hardware/table.changed')}} + {{ trans('admin/hardware/table.changed')}} + {{ trans('admin/settings/general.login_ip') }} + {{ trans('admin/settings/general.login_user_agent') }} + {{ trans('general.action_source') }} diff --git a/resources/views/reports/activity.blade.php b/resources/views/reports/activity.blade.php index c0c259a950..a7e6f9f15b 100644 --- a/resources/views/reports/activity.blade.php +++ b/resources/views/reports/activity.blade.php @@ -54,8 +54,8 @@ {{ trans('general.to') }} {{ trans('general.notes') }} {{ trans('general.changed') }} - {{ trans('admin/settings/general.login_ip') }} - {{ trans('admin/settings/general.login_user_agent') }} + {{ trans('admin/settings/general.login_ip') }} + {{ trans('admin/settings/general.login_user_agent') }} {{ trans('general.action_source') }} diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 1b9bc303eb..c28a9079cf 100755 --- a/resources/views/users/view.blade.php +++ b/resources/views/users/view.blade.php @@ -1003,8 +1003,8 @@ @endif {{ trans('admin/hardware/table.serial') }} {{ trans('general.admin') }} - {{ trans('admin/settings/general.login_ip') }} - {{ trans('admin/settings/general.login_user_agent') }} + {{ trans('admin/settings/general.login_ip') }} + {{ trans('admin/settings/general.login_user_agent') }} {{ trans('general.action_source') }} From e71080ef3712b84994980bcb8405ec1d7d93ea9c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:41:15 +0000 Subject: [PATCH 10/13] Make action date sortable on asset history view Signed-off-by: snipe --- resources/views/hardware/view.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index d039f7d164..1ded42f456 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -1206,7 +1206,7 @@ {{ trans('admin/hardware/table.icon') }} - {{ trans('general.date') }} + {{ trans('general.date') }} {{ trans('general.admin') }} {{ trans('general.action') }} {{ trans('general.item') }} From 7aabf94a352e1b439fb712207aa34cfadb52f84e Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 14 Dec 2023 14:43:38 +0000 Subject: [PATCH 11/13] Added comments Signed-off-by: snipe --- app/Models/Actionlog.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index de00eefeb7..bc08aa800a 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -334,6 +334,13 @@ class Actionlog extends SnipeModel ->get(); } + /** + * Determines what the type of request is so we can log it to the action_log + * + * @author A. Gianotto + * @since v6.3.0 + * @return string + */ public function determineActionSource() { // This is an API call From 421802893e224afcaf912f0d1177fa5dcb0e1f71 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 15 Dec 2023 10:45:22 +0000 Subject: [PATCH 12/13] Added new fields to action report Signed-off-by: snipe --- app/Http/Controllers/ReportsController.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 04e3c434eb..5c55376c85 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -252,6 +252,9 @@ class ReportsController extends Controller trans('general.model_no'), 'To', trans('general.notes'), + trans('admin/settings/general.login_ip'), + trans('admin/settings/general.login_user_agent'), + trans('general.action_source'), 'Changed', ]; @@ -298,6 +301,9 @@ class ReportsController extends Controller $target_name, ($actionlog->note) ? e($actionlog->note) : '', $actionlog->log_meta, + $actionlog->remote_ip, + $actionlog->user_agent, + $actionlog->action_source, ]; fputcsv($handle, $row); } From 2d85eefacef817d7bc582c457a057c6cdf1fcdb4 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 15 Dec 2023 13:24:01 +0000 Subject: [PATCH 13/13] Added indexes for reports Signed-off-by: snipe --- ..._indexes_to_new_activity_report_fields.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 database/migrations/2023_12_15_024643_add_indexes_to_new_activity_report_fields.php diff --git a/database/migrations/2023_12_15_024643_add_indexes_to_new_activity_report_fields.php b/database/migrations/2023_12_15_024643_add_indexes_to_new_activity_report_fields.php new file mode 100644 index 0000000000..1410430592 --- /dev/null +++ b/database/migrations/2023_12_15_024643_add_indexes_to_new_activity_report_fields.php @@ -0,0 +1,34 @@ +index('action_type'); + $table->index('remote_ip'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('action_logs', function (Blueprint $table) { + $table->dropIndex(['action_type']); + $table->dropIndex(['remote_ip']); + }); + } +}