Skip to content

A free open source IT asset/license management system

Item Acceptance: Fixed FD-56096 - tighter guard on acceptance deletion

snipe0aba33aa0afd
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index d3269a6115..a1b472bf21 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -16,6 +16,7 @@ use App\Models\AssetModel;
use App\Models\Category;
use App\Models\Checkoutable;
use App\Models\CheckoutAcceptance;
+use App\Models\Company;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\CustomField;
@@ -1365,29 +1366,24 @@ class ReportsController extends Controller
private function currentUserCanAccessAcceptance(CheckoutAcceptance $acceptance): bool
{
- if (Setting::getSettings()->full_multiple_companies_support != '1') {
+ if (! Company::isFullMultipleCompanySupportEnabled()) {
return true;
}
- $user = auth()->user();
-
- if (! $user->company_id || $user->isSuperUser()) {
+ if (auth()->user()->isSuperUser()) {
return true;
}
- // Bypass Eloquent global scopes so cross-company items are still found
+ // Bypass Eloquent global scopes so cross-company items are still resolved for the check.
$checkoutableType = $acceptance->checkoutable_type;
$checkoutable = $checkoutableType::withoutGlobalScopes()->find($acceptance->checkoutable_id);
+ // LicenseSeat has no company_id of its own; the parent License carries the company.
if ($checkoutable instanceof LicenseSeat) {
- $itemCompanyId = License::withoutGlobalScopes()
- ->where('id', $checkoutable->license_id)
- ->value('company_id');
- } else {
- $itemCompanyId = $checkoutable?->company_id;
+ $checkoutable = License::withoutGlobalScopes()->find($checkoutable->license_id);
}
- return $itemCompanyId === null || (int) $itemCompanyId === (int) $user->company_id;
+ return Company::isCurrentUserHasAccess($checkoutable);
}
private function getCheckoutMailType(CheckoutAcceptance $acceptance, $logItem): Mailable
diff --git a/tests/Feature/Reporting/DeleteAcceptanceAuthorizationTest.php b/tests/Feature/Reporting/DeleteAcceptanceAuthorizationTest.php
index 91911d46d1..0749068e59 100644
--- a/tests/Feature/Reporting/DeleteAcceptanceAuthorizationTest.php
+++ b/tests/Feature/Reporting/DeleteAcceptanceAuthorizationTest.php
@@ -75,6 +75,26 @@ class DeleteAcceptanceAuthorizationTest extends TestCase
$this->assertNotNull($acceptance->fresh()->deleted_at);
}
+ public function test_pivot_only_user_cannot_delete_acceptance_belonging_to_another_company()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $reporter = User::factory()->canViewReports()->create(['company_id' => null]);
+ $reporter->companies()->sync([$companyA->id]);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->for($assetB, 'checkoutable')->create();
+
+ $this->actingAs($reporter)
+ ->delete(route('reports/unaccepted_assets_delete', $acceptance->id))
+ ->assertRedirectToRoute('reports/unaccepted_assets')
+ ->assertSessionHas('error');
+
+ $this->assertNull($acceptance->fresh()->deleted_at);
+ }
+
public function test_company_scoping_not_enforced_when_fmcs_disabled()
{
$this->settings->disableMultipleFullCompanySupport();
diff --git a/tests/Feature/Reporting/SentReminderAuthorizationTest.php b/tests/Feature/Reporting/SentReminderAuthorizationTest.php
new file mode 100644
index 0000000000..7cd9568997
--- /dev/null
+++ b/tests/Feature/Reporting/SentReminderAuthorizationTest.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace Tests\Feature\Reporting;
+
+use App\Mail\CheckoutAssetMail;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Company;
+use App\Models\User;
+use Illuminate\Support\Facades\Mail;
+use Tests\TestCase;
+
+class SentReminderAuthorizationTest extends TestCase
+{
+ public function test_user_without_reports_view_cannot_send_reminder()
+ {
+ Mail::fake();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $asset = Asset::factory()->create();
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($asset, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAs(User::factory()->create())
+ ->post(route('reports/unaccepted_assets_sent_reminder'), ['acceptance_id' => $acceptance->id])
+ ->assertForbidden();
+
+ Mail::assertNothingSent();
+ }
+
+ public function test_reports_user_can_send_reminder_for_their_own_company()
+ {
+ Mail::fake();
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA] = Company::factory()->count(2)->create();
+
+ $assignee = User::factory()->create(['email' => '[email protected]', 'company_id' => $companyA->id]);
+ $asset = Asset::factory()->create(['company_id' => $companyA->id]);
+ $reporter = User::factory()->canViewReports()->create(['company_id' => $companyA->id]);
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($asset, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAs($reporter)
+ ->post(route('reports/unaccepted_assets_sent_reminder'), ['acceptance_id' => $acceptance->id])
+ ->assertRedirectToRoute('reports/unaccepted_assets')
+ ->assertSessionHas('success');
+
+ Mail::assertSent(CheckoutAssetMail::class);
+ }
+
+ public function test_reports_user_cannot_send_reminder_for_another_company()
+ {
+ Mail::fake();
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $reporter = User::factory()->canViewReports()->create(['company_id' => $companyA->id]);
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($assetB, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAs($reporter)
+ ->post(route('reports/unaccepted_assets_sent_reminder'), ['acceptance_id' => $acceptance->id])
+ ->assertRedirectToRoute('reports/unaccepted_assets')
+ ->assertSessionHas('error');
+
+ Mail::assertNothingSent();
+ }
+
+ public function test_pivot_only_user_cannot_send_reminder_for_another_company()
+ {
+ Mail::fake();
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $reporter = User::factory()->canViewReports()->create(['company_id' => null]);
+ $reporter->companies()->sync([$companyA->id]);
+
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($assetB, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAs($reporter)
+ ->post(route('reports/unaccepted_assets_sent_reminder'), ['acceptance_id' => $acceptance->id])
+ ->assertRedirectToRoute('reports/unaccepted_assets')
+ ->assertSessionHas('error');
+
+ Mail::assertNothingSent();
+ }
+
+ public function test_superuser_can_send_reminder_for_any_company()
+ {
+ Mail::fake();
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $superuser = User::factory()->superuser()->create(['company_id' => $companyA->id]);
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($assetB, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAs($superuser)
+ ->post(route('reports/unaccepted_assets_sent_reminder'), ['acceptance_id' => $acceptance->id])
+ ->assertRedirectToRoute('reports/unaccepted_assets')
+ ->assertSessionHas('success');
+
+ Mail::assertSent(CheckoutAssetMail::class);
+ }
+}