Skip to content

A free open source IT asset/license management system

Refactor CheckoutRequest actions for consistency

spencerrlongg513c78a09f36
diff --git a/app/Actions/CheckoutRequests/CancelCheckoutRequest.php b/app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php
similarity index 83%
rename from app/Actions/CheckoutRequests/CancelCheckoutRequest.php
rename to app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php
index ccad6b2e64..2d6dd68a08 100644
--- a/app/Actions/CheckoutRequests/CancelCheckoutRequest.php
+++ b/app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php
@@ -4,14 +4,20 @@ namespace App\Actions\CheckoutRequests;
use App\Models\Actionlog;
use App\Models\Asset;
+use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\RequestAssetCancelation;
+use Illuminate\Auth\Access\AuthorizationException;
-class CancelCheckoutRequest
+class CancelCheckoutRequestAction
{
public static function run(Asset $asset, User $user)
{
+ if (!Company::isCurrentUserHasAccess($asset)) {
+ throw new AuthorizationException();
+ }
+
$asset->cancelRequest();
$asset->decrement('requests_counter', 1);
diff --git a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php b/app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php
similarity index 93%
rename from app/Actions/CheckoutRequests/CreateCheckoutRequest.php
rename to app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php
index 9d1ba332ff..6870cfba2d 100644
--- a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php
+++ b/app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php
@@ -3,18 +3,16 @@
namespace App\Actions\CheckoutRequests;
use App\Exceptions\AssetNotRequestable;
-use App\Exceptions\ThereIsNoUser;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
-use App\Notifications\RequestAssetCancelation;
use App\Notifications\RequestAssetNotification;
use Illuminate\Auth\Access\AuthorizationException;
use Log;
-class CreateCheckoutRequest
+class CreateCheckoutRequestAction
{
/**
* @throws AssetNotRequestable
diff --git a/app/Http/Controllers/Api/CheckoutRequest.php b/app/Http/Controllers/Api/CheckoutRequest.php
index 1ee9c4c025..9b66531ae4 100644
--- a/app/Http/Controllers/Api/CheckoutRequest.php
+++ b/app/Http/Controllers/Api/CheckoutRequest.php
@@ -2,40 +2,43 @@
namespace App\Http\Controllers\Api;
-use App\Actions\CheckoutRequests\CancelCheckoutRequest;
-use App\Actions\CheckoutRequests\CreateCheckoutRequest;
+use App\Actions\CheckoutRequests\CancelCheckoutRequestAction;
+use App\Actions\CheckoutRequests\CreateCheckoutRequestAction;
use App\Exceptions\AssetNotRequestable;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\Asset;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
+use Exception;
class CheckoutRequest extends Controller
{
public function store(Asset $asset): JsonResponse
{
try {
- CreateCheckoutRequest::run($asset, auth()->user());
+ CreateCheckoutRequestAction::run($asset, auth()->user());
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.requests.success')));
} catch (AssetNotRequestable $e) {
return response()->json(Helper::formatStandardApiResponse('error', 'Asset is not requestable'));
} catch (AuthorizationException $e) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions')));
- } catch (\Exception $e) {
+ } catch (Exception $e) {
report($e);
- return response()->json(Helper::formatStandardApiResponse('error', null, 'Something terrible has gone wrong and we\'re not sure if we can help - may god have mercy on your soul. Contact your admin :)'));
+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.something_went_wrong')));
}
}
public function destroy(Asset $asset): JsonResponse
{
try {
- CancelCheckoutRequest::run($asset, auth()->user());
+ CancelCheckoutRequestAction::run($asset, auth()->user());
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.requests.canceled')));
- } catch (\Exception $e) {
+ } catch (AuthorizationException $e) {
+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions')));
+ } catch (Exception $e) {
report($e);
- return response()->json(Helper::formatStandardApiResponse('error', null, $e->getMessage()));
+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.something_went_wrong')));
}
}
}
diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php
index 56d1f8b390..bbff6ba4f7 100755
--- a/app/Http/Controllers/ViewAssetsController.php
+++ b/app/Http/Controllers/ViewAssetsController.php
@@ -2,8 +2,8 @@
namespace App\Http\Controllers;
-use App\Actions\CheckoutRequests\CancelCheckoutRequest;
-use App\Actions\CheckoutRequests\CreateCheckoutRequest;
+use App\Actions\CheckoutRequests\CancelCheckoutRequestAction;
+use App\Actions\CheckoutRequests\CreateCheckoutRequestAction;
use App\Exceptions\AssetNotRequestable;
use App\Models\Actionlog;
use App\Models\Asset;
@@ -16,7 +16,7 @@ use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use \Illuminate\Contracts\View\View;
-use Illuminate\Support\Facades\Log;
+use Exception;
/**
* This controller handles all actions related to the ability for users
@@ -150,26 +150,26 @@ class ViewAssetsController extends Controller
public function store(Asset $asset): RedirectResponse
{
try {
- CreateCheckoutRequest::run($asset, auth()->user());
+ CreateCheckoutRequestAction::run($asset, auth()->user());
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
} catch (AssetNotRequestable $e) {
return redirect()->back()->with('error', 'Asset is not requestable');
} catch (AuthorizationException $e) {
return redirect()->back()->with('error', trans('admin/hardware/message.requests.error'));
- } catch (\Exception $e) {
+ } catch (Exception $e) {
report($e);
- return redirect()->back()->with('error', 'Something terrible has gone wrong and we\'re not sure if we can help - may god have mercy on your soul. Contact your admin :)');
+ return redirect()->back()->with('error', trans('general.something_went_wrong'));
}
}
public function destroy(Asset $asset): RedirectResponse
{
try {
- CancelCheckoutRequest::run($asset, auth()->user());
+ CancelCheckoutRequestAction::run($asset, auth()->user());
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
- } catch (\Exception $e) {
+ } catch (Exception $e) {
report($e);
- return redirect()->back()->with('error', 'something bad happened');
+ return redirect()->back()->with('error', trans('general.something_went_wrong'));
}
}
diff --git a/routes/web.php b/routes/web.php
index 5482de9cac..473ac9a2b4 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -1,6 +1,5 @@
<?php
-use App\Actions\CheckoutRequests\CreateCheckoutRequest;
use App\Http\Controllers\Account;
use App\Http\Controllers\ActionlogController;
use App\Http\Controllers\CategoriesController;