Hugh Harlequin 2 năm trước cách đây
commit
7388ea37a9

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/vendor
+composer.phar
+.DS_Store
+.idea

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Hugh Harlequin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 36 - 0
composer.json

@@ -0,0 +1,36 @@
+{
+    "name": "hugh/laravel-admin-permission-optimize",
+    "description": "laravel-admin permission without query all the time",
+    "version": "0.0.1",
+    "keywords": ["Hugh", "Laravel", "Permission"],
+    "homepage": "https://git.hugh2113.com/hugh",
+    "license": "MIT",
+
+    "authors": [
+        {
+            "name": "Hugh Harlequin",
+            "email": "[email protected]"
+        }
+    ],
+
+    "require": {
+        "php": ">=5.4.0",
+        "ext-curl": "*",
+        "illuminate/support": ">=4.0",
+        "encore/laravel-admin": ">=1.7.0"
+    },
+
+    "autoload": {
+        "psr-4": {
+            "Hugh\\AdminPermissionOptimize\\": "src/"
+        }
+    },
+    "extra": {
+        "laravel": {
+            "providers": [
+                "Hugh\\AdminPermissionOptimize\\ServiceProvider\\AuthServiceProvider",
+                "Hugh\\AdminPermissionOptimize\\ServiceProvider\\AdminServiceProvider"
+            ]
+        }
+    }
+}

+ 22 - 0
src/EloquentUserProvider.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Hugh\AdminPermissionOptimize;
+
+class EloquentUserProvider extends \Illuminate\Auth\EloquentUserProvider
+{
+    /**
+     * Retrieve a user by their unique identifier.
+     *
+     * @param  mixed  $identifier
+     * @return \Illuminate\Contracts\Auth\Authenticatable|null
+     */
+    public function retrieveById($identifier)
+    {
+        $model = $this->createModel();
+
+        return $this->newModelQuery($model)
+            ->with(['roles.permissions', 'permissions'])
+            ->where($model->getAuthIdentifierName(), $identifier)
+            ->first();
+    }
+}

+ 41 - 0
src/Middleware/Permission.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace Hugh\AdminPermissionOptimize\Middleware;
+
+use Encore\Admin\Auth\Permission as Checker;
+use Encore\Admin\Facades\Admin;
+use Illuminate\Http\Request;
+
+class Permission extends \Encore\Admin\Middleware\Permission
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param \Illuminate\Http\Request $request
+     * @param \Closure                 $next
+     * @param array                    $args
+     *
+     * @return mixed
+     */
+    public function handle(Request $request, \Closure $next, ...$args)
+    {
+        if (config('admin.check_route_permission') === false) {
+            return $next($request);
+        }
+        $user = Admin::user();
+        if (!$user || !empty($args) || $this->shouldPassThrough($request)) {
+            return $next($request);
+        }
+
+        if ($this->checkRoutePermission($request)) {
+            return $next($request);
+        }
+        if (!$user->roles->pluck('permissions')->flatten()->merge($user->permissions)->first(function ($permission) use ($request) {
+            return $permission->shouldPassThrough($request);
+        })) {
+            Checker::error();
+        }
+
+        return $next($request);
+    }
+}

+ 23 - 0
src/ServiceProvider/AdminServiceProvider.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace Hugh\AdminPermissionOptimize\ServiceProvider;
+
+use Encore\Admin\Middleware;
+use Hugh\AdminPermissionOptimize\Middleware\Permission;
+
+class AdminServiceProvider extends \Encore\Admin\AdminServiceProvider
+{
+    /**
+     * The application's route middleware.
+     *
+     * @var array
+     */
+    protected $routeMiddleware = [
+        'admin.auth'       => Middleware\Authenticate::class,
+        'admin.pjax'       => Middleware\Pjax::class,
+        'admin.log'        => Middleware\LogOperation::class,
+        'admin.permission' => Permission::class,
+        'admin.bootstrap'  => Middleware\Bootstrap::class,
+        'admin.session'    => Middleware\Session::class,
+    ];
+}

+ 26 - 0
src/ServiceProvider/AuthServiceProvider.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Hugh\AdminPermissionOptimize\ServiceProvider;
+
+use Hugh\AdminPermissionOptimize\EloquentUserProvider;
+use Illuminate\Support\Facades\Auth;
+
+class AuthServiceProvider extends \Illuminate\Foundation\Support\Providers\AuthServiceProvider {
+
+    /**
+     * @var bool
+     */
+    protected $defer = false;
+
+
+    /**
+     * @return void
+     */
+    public function register()
+    {
+        Auth::provider('eloquent-with', function ($app, array $config){
+            return new EloquentUserProvider($this->app['hash'], $config['model']);
+        });
+    }
+
+}