From 196e585c873249807690aced67dc0cd650185a93 Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Wed, 18 Dec 2019 16:20:14 -0500 Subject: [PATCH] Prevent editing or deleting demo user in demo environment --- app/Http/Controllers/UsersController.php | 10 ++++++++++ app/Providers/AppServiceProvider.php | 1 + resources/js/Shared/FlashMessages.vue | 5 +++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 091fb29..53b746b 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -5,9 +5,11 @@ namespace App\Http\Controllers; use App\User; use Inertia\Inertia; use Illuminate\Validation\Rule; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Redirect; +use Illuminate\Validation\ValidationException; class UsersController extends Controller { @@ -77,6 +79,10 @@ class UsersController extends Controller public function update(User $user) { + if (App::environment('demo') && $user->id === 1) { + return Redirect::route('users.edit', $user)->with('error', 'Updating the demo user is not allowed.'); + } + Request::validate([ 'first_name' => ['required', 'max:50'], 'last_name' => ['required', 'max:50'], @@ -101,6 +107,10 @@ class UsersController extends Controller public function destroy(User $user) { + if (App::environment('demo') && $user->id === 1) { + return Redirect::route('users.edit', $user)->with('error', 'Deleting the demo user is not allowed.'); + } + $user->delete(); return Redirect::route('users.edit', $user)->with('success', 'User deleted.'); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e612ed6..fba7899 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -54,6 +54,7 @@ class AppServiceProvider extends ServiceProvider 'flash' => function () { return [ 'success' => Session::get('success'), + 'error' => Session::get('error'), ]; }, 'errors' => function () { diff --git a/resources/js/Shared/FlashMessages.vue b/resources/js/Shared/FlashMessages.vue index 6bc99c5..9828144 100644 --- a/resources/js/Shared/FlashMessages.vue +++ b/resources/js/Shared/FlashMessages.vue @@ -9,10 +9,11 @@ -
+
-
+
{{ $page.flash.error }}
+
There is one form error. There are {{ Object.keys($page.errors).length }} form errors.