From 2a9117dcdf06f103d7880dc1724a2cb7dd803142 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 3 Feb 2020 16:31:49 -0800 Subject: [PATCH] Add settings validation to import settings --- lib/WP_Auth0_Import_Settings.php | 15 ++++++- lib/admin/WP_Auth0_Admin.php | 30 +++++++++----- templates/import_settings.php | 6 ++- templates/settings.php | 12 ++++++ tests/testAdminAppearanceValidation.php | 9 ----- tests/testAdminBasicValidation.php | 9 ----- tests/testImportExportSettings.php | 53 +++++++++++++++++++++++++ tests/testOptionLockCdn.php | 25 +++++++----- tests/testOptionMigrationIps.php | 24 ++++++----- tests/testOptionMigrationWs.php | 27 ++++++++----- tests/testOptionSlo.php | 21 +++++----- tests/testOptionWle.php | 22 +++++----- tests/testRequiredEmail.php | 25 +++++++----- 13 files changed, 184 insertions(+), 94 deletions(-) diff --git a/lib/WP_Auth0_Import_Settings.php b/lib/WP_Auth0_Import_Settings.php index 32959510..e679cc4e 100644 --- a/lib/WP_Auth0_Import_Settings.php +++ b/lib/WP_Auth0_Import_Settings.php @@ -31,8 +31,19 @@ public function import_settings() { exit; } - foreach ( $settings as $key => $value ) { - $this->a0_options->set( $key, $value, false ); + // Keep original settings keys so we only save imported values. + $settings_keys = array_keys( $settings ); + + $admin = new WP_Auth0_Admin( $this->a0_options, new WP_Auth0_Routes( $this->a0_options ) ); + + // Default setting values will be added to the array. + $settings_validated = $admin->input_validator( $settings ); + + foreach ( $settings_keys as $settings_key ) { + // Invalid settings keys are removed in WP_Auth0_Admin::input_validator(). + if ( isset( $settings_validated[ $settings_key ] ) ) { + $this->a0_options->set( $settings_key, $settings_validated[ $settings_key ], false ); + } } $this->a0_options->update_all(); diff --git a/lib/admin/WP_Auth0_Admin.php b/lib/admin/WP_Auth0_Admin.php index a032fff0..b83fde21 100755 --- a/lib/admin/WP_Auth0_Admin.php +++ b/lib/admin/WP_Auth0_Admin.php @@ -6,11 +6,18 @@ class WP_Auth0_Admin { protected $router; - protected $sections = []; + protected $sections; public function __construct( WP_Auth0_Options $a0_options, WP_Auth0_Routes $router ) { $this->a0_options = $a0_options; $this->router = $router; + + $this->sections = [ + 'basic' => new WP_Auth0_Admin_Basic( $this->a0_options ), + 'features' => new WP_Auth0_Admin_Features( $this->a0_options ), + 'appearance' => new WP_Auth0_Admin_Appearance( $this->a0_options ), + 'advanced' => new WP_Auth0_Admin_Advanced( $this->a0_options, $this->router ), + ]; } /** @@ -58,17 +65,10 @@ public function admin_enqueue() { } public function init_admin() { - $this->sections['basic'] = new WP_Auth0_Admin_Basic( $this->a0_options ); - $this->sections['basic']->init(); - - $this->sections['features'] = new WP_Auth0_Admin_Features( $this->a0_options ); - $this->sections['features']->init(); - $this->sections['appearance'] = new WP_Auth0_Admin_Appearance( $this->a0_options ); - $this->sections['appearance']->init(); - - $this->sections['advanced'] = new WP_Auth0_Admin_Advanced( $this->a0_options, $this->router ); - $this->sections['advanced']->init(); + foreach ( $this->sections as $name => $section ) { + $section->init(); + } register_setting( $this->a0_options->get_options_name() . '_basic', @@ -95,6 +95,14 @@ public function input_validator( array $input ) { $input[ $key ] = $this->a0_options->get_constant_val( $key ); } + // Remove unknown keys. + $option_keys = $this->a0_options->get_defaults( true ); + foreach ( $input as $key => $val ) { + if ( ! in_array( $key, $option_keys ) ) { + unset( $input[ $key ] ); + } + } + foreach ( $this->sections as $name => $section ) { $input = $section->input_validator( $input ); } diff --git a/templates/import_settings.php b/templates/import_settings.php index 413ab167..06354073 100644 --- a/templates/import_settings.php +++ b/templates/import_settings.php @@ -28,7 +28,11 @@
-

+

diff --git a/templates/settings.php b/templates/settings.php index d9f52e95..40274255 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -6,6 +6,18 @@ + + + + + +