Hugh Harlequin 2 years ago
commit
9ccc38dd6f
5 changed files with 173 additions and 0 deletions
  1. 4 0
      .gitignore
  2. 21 0
      LICENSE
  3. 28 0
      composer.json
  4. 74 0
      src/Administrator.php
  5. 46 0
      src/TypeEnum.php

+ 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.

+ 28 - 0
composer.json

@@ -0,0 +1,28 @@
+{
+    "name": "hugh/laravel-admin-user-optimize",
+    "description": "laravel-admin user with url permission check",
+    "version": "0.0.1",
+    "keywords": ["Hugh", "Laravel", "User", "Permission", "URL"],
+    "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\\AdminUserOptimize\\": "src/"
+        }
+    }
+}

+ 74 - 0
src/Administrator.php

@@ -0,0 +1,74 @@
+<?php
+
+
+namespace Hugh\AdminUserOptimize;
+
+use Encore\Admin\Auth\Database\Administrator as AdminAdministrator;
+use Illuminate\Support\Str;
+
+class Administrator extends AdminAdministrator
+{
+    /**
+     * A User has and belongs to many permissions & User's permissions
+     *
+     * @return \Illuminate\Support\Collection
+     */
+    public function allPermissions() : \Illuminate\Support\Collection
+    {
+        return $this->roles->pluck('permissions')->flatten()->merge($this->permissions)->unique('id');
+    }
+
+    /**
+     * Check a user can access the url by method
+     * Warning!!!
+     * This function only check url & method
+     * But NOT check Middleware admin.permission: by Encore\Admin\Middleware\Permission@checkRoutePermission
+     *
+     * @param string $url
+     * @param <TypeEnum::*> $method
+     *
+     * @return bool
+     */
+    public function checkUrlPermission(string $url,$method = TypeEnum::GET) : bool
+    {
+        return !is_null($this->allPermissions()->first(function ($permission) use ($url, $method) {
+            return $this->shouldPassThrough($permission, $url, $method);
+        }));
+    }
+    private function shouldPassThrough($permission, $url, $requestMethod)
+    {
+        if (empty($permission->http_method) && empty($permission->http_path)) {
+            return true;
+        }
+
+        $method = $permission->http_method;
+
+        $matches = array_map(function ($path) use ($method) {
+
+            if (Str::contains($path, ':')) {
+                list($method, $path) = explode(':', $path);
+                $method = explode(',', $method);
+            }
+
+            return compact('method', 'path');
+        }, explode("\n", $permission->http_path));
+        foreach ($matches as $match) {
+
+            $path = $match['path'] === '/' ? '/' : trim($match['path'], '/');
+
+            if (!Str::is($path, $url)) {
+                return false;
+            }
+
+            $method = collect($match['method'])->filter()->map(function ($method) {
+                return strtoupper($method);
+            });
+
+            if ($method->isEmpty() || $method->contains($requestMethod)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+}

+ 46 - 0
src/TypeEnum.php

@@ -0,0 +1,46 @@
+<?php
+
+
+namespace Hugh\AdminUserOptimize;
+
+
+class TypeEnum
+{
+    /**
+     * POST
+     */
+    const POST = 'POST';
+
+    /**
+     * GET
+     */
+    const GET = 'GET';
+    /**
+     * HEAD
+     */
+    const HEAD = 'HEAD';
+    /**
+     * PUT
+     */
+    const PUT = 'PUT';
+    /**
+     * DELETE
+     */
+    const DELETE = 'DELETE';
+    /**
+     * CONNECT
+     */
+    const CONNECT = 'CONNECT';
+    /**
+     * OPTIONS
+     */
+    const OPTIONS = 'OPTIONS';
+    /**
+     * TRACE
+     */
+    const TRACE = 'TRACE';
+    /**
+     * PATCH
+     */
+    const PATCH = 'PATCH';
+}