From 123408e1338491b5561b5e588d663d4184fa226b Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Wed, 23 Oct 2024 13:51:19 -0300 Subject: [PATCH] feat(connections): jetpack sso (#3486) --- includes/wizards/class-connections-wizard.php | 1 + src/wizards/connections/views/main/index.js | 2 + .../connections/views/main/jetpack-sso.js | 149 ++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/wizards/connections/views/main/jetpack-sso.js diff --git a/includes/wizards/class-connections-wizard.php b/includes/wizards/class-connections-wizard.php index ef5ed8f0ef..c5cb4293e9 100644 --- a/includes/wizards/class-connections-wizard.php +++ b/includes/wizards/class-connections-wizard.php @@ -66,6 +66,7 @@ public function enqueue_scripts_and_styles() { 'can_connect_fivetran' => OAuth::is_proxy_configured( 'fivetran' ), 'can_use_webhooks' => defined( 'NEWSPACK_EXPERIMENTAL_WEBHOOKS' ) && NEWSPACK_EXPERIMENTAL_WEBHOOKS, 'can_use_everlit' => Everlit_Configuration_Manager::is_enabled(), + 'can_use_jetpack_sso' => class_exists( 'Jetpack' ) && defined( 'NEWSPACK_MANAGER_FILE' ), ] ); \wp_enqueue_script( 'newspack-connections-wizard' ); diff --git a/src/wizards/connections/views/main/index.js b/src/wizards/connections/views/main/index.js index f17e15b1b6..74c589a6c8 100644 --- a/src/wizards/connections/views/main/index.js +++ b/src/wizards/connections/views/main/index.js @@ -14,6 +14,7 @@ import Plugins from './plugins'; import GoogleAuth from './google'; import Mailchimp from './mailchimp'; import FivetranConnection from './fivetran'; +import JetpackSSO from './jetpack-sso'; import Recaptcha from './recaptcha'; import Webhooks from './webhooks'; @@ -39,6 +40,7 @@ const Main = () => { /> ) } + { newspack_connections_data.can_use_webhooks && } diff --git a/src/wizards/connections/views/main/jetpack-sso.js b/src/wizards/connections/views/main/jetpack-sso.js new file mode 100644 index 0000000000..b6c5a3198d --- /dev/null +++ b/src/wizards/connections/views/main/jetpack-sso.js @@ -0,0 +1,149 @@ +/* globals newspack_connections_data */ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { BaseControl, CheckboxControl } from '@wordpress/components'; +import { useEffect, useState } from '@wordpress/element'; +import apiFetch from '@wordpress/api-fetch'; + +/** + * Internal dependencies + */ +import { + ActionCard, + Button, + Grid, + Notice, + SectionHeader, + SelectControl, +} from '../../../../components/src'; + +const JetpackSSO = () => { + const [ error, setError ] = useState( null ); + const [ isLoading, setIsLoading ] = useState( false ); + const [ settings, setSettings ] = useState( {} ); + const [ settingsToUpdate, setSettingsToUpdate ] = useState( {} ); + + useEffect( () => { + const fetchSettings = async () => { + setIsLoading( true ); + try { + const fetchedSettings = await apiFetch( { path: '/newspack-manager/v1/jetpack-sso' } ); + setSettings( fetchedSettings ); + setSettingsToUpdate( fetchedSettings ); + } catch ( e ) { + setError( e.message || __( 'Error fetching settings.', 'newspack-plugin' ) ); + } finally { + setIsLoading( false ); + } + }; + fetchSettings(); + }, [] ); + + const updateSettings = async data => { + setError( null ); + setIsLoading( true ); + try { + const newSettings = await apiFetch( { + path: '/newspack-manager/v1/jetpack-sso', + method: 'POST', + data, + } ); + setSettings( newSettings ); + setSettingsToUpdate( newSettings ); + } catch ( e ) { + setError( e?.message || __( 'Error updating settings.', 'newspack-plugin' ) ); + } finally { + setIsLoading( false ); + } + }; + if ( ! newspack_connections_data.can_use_jetpack_sso ) { + return null; + } + return ( + <> + + ( + <> + { __( + 'Improve security by requiring two-factor authentication via WordPress.com for users with higher capabilities.', + 'newspack-plugin' + ) } + + ) } + hasGreyHeader={ !! settings.force_2fa } + toggleChecked={ !! settings.force_2fa } + toggleOnChange={ () => updateSettings( { force_2fa: ! settings.force_2fa } ) } + actionContent={ + settings.force_2fa && ( + + ) + } + disabled={ isLoading } + > + { settings.force_2fa && ( + <> + { error && } + { settings.jetpack_sso_force_2fa && ( + <> + +

+ { __( + 'Customize which capabilties to enforce 2FA by untoggling the “Require accounts to use WordPress.com Two-Step Authentication” option in Jetpack settings.', + 'newspack-plugin' + ) } +

+ + ) } + + + + setSettingsToUpdate( { ...settingsToUpdate, force_2fa_cap: value } ) + } + options={ + Object.keys( settings.available_caps || {} ).map( cap => ( { + label: settings.available_caps[ cap ], + value: cap, + } ) ) + } + /> + + + + setSettingsToUpdate( { ...settingsToUpdate, obfuscate_account: value } ) } + label={ __( 'Obfuscate restricted accounts by throwing WP’s “user not found” errors on login form attempts.', 'newspack-plugin' ) } + /> + + + ) } +
+ + ); +}; + +export default JetpackSSO;