Преглед на файлове

增加配置参数控制是否启用默认用户系统

jqh преди 5 години
родител
ревизия
d140f7061c

+ 10 - 0
config/admin.php

@@ -115,6 +115,7 @@ return [
     |
     */
     'auth' => [
+        'enable' => true,
 
         'controller' => Dcat\Admin\Controllers\AuthController::class,
 
@@ -156,6 +157,15 @@ return [
         'grid_action_class' => Dcat\Admin\Grid\Displayers\DropdownActions::class,
     ],
 
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin helpers setting.
+    |--------------------------------------------------------------------------
+    */
+    'helpers' => [
+        'enable' => true,
+    ],
+
     /*
     |--------------------------------------------------------------------------
     | dcat-admin permission setting

+ 30 - 20
src/Admin.php

@@ -245,7 +245,7 @@ class Admin
      */
     public static function section(Closure $builder = null)
     {
-        $manager = app('sectionManager');
+        $manager = app('admin.sections');
 
         $builder && $builder($manager);
 
@@ -253,11 +253,11 @@ class Admin
     }
 
     /**
-     * Register the auth routes.
+     * Register the admin routes.
      *
      * @return void
      */
-    public static function registerAuthRoutes()
+    public static function routes()
     {
         $attributes = [
             'prefix'     => config('admin.route.prefix'),
@@ -265,32 +265,38 @@ class Admin
         ];
 
         app('router')->group($attributes, function ($router) {
+            $enableAuth = config('admin.auth.enable', true);
 
             /* @var \Illuminate\Routing\Router $router */
-            $router->namespace('Dcat\Admin\Controllers')->group(function ($router) {
-
-                /* @var \Illuminate\Routing\Router $router */
-                $router->resource('auth/users', 'UserController');
-                $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
-                $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
-
-                if (config('admin.permission.enable')) {
-                    $router->resource('auth/roles', 'RoleController');
-                    $router->resource('auth/permissions', 'PermissionController');
+            $router->namespace('Dcat\Admin\Controllers')->group(function ($router) use ($enableAuth) {
+                if ($enableAuth) {
+                    /* @var \Illuminate\Routing\Router $router */
+                    $router->resource('auth/users', 'UserController');
+                    $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
+                    $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
+
+                    if (config('admin.permission.enable')) {
+                        $router->resource('auth/roles', 'RoleController');
+                        $router->resource('auth/permissions', 'PermissionController');
+                    }
                 }
 
                 $router->post('_handle_action_', 'HandleActionController@handle')->name('admin.handle-action');
                 $router->post('_handle_form_', 'HandleFormController@handle')->name('admin.handle-form');
             });
 
-            $authController = config('admin.auth.controller', AuthController::class);
+            if ($enableAuth) {
+                $authController = config('admin.auth.controller', AuthController::class);
 
-            $router->get('auth/login', $authController.'@getLogin');
-            $router->post('auth/login', $authController.'@postLogin');
-            $router->get('auth/logout', $authController.'@getLogout');
-            $router->get('auth/setting', $authController.'@getSetting');
-            $router->put('auth/setting', $authController.'@putSetting');
+                $router->get('auth/login', $authController . '@getLogin');
+                $router->post('auth/login', $authController . '@postLogin');
+                $router->get('auth/logout', $authController . '@getLogout');
+                $router->get('auth/setting', $authController . '@getSetting');
+                $router->put('auth/setting', $authController . '@putSetting');
+            }
         });
+
+        static::registerHelperRoutes();
     }
 
     /**
@@ -298,8 +304,12 @@ class Admin
      *
      * @return void
      */
-    public static function registerHelperRoutes()
+    protected static function registerHelperRoutes()
     {
+        if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
+            return;
+        }
+
         $attributes = [
             'prefix'     => config('admin.route.prefix'),
             'middleware' => config('admin.route.middleware'),

+ 3 - 6
src/AdminServiceProvider.php

@@ -83,7 +83,7 @@ class AdminServiceProvider extends ServiceProvider
      */
     public function register()
     {
-        require_once __DIR__.'/Support/AdminSection.php';
+        require __DIR__.'/Support/AdminSection.php';
 
         $this->registerExtensionProviders();
         $this->loadAdminAuthConfig();
@@ -202,13 +202,10 @@ class AdminServiceProvider extends ServiceProvider
      */
     protected function registerServices()
     {
-        $this->app->singleton('sectionManager', SectionManager::class);
-
+        $this->app->singleton('admin.sections', SectionManager::class);
         $this->app->singleton('admin.navbar', Navbar::class);
-
         $this->app->singleton('admin.menu', Menu::class);
-
-        $this->app->singleton('admin.temp', Fluent::class);
+        $this->app->singleton('admin.context', Fluent::class);
     }
 
     /**

+ 1 - 5
src/Console/stubs/routes.stub

@@ -4,11 +4,7 @@ use Illuminate\Routing\Router;
 use Illuminate\Support\Facades\Route;
 use Dcat\Admin\Admin;
 
-Admin::registerAuthRoutes();
-
-if (config('app.debug')) {
-    Admin::registerHelperRoutes();
-}
+Admin::routes();
 
 Route::group([
     'prefix'        => config('admin.route.prefix'),

+ 2 - 4
src/Form/Field/WebUploader.php

@@ -2,9 +2,9 @@
 
 namespace Dcat\Admin\Form\Field;
 
-use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
 use Illuminate\Support\Facades\URL;
+use Illuminate\Support\Str;
 
 /**
  * @property Form $form
@@ -143,8 +143,6 @@ trait WebUploader
      */
     protected function setupDefaultOptions()
     {
-        $primaryKey = Admin::user() ? Admin::user()->getKey() : null;
-
         $defaultOptions = [
             'isImage'             => false,
             'disableRemove'       => false,
@@ -164,7 +162,7 @@ trait WebUploader
                 '_method'                => 'PUT',
             ],
             'formData' => [
-                '_id'           => $primaryKey,
+                '_id'           => Str::random(),
                 'upload_column' => $this->column,
                 '_method'       => 'PUT',
                 '_token'        => csrf_token(),

+ 1 - 1
src/Form/Tools.php

@@ -255,7 +255,7 @@ HTML;
 
         foreach ($this->tools as $tool => $enable) {
             if ($enable) {
-                $renderMethod = 'render' . ucfirst($tool);
+                $renderMethod = 'render'.ucfirst($tool);
 
                 $output .= $this->$renderMethod();
             }

+ 1 - 1
src/Layout/Menu.php

@@ -66,7 +66,7 @@ class Menu
             });
         }
 
-        if (config('app.debug')) {
+        if (config('app.debug') && config('admin.helpers.enable', true)) {
             $this->add(static::$helperNodes, 20);
         }
     }

+ 13 - 13
src/Layout/SectionManager.php

@@ -42,7 +42,7 @@ class SectionManager
      *
      * @return void
      */
-    public function injectDefault($section, $content)
+    public function injectDefault(string $section, $content)
     {
         if ($this->hasSection($section)) {
             return;
@@ -61,7 +61,7 @@ class SectionManager
      *
      * @return void
      */
-    protected function put($section, $content, bool $append = false, int $priority = 10)
+    protected function put(string $section, $content, bool $append = false, int $priority = 10)
     {
         if (! $section) {
             throw new \InvalidArgumentException('Section name is required.');
@@ -86,13 +86,13 @@ class SectionManager
     /**
      * Get the string contents of a section.
      *
-     * @param $section
-     * @param string $default
+     * @param string $section
+     * @param mixed  $default
      * @param array  $options
      *
      * @return string
      */
-    public function yieldContent($section, $default = '', array $options = [])
+    public function yieldContent(string $section, $default = '', array $options = [])
     {
         $defaultSection = $this->defaultSections[$section] ?? null;
 
@@ -112,7 +112,7 @@ class SectionManager
      *
      * @return array
      */
-    public function getSections($name)
+    public function getSections(string $name)
     {
         return $this->sortSections($name);
     }
@@ -124,7 +124,7 @@ class SectionManager
      *
      * @return array
      */
-    protected function sortSections($name)
+    protected function sortSections(string $name)
     {
         if (empty($this->sections[$name])) {
             return [];
@@ -144,7 +144,7 @@ class SectionManager
      *
      * @return bool
      */
-    public function hasSection($name)
+    public function hasSection(string $name)
     {
         return array_key_exists($name, $this->sections);
     }
@@ -156,19 +156,19 @@ class SectionManager
      *
      * @return bool
      */
-    public function hasDefaultSection($name)
+    public function hasDefaultSection(string $name)
     {
         return array_key_exists($name, $this->defaultSections);
     }
 
     /**
-     * @param $name
-     * @param $content
-     * @param array $options
+     * @param string $name
+     * @param mixed  $content
+     * @param array  $options
      *
      * @return string
      */
-    protected function resolveContent($name, &$content, array &$options)
+    protected function resolveContent(string $name, &$content, array &$options)
     {
         if (is_string($content)) {
             return $content;

+ 17 - 13
src/Middleware/Authenticate.php

@@ -19,26 +19,30 @@ class Authenticate
      */
     public function handle($request, Closure $next)
     {
-        if (Admin::guard()->guest() && ! $this->shouldPassThrough($request)) {
-            $loginPage = admin_base_path('auth/login');
+        if (
+            ! config('admin.auth.enable', true)
+            || ! Admin::guard()->guest()
+            || $this->shouldPassThrough($request)
+        ) {
+            return $next($request);
+        }
 
-            if ($request->ajax() && ! $request->pjax()) {
-                return response()->json(['message' => 'Unauthorized.', 'login' => $loginPage], 401);
-            }
+        $loginPage = admin_base_path('auth/login');
 
-            $response = redirect()->guest($loginPage);
+        if ($request->ajax() && ! $request->pjax()) {
+            return response()->json(['message' => 'Unauthorized.', 'login' => $loginPage], 401);
+        }
 
-            if ($request->pjax()) {
-                $response->headers->remove('Location');
-                $response->setStatusCode(200);
+        $response = redirect()->guest($loginPage);
 
-                return $response->setContent("<script>location.href = '$loginPage';</script>");
-            }
+        if ($request->pjax()) {
+            $response->headers->remove('Location');
+            $response->setStatusCode(200);
 
-            return $response;
+            return $response->setContent("<script>location.href = '$loginPage';</script>");
         }
 
-        return $next($request);
+        return $response;
     }
 
     /**

+ 2 - 2
src/Repositories/Repository.php

@@ -193,7 +193,7 @@ abstract class Repository implements \Dcat\Admin\Contracts\Repository
      */
     public static function listen($repositories, $listeners)
     {
-        $storage = app('admin.temp');
+        $storage = app('admin.context');
 
         $array = $storage->get('repository.listeners') ?: [];
 
@@ -223,7 +223,7 @@ abstract class Repository implements \Dcat\Admin\Contracts\Repository
 
         $any = $repository !== '*' ? static::getListeners('*') : [];
 
-        $storage = app('admin.temp');
+        $storage = app('admin.context');
 
         $listeners = $storage->get('repository.listeners') ?: [];
         $resolves = $storage->get('repository.listeners.resolves') ?: [];

+ 28 - 27
src/Support/helpers.php

@@ -1,5 +1,6 @@
 <?php
 
+use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\MessageBag;
@@ -8,15 +9,15 @@ if (! function_exists('admin_section')) {
     /**
      * Get the string contents of a section.
      *
-     * @param $section
-     * @param null  $default
-     * @param array $options
+     * @param string $section
+     * @param mixed  $default
+     * @param array  $options
      *
      * @return mixed
      */
-    function admin_section($section, $default = null, array $options = [])
+    function admin_section(string $section, $default = null, array $options = [])
     {
-        return app('sectionManager')->yieldContent($section, $default, $options);
+        return app('admin.sections')->yieldContent($section, $default, $options);
     }
 }
 
@@ -24,13 +25,13 @@ if (! function_exists('admin_has_section')) {
     /**
      * Check if section exists.
      *
-     * @param $section
+     * @param string $section
      *
      * @return mixed
      */
-    function admin_has_section($section)
+    function admin_has_section(string $section)
     {
-        return app('sectionManager')->hasSection($section);
+        return app('admin.sections')->hasSection($section);
     }
 }
 
@@ -38,14 +39,14 @@ if (! function_exists('admin_inject_section')) {
     /**
      * Injecting content into a section.
      *
-     * @param $section
-     * @param null $content
-     * @param bool $append
-     * @param int  $priority
+     * @param string $section
+     * @param mixed  $content
+     * @param bool   $append
+     * @param int    $priority
      */
-    function admin_inject_section($section, $content = null, bool $append = true, int $priority = 10)
+    function admin_inject_section(string $section, $content = null, bool $append = true, int $priority = 10)
     {
-        app('sectionManager')->inject($section, $content, $append, $priority);
+        app('admin.sections')->inject($section, $content, $append, $priority);
     }
 }
 
@@ -53,16 +54,16 @@ if (! function_exists('admin_inject_section_if')) {
     /**
      * Injecting content into a section.
      *
-     * @param $condition
-     * @param $section
-     * @param null $content
-     * @param bool $append
-     * @param int  $priority
+     * @param mixed  $condition
+     * @param string $section
+     * @param mixed  $content
+     * @param bool   $append
+     * @param int    $priority
      */
     function admin_inject_section_if($condition, $section, $content = null, bool $append = false, int $priority = 10)
     {
         if ($condition) {
-            app('sectionManager')->inject($section, $content, $append, $priority);
+            app('admin.sections')->inject($section, $content, $append, $priority);
         }
     }
 }
@@ -71,13 +72,13 @@ if (! function_exists('admin_has_default_section')) {
     /**
      * Check if default section exists.
      *
-     * @param $section
+     * @param string $section
      *
      * @return mixed
      */
-    function admin_has_default_section($section)
+    function admin_has_default_section(string $section)
     {
-        return app('sectionManager')->hasDefaultSection($section);
+        return app('admin.sections')->hasDefaultSection($section);
     }
 }
 
@@ -85,12 +86,12 @@ if (! function_exists('admin_inject_default_section')) {
     /**
      * Injecting content into a section.
      *
-     * @param $section
+     * @param string                              $section
      * @param string|Renderable|Htmlable|callable $content
      */
-    function admin_inject_default_section($section, $content)
+    function admin_inject_default_section(string $section, $content)
     {
-        app('sectionManager')->injectDefault($section, $content);
+        app('admin.sections')->injectDefault($section, $content);
     }
 }
 
@@ -200,7 +201,7 @@ if (! function_exists('admin_controller_slug')) {
 
         $controller = admin_controller_name();
 
-        return $slug[$controller] ?? ($slug[$controller] = \Dcat\Admin\Support\Helper::slug($controller));
+        return $slug[$controller] ?? ($slug[$controller] = Helper::slug($controller));
     }
 }
 

+ 2 - 2
src/Traits/HasBuilderEvents.php

@@ -48,7 +48,7 @@ trait HasBuilderEvents
      */
     protected function callBuilderListeners($key, ...$params)
     {
-        $storage = app('admin.temp');
+        $storage = app('admin.context');
 
         $key = static::formatBuilderEventKey($key);
 
@@ -74,7 +74,7 @@ trait HasBuilderEvents
      */
     protected static function setListeners($key, $callback, $once)
     {
-        $storage = app('admin.temp');
+        $storage = app('admin.context');
 
         $key = static::formatBuilderEventKey($key);