]> _ Git - bastide-resah.git/commitdiff
wip #6857 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 10 Apr 2024 14:45:11 +0000 (16:45 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 10 Apr 2024 14:45:11 +0000 (16:45 +0200)
.docker/production/dockerterminal.bat
app/Http/Middleware/CheckIfAdmin.php [new file with mode: 0644]
app/Providers/AppServiceProvider.php

index c19c7453c96f42d4a5b0ce19a29792a7724f3e4f..5e641fe54faac429e37cff8ef5c68c9ab7264ac7 100644 (file)
@@ -1,3 +1,3 @@
 @echo off
 cls
-ssh -t root@godzilla.cubedesigners.com 'docker exec -it -u bastide-resah bastide-resah /bin/bash'
+ssh -t root@cloudatlas.cubedesigners.com 'docker exec -it -u bastide-resah bastide-resah /bin/bash'
diff --git a/app/Http/Middleware/CheckIfAdmin.php b/app/Http/Middleware/CheckIfAdmin.php
new file mode 100644 (file)
index 0000000..da1fa21
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+
+class CheckIfAdmin
+{
+    /**
+     * Checked that the logged in user is an administrator.
+     *
+     * --------------
+     * VERY IMPORTANT
+     * --------------
+     * If you have both regular users and admins inside the same table, change
+     * the contents of this method to check that the logged in user
+     * is an admin, and not a regular user.
+     *
+     * Additionally, in Laravel 7+, you should change app/Providers/RouteServiceProvider::HOME
+     * which defines the route where a logged in user (but not admin) gets redirected
+     * when trying to access an admin route. By default it's '/home' but Backpack
+     * does not have a '/home' route, use something you've built for your users
+     * (again - users, not admins).
+     *
+     * @param  \Illuminate\Contracts\Auth\Authenticatable|null  $user
+     * @return bool
+     */
+    private function checkIfUserIsAdmin($user)
+    {
+        // return ($user->is_admin == 1);
+        return true;
+    }
+
+    /**
+     * Answer to unauthorized access request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
+     */
+    private function respondToUnauthorizedRequest($request)
+    {
+        if ($request->ajax() || $request->wantsJson()) {
+            return response(trans('backpack::base.unauthorized'), 401);
+        } else {
+            return redirect()->guest(backpack_url('login'));
+        }
+    }
+
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        if (backpack_auth()->guest()) {
+            return $this->respondToUnauthorizedRequest($request);
+        }
+
+        if (! $this->checkIfUserIsAdmin(backpack_user())) {
+            return $this->respondToUnauthorizedRequest($request);
+        }
+
+        return $next($request);
+    }
+}
index 51590d1836f25063a1cb8e3f5dbb8ed4ccfec7d9..7ce2ac6ca39f112c234c32f406d6cb861445808a 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-
+namespace App\Providers;
 
 use Illuminate\Support\Facades\URL;
 use Illuminate\Support\ServiceProvider;