clear(); $allowed = DB::table('company_memberships') ->where('user_id', $userId)->where('tenant_id', $tenantId) ->where('company_id', $companyId)->exists(); if (! $allowed || ! Company::query()->where('tenant_id', $tenantId)->whereKey($companyId)->exists()) { throw new AccessDeniedHttpException('Empresa não autorizada.'); } app(Permissions::class)->require($userId, $tenantId, $companyId, 'company.view'); if ($unitId !== null) { app(Permissions::class)->require($userId, $tenantId, $companyId, 'unit.view', $unitId); abort_unless(DB::table('units')->where('tenant_id', $tenantId)->where('company_id', $companyId)->where('id', $unitId)->exists(), 403); } $this->identifiers = ['tenant_id' => $tenantId, 'company_id' => $companyId]; $this->userId = $userId; $this->unitId = $unitId; } public function identifiers(): array { return $this->identifiers ?? throw new AccessDeniedHttpException('Contexto de empresa obrigatório.'); } public function clear(): void { $this->identifiers = null; $this->userId = null; $this->unitId = null; } public function userId(): int { $this->identifiers(); return $this->userId; } public function unitId(): ?int { $this->identifiers(); return $this->unitId; } public function permittedUnitIds(): array { $ids = $this->identifiers(); return app(Permissions::class)->unitIds($this->userId(), $ids['tenant_id'], $ids['company_id']); } }