From 9b8974b00346b08e69ab5cad791f32752a1a1fc2 Mon Sep 17 00:00:00 2001 From: Samuell Date: Thu, 15 Aug 2019 05:47:13 +0200 Subject: [PATCH] Add validator for plugin navigation items (#4497) This will detect invalid navigation item configuration in installed plugins. In debug mode, this will throw an error, otherwise, it will simply log the error. Credit to @Samuell1. Fixes #4491. --- modules/backend/classes/NavigationManager.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/modules/backend/classes/NavigationManager.php b/modules/backend/classes/NavigationManager.php index 1376a7891..0566922e0 100644 --- a/modules/backend/classes/NavigationManager.php +++ b/modules/backend/classes/NavigationManager.php @@ -3,6 +3,10 @@ use Event; use BackendAuth; use System\Classes\PluginManager; +use Validator; +use SystemException; +use Log; +use Config; /** * Manages the backend navigation. @@ -195,6 +199,24 @@ class NavigationManager $this->items = []; } + $validator = Validator::make($definitions, [ + '*.label' => 'required', + '*.icon' => 'required_without:*.iconSvg', + '*.url' => 'required', + '*.sideMenu.*.label' => 'nullable|required', + '*.sideMenu.*.icon' => 'nullable|required_without:*.sideMenu.*.iconSvg', + '*.sideMenu.*.url' => 'nullable|required', + ]); + + if ($validator->fails()) { + $errorMessage = 'Invalid menu item detected in ' . $owner . '. Contact the plugin author to fix (' . $validator->errors()->first() . ')'; + if (Config::get('app.debug', false)) { + throw new SystemException($errorMessage); + } else { + Log::error($errorMessage); + } + } + $this->addMainMenuItems($owner, $definitions); }