A free open source IT asset/license management system
Fixed #19570 - resolve user from asset in component acceptance
snipe83397a8818f2
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.phpindex ba83a20afa..bbbd73cdc2 100644--- a/app/Listeners/CheckoutableListener.php+++ b/app/Listeners/CheckoutableListener.php@@ -278,8 +278,17 @@ class CheckoutableListener*/private function getCheckoutAcceptance($event){- $checkedOutToType = get_class($event->checkedOutTo);- if ($checkedOutToType != "App\Models\User") {+ // Resolve the acceptance target: the user who actually needs+ // to accept. When the checkoutable was handed to a User+ // directly, that's the target. When the checkoutable was+ // handed to an Asset (which happens for Components checked+ // out to an asset that's already assigned to a user), the+ // asset's assigned User is the effective target, so they can+ // accept the component from their profile. Any other target+ // shape (Location, unassigned Asset, etc.) has no user on the+ // hook, so no acceptance row is written. See GH #19570.+ $acceptanceTarget = $this->resolveAcceptanceTarget($event->checkedOutTo);+ if ($acceptanceTarget === null) {return null;}@@ -292,12 +301,32 @@ class CheckoutableListenerreturn CreateCheckoutAcceptanceAction::run($event->checkoutable,- $event->checkedOutTo,+ $acceptanceTarget,$event->checkoutable->checkout_qty ?? 1,$alertOnResponseId,);}+ /**+ * Walks a checkout target down to the User who should sign the+ * acceptance. Direct-user targets pass through. Asset targets+ * unwrap to the asset's currently-assigned User (if any). Any+ * other target shape returns null and the caller skips the+ * acceptance write.+ */+ private function resolveAcceptanceTarget($checkedOutTo): ?User+ {+ if ($checkedOutTo instanceof User) {+ return $checkedOutTo;+ }++ if ($checkedOutTo instanceof Asset && $checkedOutTo->assignedto instanceof User) {+ return $checkedOutTo->assignedto;+ }++ return null;+ }+/*** Get the appropriate notification for the event*diff --git a/resources/views/mail/markdown/checkout-component.blade.php b/resources/views/mail/markdown/checkout-component.blade.phpindex 8c06dc1d6e..de9a685c21 100644--- a/resources/views/mail/markdown/checkout-component.blade.php+++ b/resources/views/mail/markdown/checkout-component.blade.php@@ -24,9 +24,16 @@@endif@endcomponent-@if (($req_accept == 1) && ($eula!=''))+{{-- Accept-required copy is gated on $accept_url so the "please+ read and click" line and the accept button below only render+ when there's an actual acceptance record for the recipient to+ click through to. Otherwise (e.g. a component checked out to+ an asset whose assigned user could not be resolved) the email+ used to render an empty [text]() markdown link that read as+ broken. See GH #19570. --}}+@if (($req_accept == 1) && ($accept_url) && ($eula!='')){{ trans('mail.read_the_terms_and_click') }}-@elseif (($req_accept == 1) && ($eula==''))+@elseif (($req_accept == 1) && ($accept_url) && ($eula=='')){{ trans('mail.click_on_the_link_asset') }}@elseif (($req_accept == 0) && ($eula!='')){{ trans('mail.read_the_terms') }}@@ -38,7 +45,7 @@@endcomponent@endif-@if ($req_accept == 1)+@if (($req_accept == 1) && ($accept_url))**[✔ {{ trans('mail.i_have_read') }}]({{ $accept_url }})**@endifdiff --git a/tests/Feature/Checkouts/Ui/ComponentCheckoutAcceptanceTest.php b/tests/Feature/Checkouts/Ui/ComponentCheckoutAcceptanceTest.phpnew file mode 100644index 0000000000..b433c6826a--- /dev/null+++ b/tests/Feature/Checkouts/Ui/ComponentCheckoutAcceptanceTest.php@@ -0,0 +1,102 @@+<?php++namespace Tests\Feature\Checkouts\Ui;++use App\Models\Asset;+use App\Models\Category;+use App\Models\CheckoutAcceptance;+use App\Models\Component;+use App\Models\User;+use PHPUnit\Framework\Attributes\Test;+use Tests\TestCase;++/**+ * Regression coverage for GH #19570: components with require_acceptance+ * on their category didn't produce a CheckoutAcceptance row when the+ * checkout target was an Asset. That left the recipient (the asset's+ * assigned user) with an email that had a broken accept link and no+ * pending row to accept from their profile.+ *+ * The fix routes any Asset target through the asset's assignedto to+ * pick the real acceptance target, and the mail template no longer+ * renders the "please accept" copy or button when no accept URL exists.+ */+class ComponentCheckoutAcceptanceTest extends TestCase+{+ #[Test]+ public function component_checkout_to_asset_creates_acceptance_for_the_assets_assigned_user(): void+ {+ $assignedUser = User::factory()->create();+ $asset = Asset::factory()->assignedToUser($assignedUser)->create();++ $category = Category::factory()->componentRamCategory()->create([+ 'require_acceptance' => true,+ ]);+ $component = Component::factory()->for($category)->create();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('components.checkout.store', $component), [+ 'asset_id' => $asset->id,+ 'assigned_qty' => '1',+ 'redirect_option' => 'index',+ ]);++ $this->assertDatabaseHas('checkout_acceptances', [+ 'checkoutable_type' => Component::class,+ 'checkoutable_id' => $component->id,+ 'assigned_to_id' => $assignedUser->id,+ ]);+ }++ #[Test]+ public function component_checkout_to_asset_with_no_assigned_user_skips_acceptance(): void+ {+ // Asset that's not checked out to anyone. No user to sign an+ // acceptance, so no row should land.+ $asset = Asset::factory()->create();++ $category = Category::factory()->componentRamCategory()->create([+ 'require_acceptance' => true,+ ]);+ $component = Component::factory()->for($category)->create();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('components.checkout.store', $component), [+ 'asset_id' => $asset->id,+ 'assigned_qty' => '1',+ 'redirect_option' => 'index',+ ]);++ $this->assertDatabaseMissing('checkout_acceptances', [+ 'checkoutable_type' => Component::class,+ 'checkoutable_id' => $component->id,+ ]);+ }++ #[Test]+ public function component_checkout_without_required_acceptance_writes_no_row(): void+ {+ // Regression guard on the pre-existing branch: if the+ // category does not require acceptance, no row is written+ // even when the target chain resolves to a real user.+ $assignedUser = User::factory()->create();+ $asset = Asset::factory()->assignedToUser($assignedUser)->create();++ $category = Category::factory()->componentRamCategory()->create([+ 'require_acceptance' => false,+ ]);+ $component = Component::factory()->for($category)->create();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('components.checkout.store', $component), [+ 'asset_id' => $asset->id,+ 'assigned_qty' => '1',+ 'redirect_option' => 'index',+ ]);++ $this->assertDatabaseMissing('checkout_acceptances', [+ 'checkoutable_type' => Component::class,+ 'checkoutable_id' => $component->id,+ ]);+ }+}