Skip to content

A free open source IT asset/license management system

Graceful redirect if the user is not allowed

snipea23bc89607b0
diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php
index 8e8f70cca0..89b9f3b5a6 100755
--- a/app/Http/Controllers/ViewAssetsController.php
+++ b/app/Http/Controllers/ViewAssetsController.php
@@ -19,6 +19,7 @@ use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Log;
/**
* This controller handles all actions related to the ability for users
@@ -200,17 +201,23 @@ class ViewAssetsController extends Controller
$settings = Setting::getSettings();
- if ($cancel_by_admin) {
- $this->authorize('index', Asset::class);
+ $is_admin = $user->isSuperUser() || $user->isAdmin();
+
+ if ($cancel_by_admin && ! $is_admin) {
+ return redirect()->back()->with('error', trans('general.insufficient_permissions'));
}
- if (($item_request = $item->isRequestedBy($user)) || $cancel_by_admin) {
- $item->cancelRequest($cancel_by_admin ? $requestingUser : null);
+ if (($item_request = $item->isRequestedBy($user)) || ($is_admin && $cancel_by_admin)) {
+ $item->cancelRequest($is_admin && $cancel_by_admin ? $requestingUser : null);
$data['item_quantity'] = ($item_request) ? $item_request->qty : 1;
$logaction->logaction(ActionType::RequestCanceled);
if (($settings->alert_email != '') && ($settings->alerts_enabled == '1') && (! config('app.lock_passwords'))) {
- $settings->notify((new RequestAssetCancelation($data))->locale($settings->locale));
+ try {
+ $settings->notify((new RequestAssetCancelation($data))->locale($settings->locale));
+ } catch (Exception $e) {
+ Log::warning('Could not send request cancellation notification: '.$e->getMessage());
+ }
}
return redirect()->back()->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
@@ -218,7 +225,11 @@ class ViewAssetsController extends Controller
$item->request();
if (($settings->alert_email != '') && ($settings->alerts_enabled == '1') && (! config('app.lock_passwords'))) {
$logaction->logaction('requested');
- $settings->notify((new RequestAssetNotification($data))->locale($settings->locale));
+ try {
+ $settings->notify((new RequestAssetNotification($data))->locale($settings->locale));
+ } catch (Exception $e) {
+ Log::warning('Could not send asset request notification: '.$e->getMessage());
+ }
}
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
diff --git a/tests/Feature/Requests/Ui/CancelRequestTest.php b/tests/Feature/Requests/Ui/CancelRequestTest.php
index af8fd94b50..6b85bfaa72 100644
--- a/tests/Feature/Requests/Ui/CancelRequestTest.php
+++ b/tests/Feature/Requests/Ui/CancelRequestTest.php
@@ -5,6 +5,8 @@ namespace Tests\Feature\Requests\Ui;
use App\Models\Asset;
use App\Models\CheckoutRequest;
use App\Models\User;
+use Illuminate\Notifications\ChannelManager;
+use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class CancelRequestTest extends TestCase
@@ -40,7 +42,8 @@ class CancelRequestTest extends TestCase
'cancel_by_admin' => '1',
'requestingUser' => $victim->id,
]))
- ->assertForbidden();
+ ->assertRedirect()
+ ->assertSessionHas('error');
$this->assertNull(
CheckoutRequest::where('requestable_id', $asset->id)
@@ -85,13 +88,95 @@ class CancelRequestTest extends TestCase
);
}
+ public function test_superuser_can_cancel_another_users_request_via_cancel_by_admin(): void
+ {
+ $asset = Asset::factory()->create();
+ $victim = User::factory()->create();
+ CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $victim->id]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('account/request-item', [
+ 'itemType' => 'asset',
+ 'itemId' => $asset->id,
+ 'cancel_by_admin' => '1',
+ 'requestingUser' => $victim->id,
+ ]))
+ ->assertRedirect();
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $asset->id)
+ ->where('user_id', $victim->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_cancel_succeeds_and_logs_warning_when_notification_throws(): void
+ {
+ $asset = Asset::factory()->create();
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $user->id]);
+
+ $this->settings->enableAlertEmail();
+ config(['app.lock_passwords' => false]);
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andThrow(new \Exception('Mail server down'));
+ });
+
+ Log::spy();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', ['itemType' => 'asset', 'itemId' => $asset->id]))
+ ->assertRedirect()
+ ->assertSessionHas('success');
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $asset->id)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+
+ Log::shouldHaveReceived('warning')->once();
+ }
+
+ public function test_new_request_succeeds_and_logs_warning_when_notification_throws(): void
+ {
+ $asset = Asset::factory()->create();
+ $user = User::factory()->create();
+
+ $this->settings->enableAlertEmail();
+ config(['app.lock_passwords' => false]);
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andThrow(new \Exception('Mail server down'));
+ });
+
+ Log::spy();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', ['itemType' => 'asset', 'itemId' => $asset->id]))
+ ->assertRedirect()
+ ->assertSessionHas('success');
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $asset->id)
+ ->where('user_id', $user->id)
+ ->whereNull('canceled_at')
+ ->first()
+ );
+
+ Log::shouldHaveReceived('warning')->once();
+ }
+
public function test_admin_can_cancel_another_users_request_via_cancel_by_admin(): void
{
$asset = Asset::factory()->create();
$victim = User::factory()->create();
CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $victim->id]);
- $this->actingAs(User::factory()->viewAssets()->create())
+ $this->actingAs(User::factory()->admin()->create())
->post(route('account/request-item', [
'itemType' => 'asset',
'itemId' => $asset->id,