From d25e40ded4e7da644b99cf9e5f0fc4b655fc0174 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Tue, 11 Oct 2022 13:27:04 -0400 Subject: [PATCH] Add settings code --- README.md | 4 +- includes/classes/SSO/SSO.php | 124 +++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 23610e0..a51013b 100644 --- a/README.md +++ b/README.md @@ -95,9 +95,9 @@ There are 2 filters available here: ### SSO -10up Experience includes 10up SSO functionality. There are some useful constants related to this functionality: +10up Experience includes 10up SSO functionality. This feature can be enabled or disabled in `Settings > General`. It is enabled by default. There are some useful constants related to this functionality: -- `TENUPSSO_DISABLE` - Define this as `true` to disable SSO. +- `TENUPSSO_DISABLE` - Define this as `true` to force disable SSO. - `TENUPSSO_DISALLOW_ALL_DIRECT_LOGIN` - Define this as `true` to disable username/password log ins completely. ### Activity Log diff --git a/includes/classes/SSO/SSO.php b/includes/classes/SSO/SSO.php index d5ae07d..57af4f7 100644 --- a/includes/classes/SSO/SSO.php +++ b/includes/classes/SSO/SSO.php @@ -31,6 +31,17 @@ public function setup() { return; } + if ( TENUP_EXPERIENCE_IS_NETWORK ) { + add_action( 'wpmu_options', [ $this, 'ms_settings' ] ); + add_action( 'admin_init', [ $this, 'ms_save_settings' ] ); + } else { + add_action( 'admin_init', [ $this, 'single_site_setting' ] ); + } + + if ( 'yes' !== $this->get_setting() ) { + return; + } + if ( defined( 'TENUPSSO_DISALLOW_ALL_DIRECT_LOGIN' ) && TENUPSSO_DISALLOW_ALL_DIRECT_LOGIN ) { add_filter( 'allow_password_reset', '__return_false' ); } @@ -43,6 +54,119 @@ public function setup() { add_action( 'admin_page_access_denied', [ $this, 'check_user_blog' ] ); } + /** + * Set options in multisite + */ + public function ms_save_settings() { + global $pagenow; + if ( ! is_network_admin() ) { + return; + } + + if ( 'settings.php' !== $pagenow ) { + return; + } + + if ( ! is_super_admin() ) { + return; + } + + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'siteoptions' ) ) { + return; + } + + if ( ! isset( $_POST['tenup_allow_sso'] ) ) { + return; + } + + $setting = $this->validate_sso_setting( $_POST['tenup_allow_sso'] ); + + update_site_option( 'tenup_allow_sso', $setting ); + } + + /** + * Output multisite settings + */ + public function ms_settings() { + $setting = $this->get_setting(); + ?> +

+

+ + + + + + + + + 'string', + 'sanitize_callback' => [ $this, 'validate_sso_setting' ], + ); + + register_setting( 'general', 'tenup_allow_sso', $settings_args ); + add_settings_field( 'tenup_allow_sso', esc_html__( 'Allow 10up SSO', 'tenup' ), [ $this, 'sso_setting_field_output' ], 'general' ); + } + + /** + * Validate sso setting. + * + * @param string $value Current restriction. + * @return string + */ + public function validate_sso_setting( $value ) { + if ( in_array( $value, array( 'yes', 'no' ), true ) ) { + return $value; + } + + return 'yes'; + } + + /** + * Display UI for restrict REST API setting. + * + * @return void + */ + public function sso_setting_field_output() { + $allow_sso = $this->get_setting(); + ?> + + /> +
+ + /> + +

+