A free open source IT asset/license management system
Fixed #18409 - command line option to purge signatures
snipe79528fa87bf8
diff --git a/app/Console/Commands/PurgeEulaPDFs.php b/app/Console/Commands/PurgeEulaPDFs.phpnew file mode 100644index 0000000000..244eb29429--- /dev/null+++ b/app/Console/Commands/PurgeEulaPDFs.php@@ -0,0 +1,132 @@+<?php++namespace App\Console\Commands;++use App\Models\CheckoutAcceptance;+use Carbon\Carbon;+use Illuminate\Console\Command;+use Illuminate\Support\Facades\Storage;++class PurgeEulaPDFs extends Command+{+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'snipeit:purge-eula-pdfs+ {--older-than-days= : The number of days we should delete before }+ {--force : Skip the interactive yes/no prompt for confirmation}+ {--dryrun : Run the purge process but don\'t update the database or delete files}+ {--with-output : Display the results in a table in your console}';++ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'This purges signature files and EULAs from the system if they are older than the date passed with --older-than-days=.';++ /**+ * Execute the console command.+ */+ public function handle()+ {++ $before = $this->option('older-than-days');++ if (($before=='') || (!is_numeric($before))) {+ return $this->error('ERROR: You must pass a valid number for --older-than-days (example: snipeit:purge-eula-pdfs --older-than-days=365.)');+ }++ $interval_date = Carbon::now()->subDays($before);+ $signature_path = 'private_uploads/signatures/';+ $eula_path = 'private_uploads/eula-pdfs/';++ if (!Storage::exists($eula_path)) {+ $this->fail('The storage directory "'.$eula_path.'" does not exist. No EULA files will be deleted.');+ }++ if (!Storage::exists($signature_path)) {+ $this->fail('The storage directory "'.$signature_path.'" does not exist. No signature files will be deleted.');+ }+++ if ($this->option('dryrun')) {+ $this->info('This script is being run with the --dryrun option. No files or records will be deleted.');++ }+ $acceptances = CheckoutAcceptance::HasFiles()->where('updated_at','<', $interval_date)->with('assignedTo')->get();++ if (!$this->option('force')) {+ if ($this->confirm("\n****************************************************\nTHIS WILL DELETE ALL OF THE SIGNATURES AND EULA PDF FILES SINCE '.$interval_date.'. \nThere is NO undo! \n****************************************************\n\nDo you wish to continue? No backsies! [y|N]")) {+ }+ }++++ if ($acceptances->count() == 0) {+ return $this->warn('There are no item acceptances with signatures or EULA PDFs from before '.$interval_date);+ }++ $this->info(number_format($acceptances->count()) . ' EULA PDFs from before '.$interval_date.' will be purged');++ if (!$this->option('with-output')) {+ $this->info('Run this command with the --with-output option to see the full list in the console.');+ } else {+ $this->table(+ [+ trans('general.user'),+ trans('general.type'),+ trans('general.item'),+ trans('general.category'),+ trans('general.accepted_date'),+ trans('general.declined_date'),+ trans('general.signature'),+ trans('general.filename'),++ ],+ $acceptances->map(fn($acceptance) => [+ trans('general.user') => $acceptance->assignedTo->display_name,+ trans('general.type') => $acceptance->display_checkoutable_type,+ trans('general.item') => $acceptance->checkoutable_type::find($acceptance->checkoutable_id)->display_name,+ trans('general.category') => $acceptance->checkoutable_category_name,+ trans('general.accepted_date') => $acceptance->accepted_at,+ trans('general.declined_date') => $acceptance->declined_at,+ trans('general.signature') => $acceptance->signature_filename,+ trans('general.filename') => $acceptance->stored_eula_file,+ ])+ );+ }++++ foreach ($acceptances as $acceptance) {++ $signature_file = $signature_path.$acceptance->signature_filename;+ $eula_file = $eula_path.$acceptance->stored_eula_file;++ if (Storage::exists($signature_file)) {+ if (!$this->option('dryrun')) {+ Storage::delete($signature_file);+ }+ } else {+ $this->error('The file "'. $signature_file.'" does not exist.');+ }+++ if (Storage::exists($eula_file)) {+ if (!$this->option('dryrun')) {+ Storage::delete($eula_file);+ }+ } else {+ $this->error('The file "'.$eula_file.'" does not exist.');+ }++ if (!$this->option('dryrun')) {+ $acceptance->delete();+ }+ }++ }+}diff --git a/app/Models/CheckoutAcceptance.php b/app/Models/CheckoutAcceptance.phpindex b9895d92bc..adcc6b260e 100644--- a/app/Models/CheckoutAcceptance.php+++ b/app/Models/CheckoutAcceptance.php@@ -85,7 +85,7 @@ class CheckoutAcceptance extends Model/*** The user that the checkoutable was checked out to*- * @return Illuminate\Database\Eloquent\Relations\BelongsTo+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo*/public function assignedTo(){@@ -186,6 +186,10 @@ class CheckoutAcceptance extends Model);}+ protected function scopeHasFiles(Builder $query) {+ return $query->whereNotNull('signature_filename')->orWhereNotNull('stored_eula_file');+ }+public function generateAcceptancePdf($data, $pdf_filename) {// set some language dependent data:diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.phpindex 194a0864ca..68b6244d22 100644--- a/resources/lang/en-US/general.php+++ b/resources/lang/en-US/general.php@@ -332,6 +332,7 @@ return ['password' => 'Password','accepted' => 'accepted','declined' => 'declined',+ 'declined_date' => 'Date Declined','declined_note' => 'Declined Notes','unassigned' => 'Unassigned','unaccepted_asset_report' => 'Unaccepted Items',@@ -351,7 +352,7 @@ return ['file_not_inlineable' => 'The requested file cannot be opened inline in your browser. You can download it instead.','open_new_window' => 'Open this file in a new window','file_upload_success' => 'File upload success!',- 'no_files_uploaded' => 'File upload success!',+ 'no_files_uploaded' => 'No files were uploaded.','token_expired' => 'Your form session has expired. Please try again.','login_enabled' => 'Login Enabled','login_disabled' => 'Login Disabled',@@ -628,6 +629,7 @@ return ['something_went_wrong' => 'Something went wrong with your request.','close' => 'Close','expires' => 'Expires',+ 'filename' => 'File Name','map_fields'=> 'Map :item_type Fields','remaining_var' => ':count Remaining','label' => 'Label',