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', ]; 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); } 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/app/Models/Actionlog.php b/app/Models/Actionlog.php index 7e24b839ec..bc08aa800a 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. @@ -248,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; @@ -312,4 +333,29 @@ class Actionlog extends SnipeModel ->orderBy('created_at', 'asc') ->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 + 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'; + + } } 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'); + } + }); + } +} 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', ]; diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index badb4b1e6b..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') }} @@ -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 0922bf6b06..a7e6f9f15b 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') }} diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 5de2d754fb..c28a9079cf 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') }}