commit melhorias compras1
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-21 21:04:40 +02:00
parent 976271f948
commit 12dd6df28a
108 changed files with 4391 additions and 408 deletions
@@ -4,6 +4,9 @@ namespace App\Http\Controllers;
use App\Models\Supplier;
use App\Models\SupplierDocument;
use App\Models\User;
use App\Services\ConflictGuard;
use App\Services\CurrentActor;
use App\Services\RequisitionWorkflow;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
@@ -20,6 +23,8 @@ class SupplierDocumentController extends Controller
{
public function store(Request $request, Supplier $supplier, RequisitionWorkflow $workflow): RedirectResponse
{
$actor = $request->user('web');
abort_unless($actor instanceof User, 403);
Gate::authorize('update', $supplier);
$data = $request->validate([
'document_type' => ['required', Rule::in(['license', 'tax', 'conflict', 'other'])],
@@ -30,18 +35,21 @@ class SupplierDocumentController extends Controller
$path = $file->store($supplier->tenant_id.'/'.$supplier->id, 'supplier_documents');
abort_if($path === false, 500);
try {
DB::transaction(function () use ($request, $supplier, $workflow, $data, $file, $path) {
DB::transaction(function () use ($actor, $supplier, $workflow, $data, $file, $path) {
app(ConflictGuard::class)->lockCompany($actor);
$actor = app(CurrentActor::class)->lock($actor);
$supplier = Supplier::query()->lockForUpdate()->findOrFail($supplier->id);
Gate::forUser($actor)->authorize('update', $supplier);
$before = $supplier->toArray();
$name = Str::substr(Str::replaceMatches('/[^\pL\pN ._()-]/u', '_', basename(str_replace('\\', '/', $file->getClientOriginalName()))), 0, 180);
$document = $supplier->documents()->create([
'tenant_id' => $supplier->tenant_id, 'uploaded_by' => $request->user()->id,
'tenant_id' => $supplier->tenant_id, 'uploaded_by' => $actor->id,
'document_type' => $data['document_type'], 'original_name' => $name,
'path' => $path, 'mime_type' => $file->getMimeType(), 'size' => $file->getSize(),
'sha256' => hash_file('sha256', $file->getPathname()),
]);
$supplier->update(['status' => 'pending', 'reviewed_by' => null, 'review_comment' => null, 'last_changed_by' => $request->user()->id, 'version' => $supplier->version + 1]);
$workflow->audit($request->user(), 'supplier.document_uploaded', null, $before, ['supplier' => $supplier->toArray(), 'document' => $document->toArray()]);
$supplier->update(['status' => 'pending', 'reviewed_by' => null, 'review_comment' => null, 'last_changed_by' => $actor->id, 'version' => $supplier->version + 1]);
$workflow->audit($actor, 'supplier.document_uploaded', null, $before, ['supplier' => $supplier->toArray(), 'document' => $document->toArray()]);
});
} catch (Throwable $exception) {
Storage::disk('supplier_documents')->delete($path);
@@ -53,13 +61,35 @@ class SupplierDocumentController extends Controller
public function download(Request $request, Supplier $supplier, SupplierDocument $document, RequisitionWorkflow $workflow): StreamedResponse
{
Gate::authorize('viewDocuments', $supplier);
$actor = $request->user('web');
abort_unless($actor instanceof User, 403);
$actor = User::query()->where('tenant_id', $actor->tenant_id)->findOrFail($actor->id);
Gate::forUser($actor)->authorize('viewDocuments', $supplier);
abort_unless($document->supplier_id === $supplier->id, 404);
$prefix = $supplier->tenant_id.'/'.$supplier->id.'/';
abort_unless(str_starts_with($document->path, $prefix)
&& preg_match('/\A[A-Za-z0-9_-]+\.[A-Za-z0-9]+\z/D', substr($document->path, strlen($prefix))), 409,
'Documento indisponível para descarga. Solicite conferência ao responsável.');
$disk = Storage::disk('supplier_documents');
abort_unless($disk->exists($document->path), 404);
$workflow->audit($request->user(), 'supplier.document_downloaded', null, null, ['supplier_id' => $supplier->id, 'document_id' => $document->id, 'name' => $document->original_name]);
$stream = $disk->readStream($document->path);
abort_unless(is_resource($stream), 404);
try {
// Capture at most the accepted upload size plus one byte. The response uses
// these verified bytes even if the underlying file subsequently changes.
$content = stream_get_contents($stream, 2097153);
} finally {
fclose($stream);
}
abort_unless(is_string($content) && strlen($content) > 0 && strlen($content) <= 2097152
&& strlen($content) === (int) $document->size
&& hash_equals($document->sha256, hash('sha256', $content)), 409,
'Documento indisponível para descarga. Solicite conferência ao responsável.');
$workflow->audit($actor, 'supplier.document_downloaded', null, null, ['supplier_id' => $supplier->id, 'document_id' => $document->id, 'name' => $document->original_name]);
return $disk->download($document->path, $document->original_name, [
return response()->streamDownload(static function () use ($content): void {
echo $content;
}, $document->original_name, [
'Content-Type' => 'application/octet-stream',
'X-Content-Type-Options' => 'nosniff',
'Cache-Control' => 'private, no-store',