Files
laraundfelix/app/Domain/Entitlement/DecideAccess.php
T
Lars 4e0760db1e
Deploy Test / test (push) Successful in 1m38s
Deploy Test / deploy (push) Successful in 31s
Add feature tests for shop functionality and organization purchases
- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation.
- Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing.
- Add ShopProductPageTest to verify shop product listings, search functionality, and product page details.
- Introduce BuildsShop trait for seeding test data related to shop functionality.
- Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
2026-11-21 12:06:24 +01:00

82 lines
2.9 KiB
PHP

<?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)
->usableBy($user)
->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);
}
}