status !== AccountStatus::Aktiv) { return new AccessResult(false, null, 'Das Konto ist nicht aktiv.'); } if ($release->license_article_id !== $article->id) { return new AccessResult(false, null, 'Der Stand gehört zu einem anderen Artikel.'); } if (! in_array($release->status, [ReleaseStatus::Veroeffentlicht, ReleaseStatus::AbgeloestVerfuegbar], true)) { return new AccessResult(false, null, 'Dieser Stand ist nicht freigegeben.'); } $contained = $release->contents()->where('content_element_version_id', $version->id)->exists(); if (! $contained) { return new AccessResult(false, null, 'Die Fassung gehört nicht zum fixierten Inhalt dieses Stands.'); } if ($version->status === ContentVersionStatus::Gesperrt) { return new AccessResult(false, null, 'Die Fassung ist gesperrt.'); } $version->loadMissing('element'); $outputClass = $version->element->output_class; $now = now(); $candidates = Grant::query() ->where('license_article_id', $article->id) ->where('status', GrantStatus::Aktiv) ->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id)) ->get() ->filter(function (Grant $grant) use ($now): bool { if ($grant->valid_from !== null && $grant->valid_from->greaterThan($now)) { return false; } return $grant->valid_until === null || $grant->valid_until->greaterThan($now); }) ->filter(function (Grant $grant) use ($outputClass, $action): bool { $capabilities = $grant->rights_snapshot['capabilities'] ?? []; $actions = $capabilities[$outputClass] ?? []; return in_array($action, $actions, true); }) ->sort(function (Grant $left, Grant $right): int { $leftUntil = $left->valid_until?->getTimestamp() ?? PHP_INT_MAX; $rightUntil = $right->valid_until?->getTimestamp() ?? PHP_INT_MAX; return $leftUntil === $rightUntil ? $right->id <=> $left->id : $leftUntil <=> $rightUntil; }) ->values(); $grant = $candidates->last(); if ($grant === null) { return new AccessResult(false, null, 'Keine passende Berechtigung.'); } return new AccessResult(true, $grant); } }