oportunidades 7
tests / ci (push) Waiting to run

This commit is contained in:
2026-10-13 11:40:29 +02:00
parent 20971d523f
commit 287e991486
2902 changed files with 1085456 additions and 25957 deletions
+128 -24
View File
@@ -16,19 +16,20 @@ class PortalProfileAccess
/** @return list<string> */
public static function fingerprintFiles(): array
{
return [__FILE__, app_path('Models/User.php'), app_path('Models/Opportunity.php'), config_path('portal.php'), base_path('routes/web.php'),
return [__FILE__, app_path('Services/OpportunityContacts.php'), resource_path('js/components/opportunity-contact-form.tsx'), app_path('Services/PortalInternalDesignation.php'), app_path('Models/User.php'), app_path('Models/Opportunity.php'), config_path('portal.php'), base_path('routes/web.php'),
app_path('Http/Middleware/HandleInertiaRequests.php'), app_path('Services/OpportunityManagement.php'),
app_path('Services/OpportunityResources.php'), app_path('Services/OpportunityImageProcessor.php'),
app_path('Services/OpportunityResources.php'), app_path('Services/OpportunityImageProcessor.php'), app_path('Services/VerifiedDocumentBinary.php'),
app_path('Services/PortalProfileAdministration.php'), app_path('Services/PortalSuperusers.php'),
app_path('Services/OpportunityProfileWorkflow.php'), app_path('Http/Controllers/PortalProfileController.php'), app_path('Http/Controllers/DashboardController.php'),
app_path('Http/Controllers/OpportunityManagementController.php'),
app_path('Services/OpportunityCanonicalContract.php'), app_path('Services/OpportunityCanonicalResponse.php'), app_path('Services/OpportunityCategoryFields.php'), app_path('Services/OpportunityModularProfiles.php'), app_path('Services/OpportunityWorkflowTasks.php'), base_path('composer.json'), base_path('composer.lock'),
app_path('Services/OpportunityReviewMateriality.php'), app_path('Services/OpportunityReviewConditions.php'), app_path('Services/OpportunityCanonicalMetadata.php'), app_path('Services/OpportunityCompleteness.php'), app_path('Services/OpportunityCanonicalValue.php'),
resource_path('data/investmoz-opportunity-modular.schema.json'), resource_path('js/components/opportunity-canonical-fields.tsx'), resource_path('js/components/opportunity-parcel-geometry.tsx'),
app_path('Http/Controllers/OpportunityResourceController.php'),
app_path('Http/Controllers/InvestmentTerritoryController.php'),
app_path('Http/Middleware/PortalProfileSession.php'), app_path('Http/Middleware/ProtectProfileOpportunity.php'),
app_path('Http/Middleware/ProtectPrivilegedIdentity.php'), app_path('Http/Middleware/EnsurePortalProfiles.php'),
app_path('Http/Middleware/AuditProfileDecision.php'), app_path('Providers/AppServiceProvider.php'),
app_path('Http/Middleware/AuditProfileDecision.php'), app_path('Http/Middleware/EnsurePortalProfileAdministration.php'), app_path('Providers/AppServiceProvider.php'),
database_path('migrations/2026_10_10_040000_create_portal_profile_authorization.php'), base_path('bootstrap/app.php'),
resource_path('js/pages/profiles/request.tsx'), resource_path('js/pages/profiles/administration.tsx'), resource_path('js/pages/profiles/audit.tsx'),
resource_path('js/components/profile-navigation.tsx'), resource_path('js/components/opportunity-profile-actions.tsx'),
@@ -45,7 +46,17 @@ class PortalProfileAccess
public function available(): bool
{
return config('portal.profile_access_enabled') && Schema::hasTable('portal_role_assignments')
return config('portal.profile_access_enabled') && $this->installed();
}
public function governanceAvailable(): bool
{
return (config('portal.profile_administration_enabled') || config('portal.profile_access_enabled')) && $this->installed();
}
public function installed(): bool
{
return Schema::hasTable('portal_role_assignments')
&& Schema::hasTable('portal_superuser_pair') && Schema::hasTable('portal_profile_requests')
&& Schema::hasTable('portal_security_approvals') && Schema::hasColumn('users', 'portal_access_revision')
&& Schema::hasColumn('opportunities', 'workflow_context') && Schema::hasColumn('opportunities', 'owner_user_id')
@@ -55,7 +66,7 @@ class PortalProfileAccess
/** @return list<\stdClass> */
public function grants(User $actor, ?string $role = null): array
{
if (! $this->available() || ! $actor->hasVerifiedEmail()) {
if (! $this->governanceAvailable() || ! $actor->hasVerifiedEmail()) {
return [];
}
@@ -70,7 +81,7 @@ class PortalProfileAccess
public function superuser(User $actor): bool
{
return $this->available() && $actor->hasVerifiedEmail() && DB::table('portal_superuser_pair')->where('id', 1)
return $this->governanceAvailable() && $actor->hasVerifiedEmail() && DB::table('portal_superuser_pair')->where('id', 1)
->where(fn ($q) => $q->where('first_user_id', $actor->id)->orWhere('second_user_id', $actor->id))->exists();
}
@@ -89,8 +100,46 @@ class PortalProfileAccess
&& (int) session('portal.mfa_at', 0) >= time() - 900;
}
public function internalAssignment(User $actor, string $type): ?\stdClass
{
foreach ($this->grants($actor, 'promoter') as $grant) {
if ($grant->source === 'internal-portal-designation'
&& in_array($type, json_decode($grant->opportunity_types, true), true)
&& app(PortalInternalDesignation::class)->organization($grant->organization_id) !== null) {
return $grant;
}
}
return null;
}
public function promoterRecordScope(User $actor, Opportunity $record): bool
{
foreach ($this->grants($actor, 'promoter') as $grant) {
if (! in_array($record->opportunity_category, json_decode($grant->opportunity_types, true), true)) {
continue;
}
if ($grant->source === 'internal-portal-designation') {
if (app(PortalInternalDesignation::class)->organization($grant->organization_id) !== null
&& ($record->workflow_context['operator_organization_id'] ?? null) === $grant->organization_id
&& ((int) $record->owner_user_id === $actor->id || in_array($actor->id, $record->workflow_context['contributors'] ?? [], true))) {
return true;
}
} elseif ($grant->organization_id === $record->organization_id && $this->represents($actor, $record->organization_id)
&& ($record->organization_id !== null || (int) $record->owner_user_id === $actor->id)) {
return true;
}
}
return false;
}
public function scoped(User $actor, string $role, ?string $organization, string $type): bool
{
if ($role === 'promoter' && $organization !== null && $this->internalAssignment($actor, $type) !== null
&& DB::table('organizations')->where('id', $organization)->whereNull('deleted_at')->exists()) {
return true;
}
foreach ($this->grants($actor, $role) as $grant) {
if (($grant->organization_id === $organization || ($role !== 'promoter' && $grant->organization_id === null))
&& in_array($type, json_decode($grant->opportunity_types, true, 512, JSON_THROW_ON_ERROR), true)) {
@@ -103,6 +152,9 @@ class PortalProfileAccess
public function operational(User $actor, string $action, ?int $recordId = null): bool
{
if (! $this->available()) {
return false;
}
if ($this->has($actor, 'auditor')) {
return false;
}
@@ -110,8 +162,13 @@ class PortalProfileAccess
return true;
}
if (! $this->mfa($actor)) {
return false;
}
return DB::table('portal_security_approvals')->where('kind', 'elevation')->where('subject_user_id', $actor->id)
->whereNotNull('approved_by')->whereNull('consumed_at')->where('expires_at', '>', now())
->whereNotNull('approved_by')->whereColumn('approved_by', '!=', 'subject_user_id')
->whereColumn('approved_by', '!=', 'requested_by')->whereNull('consumed_at')->where('expires_at', '>', now())
->whereExists(fn ($q) => $q->selectRaw('1')->from('portal_superuser_pair')
->where(fn ($holders) => $holders->whereColumn('first_user_id', 'portal_security_approvals.approved_by')->orWhereColumn('second_user_id', 'portal_security_approvals.approved_by')))
->where('scope->action', $action)->where('scope->record_id', $recordId)->exists();
@@ -119,7 +176,9 @@ class PortalProfileAccess
public function canCreate(User $actor, ?string $organization, string $type): bool
{
return $this->available() && $this->scoped($actor, 'promoter', $organization, $type) && $this->operational($actor, 'create');
$included = in_array($type, OpportunityCanonicalContract::BASE_CATEGORIES, true) || app(OpportunityModularProfiles::class)->enabled();
return $included && $this->available() && $this->scoped($actor, 'promoter', $organization, $type) && $this->operational($actor, 'create');
}
public function legacyWriteAllowed(User $actor): bool
@@ -138,22 +197,33 @@ class PortalProfileAccess
&& ! DB::table('portal_security_approvals')->where('kind', 'replacement')->whereNotNull('consumed_at')->where('scope->old_user_id', $actor->id)->exists();
}
/** Administration never grants operational access to a dossier or its attachments. */
public function hasOperationalReadScope(User $actor, Opportunity $record): bool
{
$personal = $record->organization_id !== null || (int) $record->owner_user_id === $actor->id;
return $this->promoterRecordScope($actor, $record)
|| $this->scoped($actor, 'validator', $record->organization_id, $record->opportunity_category)
|| $this->scoped($actor, 'publisher', $record->organization_id, $record->opportunity_category);
}
public function canView(User $actor, Opportunity $record): bool
{
if ($record->owner_user_id !== null && ! $this->available()) {
return false;
}
if ($this->superuser($actor)) {
return ! $this->has($actor, 'auditor') && $this->hasOperationalReadScope($actor, $record)
&& $this->operational($actor, 'view', $record->id);
}
if ($record->owner_user_id === null) {
return $actor->can('manage-portal');
}
if (! $this->available() || ! $actor->hasVerifiedEmail()) {
return false;
}
if ($this->has($actor, 'auditor') || $this->superuser($actor)) {
return true;
}
$personal = $record->organization_id !== null || (int) $record->owner_user_id === $actor->id;
return ($personal && $this->scoped($actor, 'promoter', $record->organization_id, $record->opportunity_category))
|| $this->scoped($actor, 'validator', $record->organization_id, $record->opportunity_category)
|| $this->scoped($actor, 'publisher', $record->organization_id, $record->opportunity_category);
return $this->has($actor, 'auditor') || $this->hasOperationalReadScope($actor, $record);
}
public function canEdit(User $actor, Opportunity $record, ?string $newType = null): bool
@@ -162,7 +232,7 @@ class PortalProfileAccess
return $this->legacyWriteAllowed($actor);
}
return $this->canView($actor, $record) && ! $record->trashed()
return $this->canView($actor, $record) && $this->promoterRecordScope($actor, $record) && ! $record->trashed()
&& in_array($this->state($record), ['draft', 'needs_information'], true)
&& $this->scoped($actor, 'promoter', $record->organization_id, $newType ?? $record->opportunity_category)
&& $this->operational($actor, 'edit', $record->id);
@@ -197,22 +267,38 @@ class PortalProfileAccess
*/
public function scope(Builder $query, User $actor): Builder
{
if ($this->has($actor, 'auditor') || $this->superuser($actor)) {
if ($this->has($actor, 'auditor') && ! $this->superuser($actor)) {
return $query;
}
if ($this->superuser($actor)) {
$ids = DB::table('portal_security_approvals')->where('kind', 'elevation')->where('subject_user_id', $actor->id)
->where('scope->action', 'view')->whereNotNull('approved_by')->whereNull('consumed_at')->where('expires_at', '>', now())
->get()->filter(fn ($row) => $this->operational($actor, 'view', json_decode($row->scope, true)['record_id'] ?? null))
->map(fn ($row) => json_decode($row->scope, true)['record_id'])->all();
$query->whereIn('id', $ids);
}
return $query->where(function ($scope) use ($actor) {
$scope->whereRaw('1=0');
if ($actor->can('manage-portal')) {
if ($actor->can('manage-portal') && ! $this->superuser($actor)) {
$scope->orWhereNull('owner_user_id');
}
foreach ($this->grants($actor) as $grant) {
if ($grant->role === 'promoter' && ! $this->represents($actor, $grant->organization_id)) {
if (! in_array($grant->role, ['promoter', 'validator', 'publisher'], true)) {
continue;
}
if ($grant->role === 'promoter' && $grant->source !== 'internal-portal-designation' && ! $this->represents($actor, $grant->organization_id)) {
continue;
}
if ($grant->role === 'promoter' && $grant->source === 'internal-portal-designation' && app(PortalInternalDesignation::class)->organization($grant->organization_id) === null) {
continue;
}
$scope->orWhere(function ($allowed) use ($actor, $grant) {
$allowed->whereNotNull('owner_user_id')->whereIn('opportunity_category', json_decode($grant->opportunity_types, true, 512, JSON_THROW_ON_ERROR));
if ($grant->organization_id !== null) {
if ($grant->role === 'promoter' && $grant->source === 'internal-portal-designation') {
$allowed->where(fn ($q) => $q->where('owner_user_id', $actor->id)->orWhereJsonContains('workflow_context->contributors', $actor->id))
->where('workflow_context->operator_organization_id', $grant->organization_id);
} elseif ($grant->organization_id !== null) {
$allowed->where('organization_id', $grant->organization_id);
} elseif ($grant->role === 'promoter') {
$allowed->whereNull('organization_id')->where('owner_user_id', $actor->id);
@@ -225,23 +311,41 @@ class PortalProfileAccess
/** @return array<string,mixed>|null */
public function navigation(?User $actor): ?array
{
if (! $actor || ! $this->available() || ! $actor->hasVerifiedEmail()) {
if (! $actor || ! $this->governanceAvailable() || ! $actor->hasVerifiedEmail()) {
return null;
}
$contexts = [];
$seen = [];
$internalPromoter = false;
foreach ($this->grants($actor, 'promoter') as $grant) {
if ($grant->source === 'internal-portal-designation' && app(PortalInternalDesignation::class)->organization($grant->organization_id) !== null) {
$internalPromoter = true;
foreach (DB::table('organizations')->whereNull('deleted_at')->get(['id', 'name']) as $realPromoter) {
foreach (json_decode($grant->opportunity_types, true) as $type) {
$key = $realPromoter->id.'|'.$type;
if (! isset($seen[$key]) && $this->canCreate($actor, $realPromoter->id, $type)) {
$contexts[] = ['organization_id' => $realPromoter->id, 'organization_name' => $realPromoter->name, 'type' => $type];
$seen[$key] = true;
}
}
}
continue;
}
if (! $this->represents($actor, $grant->organization_id)) {
continue;
}
foreach (json_decode($grant->opportunity_types, true, 512, JSON_THROW_ON_ERROR) as $type) {
if ($this->canCreate($actor, $grant->organization_id, $type)) {
$key = ($grant->organization_id ?? 'individual').'|'.$type;
if (! isset($seen[$key]) && $this->canCreate($actor, $grant->organization_id, $type)) {
$contexts[] = ['organization_id' => $grant->organization_id, 'type' => $type];
$seen[$key] = true;
}
}
}
return ['contexts' => $contexts, 'mine' => $contexts ? '/minhas-oportunidades' : null, 'create' => $contexts ? '/minhas-oportunidades/create' : null,
'request' => '/perfil/promotor', 'review' => $this->has($actor, 'validator') || $this->has($actor, 'publisher') || $this->has($actor, 'auditor') || $this->superuser($actor) ? '/minhas-oportunidades' : null,
return ['contexts' => $contexts, 'internalPromoter' => $internalPromoter, 'mine' => $contexts ? '/minhas-oportunidades' : null, 'create' => $contexts ? '/minhas-oportunidades/create' : null,
'request' => '/perfil/promotor', 'review' => $this->available() && ($this->has($actor, 'validator') || $this->has($actor, 'publisher') || $this->has($actor, 'auditor') || $this->superuser($actor)) ? '/minhas-oportunidades' : null,
'audit' => $this->has($actor, 'auditor') || $this->superuser($actor) ? '/perfis/auditoria' : null, 'administration' => $this->superuser($actor) ? '/perfis/gestao' : null];
}