43 lines
2.7 KiB
PHP
43 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\Opportunity;
|
|
use App\Services\PortalProfileAccess;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ProtectProfileOpportunity
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$id = null;
|
|
if (preg_match('#^(?:(?:management|api/v1)/opportunities|api/v1/investment/admin/opportunities|minhas-oportunidades|management/territories/opportunities)/(\d+)(?:/|$)#', $request->path(), $match)) {
|
|
$id = (int) $match[1];
|
|
} elseif ($request->is('management/opportunity-origin/preview', 'management/opportunity-origin/apply')) {
|
|
$id = filter_var($request->input('id'), FILTER_VALIDATE_INT) ?: null;
|
|
} elseif ($request->is('api/v2/investment/admin/zones/*/links')) {
|
|
$id = filter_var($request->input('opportunity_id'), FILTER_VALIDATE_INT) ?: null;
|
|
}
|
|
if ($request->user() && $id !== null && Schema::hasColumn('opportunities', 'owner_user_id')) {
|
|
$record = Opportunity::withTrashed()->find($id);
|
|
if ($record && $record->owner_user_id === null && ! $request->isMethod('GET')) {
|
|
abort_unless(app(PortalProfileAccess::class)->legacyWriteAllowed($request->user()), 403, 'O papel v1 não contorna os controlos através de um registo legado.');
|
|
}
|
|
if ($record && ($record->owner_user_id !== null || str_starts_with($request->path(), 'minhas-oportunidades'))) {
|
|
abort_if($record->owner_user_id === null && str_starts_with($request->path(), 'minhas-oportunidades'), 403, 'Registo legado não convertido para a política v1.');
|
|
$access = app(PortalProfileAccess::class);
|
|
// The promotional controller checks its own scoped disclosure capability,
|
|
// like workflow decisions; publication does not grant draft editing.
|
|
$access->authorize($request->user(), $request->isMethod('GET') || str_ends_with($request->path(), '/workflow') || str_ends_with($request->path(), '/preview') || $request->is('minhas-oportunidades/*/classification/review', 'minhas-oportunidades/*/divulgacao/previa', 'minhas-oportunidades/*/divulgacao/confirmar') ? 'view' : 'edit', $record);
|
|
abort_if($request->is('management/opportunity-origin/apply'), 403, 'O cadastro privado v1 não usa a classificação editorial dos exemplos legados.');
|
|
abort_if($request->isMethod('POST') && str_ends_with($request->path(), '/lifecycle') && ! str_contains($request->path(), '/resources/'), 403, 'Use o workflow com decisões segregadas; o perfil não concede exclusão.');
|
|
}
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|