76 lines
3.5 KiB
PHP
76 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/** Separate private read authority approved by the owner; never a Finance/Admin/metadata fallback. */
|
|
final class PrivateAuditAccess
|
|
{
|
|
public const FAMILIES = [
|
|
'purchasing' => ['permission' => 'audit_private_purchasing_read', 'label' => 'Compras e documentos de origem'],
|
|
'stock' => ['permission' => 'audit_private_stock_read', 'label' => 'Recepções, stock e movimentos físicos'],
|
|
'finance' => ['permission' => 'audit_private_finance_read', 'label' => 'Orçamento, facturas e pagamentos registados'],
|
|
'configuration' => ['permission' => 'audit_private_configuration_read', 'label' => 'Regras, perfis e alterações da configuração'],
|
|
];
|
|
|
|
public function ready(): bool
|
|
{
|
|
$db = DB::connection();
|
|
|
|
return config('private-audit.qa_enabled') === true && app()->environment('testing')
|
|
&& (($db->getDriverName() === 'sqlite' && $db->getDatabaseName() === ':memory:')
|
|
|| ($db->getDriverName() === 'pgsql' && $db->getConfig('host') === '127.0.0.1'
|
|
&& preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $db->getDatabaseName())))
|
|
&& Schema::hasTable('private_audit_exports');
|
|
}
|
|
|
|
/** @return array{actor:User,context:string,granted:list<string>} */
|
|
public function current(User $candidate): array
|
|
{
|
|
abort_unless($this->ready(), 503);
|
|
abort_unless(auth()->id() === $candidate->id && $candidate->tenant_id !== null && auth()->user()?->tenant_id === $candidate->tenant_id, 403);
|
|
$actor = User::query()->where('tenant_id', $candidate->tenant_id)->whereKey($candidate->id)->firstOrFail();
|
|
$access = app(OrganizationAccess::class);
|
|
abort_if($access->maintenanceOnly($actor), 403);
|
|
$contexts = app(WorkContext::class);
|
|
$context = 'legacy';
|
|
if ($contexts->enforced($actor)) {
|
|
abort_unless(app()->bound('request') && request()->hasSession() && $contexts->active(request()) === 'procurement.internal', 403);
|
|
$context = 'procurement.internal';
|
|
}
|
|
$permissions = $access->permissions($actor, $context === 'legacy' ? null : $context);
|
|
$granted = [];
|
|
foreach (self::FAMILIES as $key => $definition) {
|
|
if (in_array($definition['permission'], $permissions, true)) {
|
|
$granted[] = $key;
|
|
}
|
|
}
|
|
abort_unless($granted !== [], 403);
|
|
|
|
return ['actor' => $actor, 'context' => $context, 'granted' => $granted];
|
|
}
|
|
|
|
/** Untrusted family selection from the export request is validated before producing a typed scope.
|
|
* @param array<array-key,mixed> $families
|
|
* @return array{actor:User,context:string,families:list<string>}
|
|
*/
|
|
public function authorise(User $candidate, array $families): array
|
|
{
|
|
$state = $this->current($candidate);
|
|
abort_unless($families !== [] && count($families) <= count(self::FAMILIES) && array_is_list($families), 422);
|
|
$selected = [];
|
|
foreach ($families as $family) {
|
|
abort_unless(is_string($family) && isset(self::FAMILIES[$family]), 422);
|
|
abort_unless(in_array($family, $state['granted'], true), 403);
|
|
$selected[] = $family;
|
|
}
|
|
$selected = array_values(array_unique($selected));
|
|
sort($selected);
|
|
|
|
return ['actor' => $state['actor'], 'context' => $state['context'], 'families' => $selected];
|
|
}
|
|
}
|