Bläddra i källkod

middlware for admin

Carello Qwerty 5 år sedan
förälder
incheckning
eed4cd4ac6

+ 65 - 0
app/Admin/Widgets/LogoutNav.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Admin\Widgets;
+
+use AdminTemplate;
+use SleepingOwl\Admin\Widgets\Widget;
+
+class LogoutNav extends Widget
+{
+
+    /**
+     * Если метод вернет false, блок не будет помещен в шаблон
+     * Данный метод не обязателен
+     *
+     * @return boolean
+     */
+    public function active()
+    {
+        return true;
+    }
+
+    /**
+     * При помещении в один блок нескольких виджетов они будут выведены в порядке их позиции
+     * Данный метод не обязателен
+     *
+     * @return integer
+     */
+    public function position()
+    {
+        return 1;
+    }
+
+    /**
+     * шаблон HTML который необходимо поместить
+     *
+     * @return string
+     */
+    public function toHtml()
+    {
+        return view('admin.navbar', [
+            'user' => auth()->user(),
+        ])->render();
+    }
+
+    /**
+     * Путь до шаблона, в который добавляем
+     *
+     * @return string|array
+     */
+    public function template()
+    {
+        // AdminTemplate::getViewPath('dashboard') == 'sleepingowl:default.dashboard'
+        return \AdminTemplate::getViewPath('_partials.header');
+    }
+
+    /**
+     * Блок в шаблоне, куда помещаем
+     *
+     * @return string
+     */
+    public function block()
+    {
+        return 'navbar.right';
+    }
+}

+ 1 - 0
app/Http/Kernel.php

@@ -61,6 +61,7 @@ class Kernel extends HttpKernel
         'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
         'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
+        'admin' => \App\Http\Middleware\Admin::class,
     ];
 
     /**

+ 37 - 0
app/Http/Middleware/Admin.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Log;
+
+class Admin
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+
+    public function handle($request, Closure $next, $guard = null)
+    {
+        $auth = Auth::guard($guard);
+
+        if (Auth::guard($guard)->guest()) {
+            if ($request->ajax() || $request->wantsJson()) {
+                return response('Unauthorized.', 401);
+            } else {
+                return redirect()->guest('/');
+            }
+        }
+
+        if ($auth->user()->email != 'admin@geolook.com') {
+            return abort(404);
+        }
+
+        return $next($request);
+    }
+}

+ 8 - 1
app/Providers/AppServiceProvider.php

@@ -7,6 +7,9 @@ use Illuminate\Support\ServiceProvider;
 
 class AppServiceProvider extends ServiceProvider
 {
+    protected $widgets = [
+        \App\Admin\Widgets\LogoutNav::class,
+    ];
     /**
      * Register any application services.
      *
@@ -24,6 +27,10 @@ class AppServiceProvider extends ServiceProvider
      */
     public function boot()
     {
-//        URL::forceScheme('https');
+        $widgetsRegistry = $this->app[\SleepingOwl\Admin\Contracts\Widgets\WidgetsRegistryInterface::class];
+
+        foreach ($this->widgets as $widget) {
+            $widgetsRegistry->registerWidget($widget);
+        }
     }
 }

+ 1 - 1
config/sleeping_owl.php

@@ -50,7 +50,7 @@ return [
     |
     */
 
-    'middleware' => ['web'],
+    'middleware' => ['web', 'admin'],
 
     /*
     |--------------------------------------------------------------------------

+ 32 - 0
database/migrations/2019_04_17_104050_alter_default_value_for_role_id_column_in_users_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AlterDefaultValueForRoleIdColumnInUsersTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->unsignedInteger('role_id')->after('google_id')->nullable()->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 22 - 0
resources/views/admin/navbar.blade.php

@@ -0,0 +1,22 @@
+<!-- Authentication Links -->
+@if (Auth::guest())
+    <li><a href="{{ url('/login') }}">Login</a></li>
+    <li><a href="{{ url('/register') }}">Register</a></li>
+@else
+    <li class="dropdown">
+        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
+            {{ Auth::user()->name }} <span class="caret"></span>
+        </a>
+
+        <ul class="dropdown-menu" role="menu">
+            <li><a class="scroll" href="{{ route('logout') }}"
+                   onclick="event.preventDefault();
+                                                 document.getElementById('logout-form').submit();">
+                    <i class="fa fa-btn fa-sign-out"></i>Вихід
+                </a></li>
+            <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
+                @csrf
+            </form>
+        </ul>
+    </li>
+@endif