Öffne Katalog, Bibliothek und Testkauf für den MVP.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
463851d402
commit
fd6860efda
51 files changed
+2331
-31
No files matched your search
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Entitlement;
|
||||
|
||||
use App\Domain\Catalog\ArticleRelease;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\ContentVersionStatus;
|
||||
use App\Domain\Catalog\LicenseArticle;
|
||||
use App\Domain\Catalog\ReleaseStatus;
|
||||
use App\Domain\Identity\AccountStatus;
|
||||
use App\Models\User;
|
||||
|
||||
class DecideAccess
|
||||
{
|
||||
public function __invoke(
|
||||
User $user,
|
||||
LicenseArticle $article,
|
||||
ArticleRelease $release,
|
||||
ContentElementVersion $version,
|
||||
string $action,
|
||||
): AccessResult {
|
||||
if ($user->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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user