51 lines
2.6 KiB
PHP
51 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Opportunity;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/** Navigation over existing authorisations; never grants a role or publishes content. */
|
|
final class ContentManagement
|
|
{
|
|
public function access(?User $actor): array
|
|
{
|
|
if (! $actor || ! $actor->hasVerifiedEmail()) return [];
|
|
$profiles = app(PortalProfileAccess::class);
|
|
$navigation = $profiles->navigation($actor);
|
|
$legacy = $profiles->legacyWriteAllowed($actor);
|
|
$documents = app(DocumentManagementAccess::class);
|
|
$editor = $documents->full($actor) || $documents->editorial($actor);
|
|
$upload = $editor || $documents->limited($actor);
|
|
$result = [];
|
|
if (app(Procurement::class)->available() && app(Procurement::class)->access($actor)) $result['procurement'] = ['index' => '/management/concursos', 'create' => '/management/concursos/novo'];
|
|
if ($legacy || ($navigation['mine'] ?? null) || ($navigation['review'] ?? null)) {
|
|
$result['opportunities'] = ['index' => $legacy ? '/management/opportunities' : '/minhas-oportunidades',
|
|
'create' => $legacy ? '/management/opportunities/create' : ($navigation['create'] ?? null), 'profileWorkflow' => ! $legacy];
|
|
}
|
|
if ($upload) $result['documents'] = ['index' => '/management/portal-services', 'edit' => $editor];
|
|
return $result;
|
|
}
|
|
|
|
public function view(User $actor): array
|
|
{
|
|
$access = $this->access($actor);
|
|
abort_unless($access !== [], 403);
|
|
$counts = [];
|
|
if (isset($access['opportunities'])) {
|
|
$counts['opportunities'] = app(PortalProfileAccess::class)->scope(Opportunity::query(), $actor)
|
|
->selectRaw("COALESCE(workflow_context->>'working_state',status) as state, count(*) as total")
|
|
->groupByRaw("COALESCE(workflow_context->>'working_state',status)")->pluck('total', 'state');
|
|
}
|
|
if (isset($access['documents'])) {
|
|
$documents = app(DocumentManagementAccess::class);
|
|
$query = $documents->full($actor) ? DB::table('portal_resources as r')->join('documents as d', 'd.id', '=', 'r.document_id')->whereNull('d.deleted_at')
|
|
: ($access['documents']['edit'] ? $documents->editorialResources($actor) : null);
|
|
$counts['documents'] = $query ? $query->select('r.published', DB::raw('count(*) as total'))->groupBy('r.published')->get()
|
|
->mapWithKeys(fn ($row) => [$row->published ? 'published' : 'unpublished' => (int) $row->total]) : [];
|
|
}
|
|
return ['access' => $access, 'counts' => $counts];
|
|
}
|
|
}
|