--- ativo/app/Console/Commands/ImportDocumentBatch.php +++ QA/app/Console/Commands/ImportDocumentBatch.php @@ -14,7 +14,7 @@ class ImportDocumentBatch extends Command { - protected $signature = 'documents:import-batch {manifest : JSON manifest inside storage/app/private/document-import-staging} {--apply : Register private drafts and queue malware scans} {--actor= : Existing user ID responsible for an applied import} {--pilot-agronegocio : Restrict to the approved two-original pilot} {--sectors-lot20 : Restrict to the named 20-resource sector batch} {--backup-proof= : Fresh verified destination backup receipt for active pilot apply}'; + protected $signature = 'documents:import-batch {manifest : JSON manifest inside storage/app/private/document-import-staging} {--apply : Register private drafts and queue malware scans} {--actor= : Existing user ID responsible for an applied import} {--pilot-agronegocio : Restrict to the approved two-original pilot} {--sectors-lot20 : Restrict to the named 20-resource sector batch} {--sectors-trio : Restrict to the three named sector articles and reuse the existing image} {--backup-proof= : Fresh verified destination backup receipt for active pilot apply}'; protected $description = 'Preview or idempotently import a document batch into the existing private library'; @@ -52,6 +52,14 @@ 'legacy-tempo-image-8e58544e49e719f9' => '8e58544e49e719f9c865c91e07ef1030155a9e3d59260e6dc93e9c9e25674e39', ]; + private const TRIO_MANIFEST_SHA256 = '9210ad7e1bea557349c440fd8295b321a10c2ddd34cc170f3dad18b5517751f8'; + private const TRIO_REUSED_IMAGE_SHA256 = '28f41f12e10ab34545d8e432f6e8c74f86960e17e0c4a9956f514bcde4881eaa'; + private const TRIO_HASHES = [ + 'legacy-tempo-post-173209-20260623' => '926f5e4096737c936c9d5002ebdc64be9e63b0c0822023d17ce618111a65c941', + 'legacy-tempo-post-173218-20260616' => '43612c358c7573475cea24ef4825c5b7c7925306fa5861051220eed3f4e333b5', + 'legacy-tempo-post-173213-20260616' => '0ba605ae43dec7f685610f828df884bc82cb4da67131fc9783b5cdbe4cc6d9f0', + ]; + public function handle(): int { $staging = storage_path('app/private/document-import-staging'); @@ -92,21 +100,22 @@ $actorId = $this->option('actor'); $pilot = (bool) $this->option('pilot-agronegocio'); $sectors = (bool) $this->option('sectors-lot20'); - if ($pilot && $sectors) { + $trio = (bool) $this->option('sectors-trio'); + if ((int) $pilot + (int) $sectors + (int) $trio > 1) { $this->error('Select exactly one named batch; agribusiness scope is unchanged.'); return self::FAILURE; } - if ($pilot || $sectors) { + if ($pilot || $sectors || $trio) { try { - $this->validatePilot($manifestPath, $manifestBytes, $manifest, $stagingReal, $apply, $actorId, $sectors); + $this->validatePilot($manifestPath, $manifestBytes, $manifest, $stagingReal, $apply, $actorId, $sectors, $trio); } catch (Throwable $exception) { $this->error('Pilot refused before import: '.$exception->getMessage()); return self::FAILURE; } } - if ($apply && ! $pilot && ! $sectors) { + if ($apply && ! $pilot && ! $sectors && ! $trio) { // CLI attribution is not an authenticated active installation. Rehearsals stay isolated. $target = DB::selectOne('SELECT current_database() AS database,inet_server_port() AS port'); if (! app()->environment('testing') || ! str_ends_with($target->database, '_test') || (int) $target->port !== 55441 @@ -136,7 +145,7 @@ $label = is_array($record) ? (string) ($record['external_id'] ?? '#'.($index + 1)) : '#'.($index + 1); try { $validated = $this->validateRecord($record, $stagingReal); - if (($pilot || $sectors) && ! hash_equals(($sectors ? self::SECTORS_HASHES : self::PILOT_HASHES)[$validated['external_id']] ?? '', $validated['sha256'])) { + if (($pilot || $sectors || $trio) && ! hash_equals(($trio ? self::TRIO_HASHES : ($sectors ? self::SECTORS_HASHES : self::PILOT_HASHES))[$validated['external_id']] ?? '', $validated['sha256'])) { throw new RuntimeException('Approved pilot bytes changed after preview validation.'); } $hash = $validated['sha256']; @@ -268,11 +277,11 @@ } /** The extra CLI path is a named, byte-bound pilot; it is not a general active import gate. */ - private function validatePilot(string $manifestPath, string $manifestBytes, array $manifest, string $staging, bool $apply, mixed $actorId, bool $sectors = false): void + private function validatePilot(string $manifestPath, string $manifestBytes, array $manifest, string $staging, bool $apply, mixed $actorId, bool $sectors = false, bool $trio = false): void { - $allowed = $sectors ? self::SECTORS_HASHES : self::PILOT_HASHES; - $manifestName = $sectors ? 'legacy-setores-lote20-20261015/manifest.json' : 'legacy-agronegocio/manifest.json'; - $manifestHash = $sectors ? self::SECTORS_MANIFEST_SHA256 : self::PILOT_MANIFEST_SHA256; + $allowed = $trio ? self::TRIO_HASHES : ($sectors ? self::SECTORS_HASHES : self::PILOT_HASHES); + $manifestName = $trio ? 'legacy-trio-20261015/manifest.json' : ($sectors ? 'legacy-setores-lote20-20261015/manifest.json' : 'legacy-agronegocio/manifest.json'); + $manifestHash = $trio ? self::TRIO_MANIFEST_SHA256 : ($sectors ? self::SECTORS_MANIFEST_SHA256 : self::PILOT_MANIFEST_SHA256); if ($manifestPath !== realpath($staging.'/'.$manifestName) || ! hash_equals($manifestHash, hash('sha256', $manifestBytes)) || count($manifest['documents']) !== count($allowed)) { @@ -293,6 +302,17 @@ } if ($active && $apply) { $this->validatePilotBackup($this->option('backup-proof')); + } + if ($trio) { + $images = DB::table('documents as d')->join('portal_resources as r', 'r.document_id', '=', 'd.id') + ->where('d.external_id', 'legacy-tempo-image-28f41f12e10ab345')->where('d.sha256', self::TRIO_REUSED_IMAGE_SHA256) + ->whereNull('d.deleted_at')->where('d.status', 'active')->where('d.data_classification', 'public') + ->where('d.rights_status', 'authorized')->where('r.rights_status', 'authorized')->where('r.published', true) + ->where('d.malware_scan_status', 'clean')->whereColumn('d.sha256', 'd.malware_scanned_sha256')->get(['d.storage_key']); + if ($images->count() !== 1 || ! Storage::disk('local')->exists($images[0]->storage_key) + || ! hash_equals(self::TRIO_REUSED_IMAGE_SHA256, hash_file('sha256', Storage::disk('local')->path($images[0]->storage_key)))) { + throw new RuntimeException('The shared image must already exist once, published, authorized and clean with the exact original bytes; no image is imported.'); + } } foreach ($manifest['documents'] as $record) { $entry = $this->validateRecord($record, $staging);