feat: protect owner-only supply aggregates and record target CAE blocker

This commit is contained in:
2026-11-15 23:10:17 +02:00
parent e0b18413ac
commit c8957ecb9d
15 changed files with 1255 additions and 49 deletions
+16 -7
View File
@@ -195,28 +195,37 @@ final class InvestorContext
*/
public function saveSupplyAggregate(User $actor, string $id, array $input): array
{
abort_unless(config('investor-path.aggregate_supply_qa_enabled') && \App\Support\IsolatedQaDatabase::allowsSchemaChanges(), 404);
$control = Validator::make($input, ['version' => 'required|integer|min:1', 'operation_key' => 'required|uuid'])->validate();
return DB::transaction(function () use ($actor, $id, $control): array {
abort_unless(app(ProactiveSupply::class)->aggregateAvailable(), 404);
$protected = (bool) config('investor-path.protected_supply_enabled');
abort_if($protected && array_diff(array_keys($input), ['version', 'operation_key', 'activity_id']), 422, 'O território e o sector são recuperados do contexto guardado. Altere o contexto do dossier antes de consultar.');
$control = Validator::make($input, ['version' => 'required|integer|min:1', 'operation_key' => 'required|uuid', 'activity_id' => 'nullable|uuid'])->validate();
$signature = hash('sha256', json_encode(\Illuminate\Support\Arr::except($control, ['version', 'operation_key']), JSON_THROW_ON_ERROR));
return DB::transaction(function () use ($actor, $id, $control, $protected, $signature): array {
$case = $this->desk->case($actor, $id, true, true);
abort_unless((int) $case->user_id === $actor->id, 403);
$this->desk->assertOpen($case);
$payload = $this->desk->decode($case->payload);
$history = $payload['investor_path']['supply_aggregates'] ?? [];
foreach ($history as $saved) {
if ($saved['operation_key'] === $control['operation_key']) return ['version' => $case->version, 'analysis' => $saved, 'replayed' => true];
if ($saved['operation_key'] === $control['operation_key']) {
abort_if($protected && app(ProactiveSupply::class)->protectedHistory([$saved]) === [], 409, 'Esta consulta antiga não cumpre a protecção actual. Prepare uma nova consulta.');
abort_if($protected && ($saved['request_signature'] ?? null) !== $signature, 409, 'A mesma chave de consulta não pode ser usada com outra actividade. Prepare uma nova consulta.');
return ['version' => $case->version, 'analysis' => $protected ? app(ProactiveSupply::class)->protectedHistory([$saved])[0] : $saved, 'replayed' => true];
}
}
abort_unless((int) $case->version === (int) $control['version'], 409, 'O dossier mudou. Recupere-o antes de consultar.');
$context = $this->state($case);
abort_if(count($context['opportunity_choices'] ?? []) > 1, 422, 'Esta consulta agregada de QA utiliza apenas o contexto comum de uma ideia própria.');
abort_if(count($context['opportunity_choices'] ?? []) > 1, 422, 'Esta análise utiliza um único contexto de investimento. Escolha o investimento a analisar, preservando as restantes associações.');
$f = $context['fields'];
if ($protected && array_key_exists('activity_id', $control)) $f['activity_id'] = $control['activity_id'];
$territory = $f['locality_id'] ?? $f['admin_post_id'] ?? $f['district_id'] ?? $f['province_id'] ?? null;
abort_unless($territory, 422, 'Guarde primeiro a localização conhecida no dossier.');
$analysis = [...app(ProactiveSupply::class)->aggregate($actor, $territory), 'context_revision' => $context['revision'], 'recorded_at' => now()->toIso8601String(), 'operation_key' => $control['operation_key']];
$data = $protected ? app(ProactiveSupply::class)->protectedAggregate($actor, $id, $territory, $f) : app(ProactiveSupply::class)->aggregate($actor, $territory);
$analysis = [...$data, 'context_revision' => $context['revision'], 'recorded_at' => now()->toIso8601String(), 'operation_key' => $control['operation_key'], ...($protected ? ['request_signature' => $signature] : [])];
$payload['investor_path']['supply_aggregates'] = [...$history, $analysis];
DB::table('desk_cases')->where('id', $id)->update(['payload' => $this->desk->encode($payload), 'version' => $case->version + 1, 'updated_at' => now()]);
$this->desk->event($this->desk->case($actor, $id), $actor, 'investment_supply_aggregate_saved', ['context_revision' => $context['revision'], 'territory_id' => $territory, 'aggregate_only' => true]);
return ['version' => $case->version + 1, 'analysis' => $analysis, 'replayed' => false];
return ['version' => $case->version + 1, 'analysis' => $protected ? app(ProactiveSupply::class)->protectedHistory([$analysis])[0] : $analysis, 'replayed' => false];
});
}
+4 -2
View File
@@ -162,7 +162,7 @@ final class InvestorPath
}
$preparationTaskKeys = $opportunity !== null && ($owner || $manager) && app(PrivateInvestmentPreparation::class)->available() ? app(InvestmentPreparationWorkspace::class)->taskKeys($actor, $id, $opportunity) : null;
$supplyEnabled = (int) $case->user_id === $actor->id && config('investor-path.aggregate_supply_qa_enabled') && \App\Support\IsolatedQaDatabase::allowsSchemaChanges();
$supplyEnabled = (int) $case->user_id === $actor->id && app(ProactiveSupply::class)->aggregateAvailable();
return ['funding_selections' => ($owner || $manager) ? array_values(array_filter($path['funding_selections'] ?? [], fn ($row) => ($row['opportunity'] ?? null) === $opportunity)) : [], 'preparation_action' => ($owner || $manager) && app(PrivateInvestmentPreparation::class)->available() ? app(InvestmentPreparationWorkspace::class)->continuation($actor, $id, $opportunity, request()->query('swot_action')) : null, 'private_investment' => $preparation, 'case' => ['id' => $id, 'title' => $case->title, 'version' => $case->version, 'closed' => $case->closed_at !== null],
'journey_id' => $journey->id, 'definition_id' => $version->id, 'definition_version' => $version->version_no, 'source_sha256' => $definition['source_sha256'],
@@ -174,7 +174,9 @@ final class InvestorPath
'investment_context' => ($owner || $manager) ? [...$context, 'opportunity_choices' => $opportunity ? $context['opportunity_choices'] : $allChoices] : null,
'can_set_promoter' => (int) $case->user_id === $actor->id && $case->organization_id === null && ! $case->closed_at,
'results' => $results, 'requests' => $requests, 'documents' => $documents,
'supply_aggregate_enabled' => $supplyEnabled, 'supply_aggregates' => $supplyEnabled ? ($path['supply_aggregates'] ?? []) : [],
'supply_aggregate_enabled' => $supplyEnabled, 'supply_aggregate_protected' => $supplyEnabled && (bool) config('investor-path.protected_supply_enabled'),
'supply_activity_options' => $supplyEnabled && config('investor-path.protected_supply_enabled') ? app(ProactiveSupply::class)->aggregateActivityOptions(isset($context['fields']['sector_id']) ? (int) $context['fields']['sector_id'] : null) : [],
'supply_aggregates' => $supplyEnabled ? (config('investor-path.protected_supply_enabled') ? app(ProactiveSupply::class)->protectedHistory($path['supply_aggregates'] ?? []) : ($path['supply_aggregates'] ?? [])) : [],
'fiscal_reports' => (int) $case->user_id === $actor->id && (count($allChoices) < 2 || $opportunity !== null)
? app(SimulationFormDrafts::class)->dossierReportSources($actor, $id, $opportunity) : [],
'land_report' => $landCase ? ['id' => $landCase->id, 'version' => $landCase->version, 'updated_at' => $landCase->updated_at?->toIso8601String(),
+211 -2
View File
@@ -4,7 +4,10 @@ namespace App\Services;
use App\Models\Opportunity;
use App\Models\User;
use App\Support\IsolatedQaDatabase;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
@@ -36,14 +39,219 @@ final class ProactiveSupply
});
}
/** Aggregate access never confers the existing internal row/source permission. */
public function aggregateAvailable(): bool
{
return (bool) config('investor-path.protected_supply_enabled')
|| (config('investor-path.aggregate_supply_qa_enabled') && IsolatedQaDatabase::allowsSchemaChanges());
}
/** Fixed district/division cells; filters select released cells, never recalculate intersections.
* @param array<string,mixed> $fields
* @return array<string,mixed>
*/
public function protectedAggregate(User $actor, string $caseId, string $territory, array $fields): array
{
abort_unless(config('investor-path.protected_supply_enabled'), 404);
$case = app(VirtualDesk::class)->case($actor, $caseId, true, true);
abort_unless((int) $case->user_id === (int) $actor->id, 403);
app(VirtualDesk::class)->assertOpen($case);
$geo = app(InvestmentGeography::class);
$path = collect($geo->path($territory));
$province = $path->firstWhere('location_type', 'province');
abort_unless($province !== null, 422, 'Guarde uma localização com província conhecida no dossier.');
$scope = $path->firstWhere('location_type', 'district') ?? $province;
$nodes = $geo->publicReferences()->whereIn('id', $geo->descendants($province->id))->get(['id', 'parent_id', 'name', 'location_type'])->keyBy('id');
$mapping = [];
$scopes = [$province->id => $province];
foreach ($nodes as $node) {
$current = $node;
for ($depth = 0; $current && $depth < 5; $depth++) {
if ($current->location_type === 'district' || $current->location_type === 'province') {
break;
}
$current = $nodes->get($current->parent_id);
}
if ($current && in_array($current->location_type, ['district', 'province'], true)) {
$mapping[$node->id] = $current->id;
$scopes[$current->id] = $current;
}
}
$activities = $this->publicAggregateActivities();
$divisions = $activities->filter(fn ($a) => preg_match('/^[0-9]{2}$/D', $a->code) === 1)->groupBy('code');
$sector = isset($fields['sector_id']) ? (int) $fields['sector_id'] : null;
abort_unless($divisions->contains(fn ($references) => $references->contains(fn ($a) => ! $sector || (int) $a->effective_sector_id === $sector)), 422,
'A classificação de actividades aplicável a este sector ainda não está disponível para esta consulta. O dossier mantém-se guardado; esta limitação não significa ausência de empresas.');
$activity = ! empty($fields['activity_id']) ? $activities->firstWhere('id', $fields['activity_id']) : null;
abort_if(! empty($fields['activity_id']) && ! $activity, 422, 'A actividade guardada não está disponível nesta classificação. Reveja o contexto.');
abort_if($activity && $sector && (int) $activity->effective_sector_id !== $sector, 422, 'A actividade não corresponde ao sector guardado. Reveja o contexto.');
$rawActivityCode = $activity?->code;
abort_if($activity && ! is_string($rawActivityCode), 422, 'A referência de actividade não tem um código válido.');
$activityCode = is_string($rawActivityCode) ? str_replace('.', '', $rawActivityCode) : null;
abort_if($activityCode && ! preg_match('/^[0-9]{2,5}$/D', $activityCode), 422, 'Seleccione uma actividade com código CAE disponível.');
$division = $activityCode ? substr($activityCode, 0, 2) : null;
$previous = DB::selectOne("select current_setting('app.actor_user_id',true) as actor,current_setting('app.proactive_supply_read',true) as scope");
return $this->read($actor, function () use ($mapping, $scopes, $province, $scope, $territory, $path, $divisions, $sector, $division, $activity, $previous): array {
try {
$parts = [];
$bindings = [];
foreach ($mapping as $id => $parent) {
$parts[] = 'WHEN ? THEN ?';
$bindings[] = $id;
$bindings[] = $parent;
}
$scopeSql = 'CASE o.territory_id::text '.implode(' ', $parts).' ELSE NULL END';
$codeSql = "CASE WHEN replace(o.cae,'.','') ~ '^[0-9]{2,5}$' THEN substring(replace(o.cae,'.','') from 1 for 2) ELSE NULL END";
$rows = DB::table('proactive_supply_observations as o')->join('proactive_supply_sources as s', 's.sha256', '=', 'o.source_hash')->whereIn('o.territory_id', array_keys($mapping))
->whereRaw("s.receipt->>'candidate' = ? AND s.receipt->>'access' = 'internal'", ['a022ed9848b41ba342b4209414c2bbf2ebd0a7a3'])
->whereNull('o.duplicate_of')->where('o.identity_conflict', false)->whereRaw("trim(o.name) <> ''")
// The accepted importer uses an observation-specific key when NUIT is unresolved.
// Those keys must never masquerade as distinct documentary entities in the cohort.
->whereRaw("o.entity_key <> encode(sha256(convert_to('unresolved:' || o.source_hash || '|' || o.sheet || '|' || o.row_number::text,'UTF8')),'hex')")
->selectRaw($scopeSql.' as scope_id,'.$codeSql.' as code,count(DISTINCT o.entity_key) as entities,count(*) as observations', $bindings)
->groupByRaw('1,2')->orderByRaw('1,2')->get();
$groups = [];
foreach ($rows as $row) {
// Suppressed and absent cells have the same public representation. No marginal totals.
if ((int) $row->entities < 5 || ! $row->code || ! $divisions->has($row->code)) {
continue;
}
$references = $divisions->get($row->code);
if ($sector && ! $references->contains(fn ($a) => (int) $a->effective_sector_id === $sector)) {
continue;
}
if ($division && $row->code !== $division) {
continue;
}
if ($scope->id !== $province->id && ! in_array($row->scope_id, [$scope->id, $province->id], true)) {
continue;
}
$labels = $references->pluck('label_pt_mz')->unique();
$cellScope = $scopes[$row->scope_id];
$groups[] = ['code' => $row->code, 'label' => $labels->count() === 1 ? $labels->first() : 'Grupo CAE '.$row->code,
'total' => null, 'entities_band' => $this->countBand((int) $row->entities), 'observations_band' => $this->countBand((int) $row->observations),
'scope' => ['id' => $cellScope->id, 'name' => $cellScope->name, 'level' => $cellScope->location_type], 'first_year' => null, 'last_year' => null];
}
$years = [];
$unknownPeriod = false;
foreach (DB::table('proactive_supply_sources')->whereRaw("receipt->>'candidate' = ? AND receipt->>'access' = 'internal'", ['a022ed9848b41ba342b4209414c2bbf2ebd0a7a3'])->pluck('period') as $period) {
preg_match_all('/\b(?:19|20)[0-9]{2}\b/', $period, $matches);
if (! $matches[0]) {
$unknownPeriod = true;
}
$years = [...$years, ...$matches[0]];
}
sort($years);
$period = $years ? $years[0].($years[0] !== $years[array_key_last($years)] ? '–'.$years[array_key_last($years)] : '') : 'Por confirmar';
return ['privacy_version' => 1, 'privacy_policy' => 'fixed_district_cae_division_v1',
'requested_territory' => ['id' => $territory, 'name' => $path->last()->name],
'scope' => ['id' => $scope->id, 'name' => $scope->name, 'level' => $scope->location_type],
'broader_context' => $scope->id !== $territory, 'entities' => null, 'repeated' => null, 'uncertain' => null,
'groups' => $groups, 'truncated' => false, 'coverage_unknown' => true,
'filters' => ['sector_id' => $sector, 'activity_id' => $activity?->id, 'activity_label' => $activity?->label_pt_mz, 'division_code' => $division],
'source_summary' => ['label' => 'Base administrativa empresarial histórica incorporada no portal', 'period' => $period, 'incomplete_periods' => $unknownPeriod],
'limitations' => self::LIMITATIONS,
'interpretation' => 'Intervalos de cinco identidades documentais, nunca empresas únicas confirmadas. Observações consolidadas e identidades são medidas distintas. Uma identidade pode constar em várias actividades; não some grupos. Grupos com menos de cinco identidades, conflitos e dados sem classificação publicada não são divulgados; nenhum resultado pode significar ausência de registos ou protecção de informação.',
'privacy_notice' => 'A precisão mínima é distrital e a actividade é apresentada por divisão CAE. Não há totais marginais, contagens exactas, filtros livres ou datas por grupo. Dados apenas provinciais são contexto provincial, não implantação no distrito. Estes controlos reduzem a identificação indirecta; não constituem garantia matemática contra conhecimento externo.'];
} finally {
DB::select("select set_config('app.actor_user_id',?,true),set_config('app.proactive_supply_read',?,true)", [$previous->actor ?? '', $previous->scope ?? '']);
}
});
}
private function countBand(int $count): string
{
$lower = intdiv($count, 5) * 5;
return $lower.'–'.($lower + 4);
}
/** @return Collection<int,\stdClass> */
private function publicAggregateActivities(): Collection
{
$rows = app(EconomicReferenceCatalog::class)->activities()->keyBy('id');
return $rows->map(function ($row) use ($rows) {
$result = clone $row;
$current = $row;
$result->effective_sector_id = null;
for ($i = 0; $current && $i < 10; $i++) {
if ($current->sector_id !== null) {
$result->effective_sector_id = (int) $current->sector_id;
break;
}
$current = $current->parent_id ? $rows->get($current->parent_id) : null;
}
return $result;
})->values();
}
/** @return list<array{id:string,code:string,label:string}> */
public function aggregateActivityOptions(?int $sector): array
{
$rows = $this->publicAggregateActivities();
$divisions = $rows->filter(fn ($a) => preg_match('/^[0-9]{2}$/D', $a->code) === 1)->pluck('code')->all();
$result = [];
foreach ($rows as $a) {
if (! is_string($a->id) || ! is_string($a->code) || ! is_string($a->label_pt_mz)) {
continue;
}
if (($sector && $a->effective_sector_id !== $sector) || ! preg_match('/^[0-9]{2,5}$/D', str_replace('.', '', $a->code))
|| ! in_array(substr(str_replace('.', '', $a->code), 0, 2), $divisions, true)) {
continue;
}
$result[] = ['id' => $a->id, 'code' => $a->code, 'label' => $a->label_pt_mz];
}
return $result;
}
/** Old exact QA snapshots stay stored, but are not exposed by the protected capability.
* @param list<array<string,mixed>> $history
* @return list<array<string,mixed>>
*/
public function protectedHistory(array $history): array
{
$result = [];
foreach ($history as $row) {
if (($row['privacy_version'] ?? null) !== 1 || ($row['privacy_policy'] ?? null) !== 'fixed_district_cae_division_v1') {
continue;
}
$safe = Arr::only($row, ['privacy_version', 'privacy_policy', 'requested_territory', 'scope', 'broader_context', 'truncated', 'coverage_unknown', 'limitations', 'interpretation', 'privacy_notice', 'context_revision', 'recorded_at', 'operation_key']);
$safe['entities'] = $safe['repeated'] = $safe['uncertain'] = null;
$safe['groups'] = [];
foreach ($row['groups'] ?? [] as $group) {
if (! preg_match('/^[0-9]{2}$/D', $group['code'] ?? '') || ! $this->validBand($group['entities_band'] ?? '') || ! $this->validBand($group['observations_band'] ?? '')) {
continue;
}
$safe['groups'][] = [...Arr::only($group, ['code', 'label', 'entities_band', 'observations_band', 'scope']), 'total' => null, 'first_year' => null, 'last_year' => null];
}
$safe['filters'] = Arr::only($row['filters'] ?? [], ['sector_id', 'activity_id', 'activity_label', 'division_code']);
$safe['source_summary'] = Arr::only($row['source_summary'] ?? [], ['label', 'period', 'incomplete_periods']);
$result[] = $safe;
}
return $result;
}
private function validBand(string $band): bool
{
return preg_match('/^([0-9]{1,12})–([0-9]{1,12})$/uD', $band, $m) === 1
&& (int) $m[1] >= 5 && (int) $m[1] % 5 === 0 && (int) $m[2] - (int) $m[1] === 4;
}
/** No names, contacts, row identifiers or free-text activity descriptions leave this boundary.
* @return array<string,mixed>
*/
public function aggregate(User $actor, string $territory): array
{
abort_unless(config('investor-path.aggregate_supply_qa_enabled') && \App\Support\IsolatedQaDatabase::allowsSchemaChanges(), 404);
abort_unless(config('investor-path.aggregate_supply_qa_enabled') && IsolatedQaDatabase::allowsSchemaChanges(), 404);
$path = app(InvestmentGeography::class)->path($territory);
abort_if($path === [], 422, 'Confirme primeiro a localização do dossier.');
return $this->read($actor, function () use ($path, $territory): array {
$labels = app(EconomicReferenceCatalog::class)->activities()->groupBy('code')->map(fn ($rows) => $rows->pluck('label_pt_mz')->unique()->count() === 1 ? $rows->first()->label_pt_mz : null);
$scope = collect($path)->last();
@@ -58,10 +266,11 @@ final class ProactiveSupply
$base = $query($district->id);
}
}
$counts = (clone $base)->selectRaw("count(DISTINCT entity_key) FILTER (WHERE duplicate_of IS NULL AND NOT identity_conflict) AS entities, count(*) FILTER (WHERE duplicate_of IS NOT NULL) AS repeated, count(*) FILTER (WHERE identity_conflict) AS uncertain")->first();
$counts = (clone $base)->selectRaw('count(DISTINCT entity_key) FILTER (WHERE duplicate_of IS NULL AND NOT identity_conflict) AS entities, count(*) FILTER (WHERE duplicate_of IS NOT NULL) AS repeated, count(*) FILTER (WHERE identity_conflict) AS uncertain')->first();
$code = "CASE WHEN cae ~ '^[0-9.]{1,10}$' THEN cae ELSE '' END";
$groups = (clone $base)->whereNull('duplicate_of')->where('identity_conflict', false)->selectRaw($code." AS code, count(DISTINCT entity_key) AS total, min(substring(source_date from '(19[0-9]{2}|20[0-9]{2})')) AS first_year, max(substring(source_date from '(19[0-9]{2}|20[0-9]{2})')) AS last_year")
->groupByRaw($code)->orderByRaw($code)->limit(101)->get()->map(fn ($row) => ['code' => $row->code, 'label' => $row->code ? ($labels->get($row->code) ?? 'CAE '.$row->code.' — descrição por confirmar') : 'Actividade por classificar', 'total' => (int) $row->total, 'first_year' => $row->first_year, 'last_year' => $row->last_year])->all();
return ['requested_territory' => ['id' => $territory, 'name' => collect($path)->last()->name], 'scope' => ['id' => $scope->id, 'name' => $scope->name, 'level' => $scope->location_type],
'broader_context' => $scope->id !== $territory, 'entities' => (int) $counts->entities, 'repeated' => (int) $counts->repeated, 'uncertain' => (int) $counts->uncertain,
'groups' => array_slice($groups, 0, 100), 'truncated' => count($groups) > 100, 'coverage_unknown' => true, 'limitations' => self::LIMITATIONS,