From 830a6cf67e9638931153dcc3d16d43583c2801a4 Mon Sep 17 00:00:00 2001 From: Till Deeke Date: Sat, 28 Jul 2018 01:15:32 +0200 Subject: [PATCH] Adds accepting/declining to new controller --- app/Events/ItemAccepted.php | 32 +++++ app/Events/ItemDeclined.php | 32 +++++ .../Account/AcceptanceController.php | 119 +++++++++++++++ app/Listeners/LogListener.php | 24 ++++ resources/views/account/accept.blade.php | 135 ++++++++++++++++++ routes/web.php | 9 ++ 6 files changed, 351 insertions(+) create mode 100644 app/Events/ItemAccepted.php create mode 100644 app/Events/ItemDeclined.php create mode 100644 app/Http/Controllers/Account/AcceptanceController.php create mode 100644 resources/views/account/accept.blade.php diff --git a/app/Events/ItemAccepted.php b/app/Events/ItemAccepted.php new file mode 100644 index 0000000000..3c854d9ea5 --- /dev/null +++ b/app/Events/ItemAccepted.php @@ -0,0 +1,32 @@ +item = $item; + $this->acceptedBy = $acceptedBy; + $this->signature = $signature; + } +} diff --git a/app/Events/ItemDeclined.php b/app/Events/ItemDeclined.php new file mode 100644 index 0000000000..a735c193ef --- /dev/null +++ b/app/Events/ItemDeclined.php @@ -0,0 +1,32 @@ +item = $item; + $this->declinedBy = $declinedBy; + $this->signature = $signature; + } +} diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php new file mode 100644 index 0000000000..ecf9924277 --- /dev/null +++ b/app/Http/Controllers/Account/AcceptanceController.php @@ -0,0 +1,119 @@ +getItemById($type, $id); + + if (is_null($item)) { + return redirect()->reoute('account.accept')->with('error', trans('admin/hardware/message.does_not_exist')); + } + + if ($item->isAccepted()) { + return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted')); + } + + if (! $item->isCheckedOutTo(Auth::user())) { + return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted')); + } + + if (!Company::isCurrentUserHasAccess($item)) { + return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions')); + } + + return view('account/accept', compact('item')); + } + + public function update(Request $request, $type, $id) { + $item = $this->getItemById($type, $id); + + if (is_null($item)) { + return redirect()->reoute('account.accept')->with('error', trans('admin/hardware/message.does_not_exist')); + } + + if ($item->isAccepted()) { + return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted')); + } + + if (! $item->isCheckedOutTo(Auth::user())) { + return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted')); + } + + if (!Company::isCurrentUserHasAccess($item)) { + return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions')); + } + + if (!$request->filled('asset_acceptance')) { + return redirect()->back()->with('error', trans('admin/users/message.error.accept_or_decline')); + } + + /** + * Get the signature and save it + */ + if ($request->filled('signature_output')) { + $path = config('app.private_uploads').'/signatures'; + $sig_filename = "siglog-" .Str::uuid() . '-'.date('Y-m-d-his').".png"; + $data_uri = e($request->input('signature_output')); + $encoded_image = explode(",", $data_uri); + $decoded_image = base64_decode($encoded_image[1]); + file_put_contents($path."/".$sig_filename, $decoded_image); + } + + + if ($request->input('asset_acceptance') == 'accepted') { + + $item->accept(Auth::user(), $sig_filename); + + event(new ItemAccepted($item, Auth::user(), $sig_filename)); + + $return_msg = trans('admin/users/message.accepted'); + + } else { + + $item->decline(Auth::user(), $sig_filename); + + event(new ItemDeclined($item, Auth::user(), $sig_filename)); + + $return_msg = trans('admin/users/message.declined'); + + } + + return redirect()->to('account/accept')->with('success', $return_msg); + } + + private function getItemById($type, $id) : ? Acceptable { + switch ($type) { + case 'asset': + $item = Asset::findOrFail($id); + break; + case 'consumable': + $item = Consumable::findOrFail($id); + break; + case 'license': + $item = License::findOrFail($id); + break; + default: + $item = null; + break; + } + + return $item; + } +} \ No newline at end of file diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index 2b35e63658..858c9ec291 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -9,6 +9,8 @@ use App\Events\AssetCheckedOut; use App\Events\ComponentCheckedIn; use App\Events\ComponentCheckedOut; use App\Events\ConsumableCheckedOut; +use App\Events\ItemAccepted; +use App\Events\ItemDeclined; use App\Events\LicenseCheckedIn; use App\Events\LicenseCheckedOut; use App\Models\Actionlog; @@ -63,6 +65,26 @@ class LogListener $event->license->logCheckout($event->note, $event->checkedOutTo); } + public function onItemAccepted(ItemAccepted $event) { + $logaction = new Actionlog(); + $logaction->item()->associate($event->item); + $logaction->target()->associate($event->acceptedBy); + $logaction->accept_signature = $event->signature; + $logaction->action_type = 'accepted'; + + $logaction->save(); + } + + public function onItemDeclined(ItemDeclined $event) { + $logaction = new Actionlog(); + $logaction->item()->associate($event->item); + $logaction->target()->associate($event->declinedBy); + $logaction->accept_signature = $event->signature; + $logaction->action_type = 'declined'; + + $logaction->save(); + } + /** * Register the listeners for the subscriber. * @@ -80,6 +102,8 @@ class LogListener 'ConsumableCheckedOut', 'LicenseCheckedIn', 'LicenseCheckedOut', + 'ItemAccepted', + 'ItemDeclined', ]; foreach($list as $event) { diff --git a/resources/views/account/accept.blade.php b/resources/views/account/accept.blade.php new file mode 100644 index 0000000000..9073f8516a --- /dev/null +++ b/resources/views/account/accept.blade.php @@ -0,0 +1,135 @@ +@extends('layouts/default') + +{{-- Page title --}} +@section('title') + Accept {{ $item->present()->name() }} + @parent +@stop + + +{{-- Page content --}} +@section('content') + + + + + + + +
+ + + + +
+
+
+
+
+
+ +
+ +
+ +
+ + @if ($item->getEula()) +
+
+ {!! $item->getEula() !!} +
+
+ @endif + + @if ($snipeSettings->require_accept_signature=='1') +
+ +

Sign below to indicate that you agree to the terms of service:

+ +
+
+ + +
+
+ +
+
+
+ @endif + +
+ +
+ +
+
+
+
+ +@stop + +@section('moar_scripts') + + + +@stop diff --git a/routes/web.php b/routes/web.php index 20974f5f80..4b6d13a7cd 100644 --- a/routes/web.php +++ b/routes/web.php @@ -274,6 +274,15 @@ Route::group([ 'prefix' => 'account', 'middleware' => ['auth']], function () { # Account Dashboard Route::get('/', [ 'as' => 'account', 'uses' => 'ViewAssetsController@getIndex' ]); + + Route::get('accept', 'Account\AcceptanceController@index') + ->name('account.accept'); + + Route::get('accept/{type}/{id}', 'Account\AcceptanceController@edit') + ->name('account.accept.item'); + + Route::post('accept/{type}/{id}', 'Account\AcceptanceController@update'); + });