import { Transition } from '@headlessui/react'; import { useState, useEffect, useCallback } from 'react'; import RulesSummary from '../firewall/RulesSummary'; import StatusBanner from '../firewall/StatusBanner'; import ActivityPanel from '../firewall/ActivityPanel'; import { weSErrorMessage, weSIsRecord, weSSentryPost } from '../../data/ajax'; interface FirewallArgs { registrationEmail?: string; registrationName?: string; testMode?: boolean; onStep: Function; logged: boolean; /** The "Manage in cloud" handoff — the connect hook's cloud open */ onManageInCloud: () => void; } /** `route=get_firewall` — the cached copy of the cloud's rules */ interface FirewallPayload { countries: string[]; languages: string[]; logged_countries: string[]; logged_languages: string[]; mode: string; level: number | string; allowed_ips_count: number | string; blocked_count: number | string; remove_data_on_uninstall: boolean; } type FirewallResponse = { code: number; payload: FirewallPayload }; /** `route=update_option` — `applied` is the value as now stored */ type UpdateOptionResponse = { code?: number; applied?: unknown }; export default function Firewall( { onManageInCloud }: FirewallArgs ) { const [ loaded, setLoaded ] = useState( false ); /** What the last failed request said; '' when the last one succeeded */ const [ error, setError ] = useState( '' ); // allow list const [ countries, setCountries ] = useState< string[] >( [] ); // allow list const [ languages, setLanguages ] = useState< string[] >( [] ); // firewall mode: 'off' | 'monitor' | 'protect' const [ mode, setMode ] = useState( 'monitor' ); // protection level // 0, 1, 2 const [ protectLevel, setProtectLevel ] = useState( 0 ); // sizes of the IP lists, for the current-rules summary sentence const [ allowedCount, setAllowedCount ] = useState( 0 ); const [ blockedCount, setBlockedCount ] = useState( 0 ); // whether uninstalling the plugin also removes its data const [ removeData, setRemoveData ] = useState( false ); /** * Set the firewall mode: 'off' | 'monitor' | 'protect'. * * Returns the write so the banner can drop its optimistic value once it * settles either way — a failed write used to leave it spinning * (Redmine #25049). * @param value */ function setFirewallMode( value: string ): Promise< void > { // If the same one is clicked then ignore it if ( value === mode ) { return Promise.resolve(); } setError( '' ); return weSSentryPost< UpdateOptionResponse >( 'update_option', { option: 'we360cloud_sentry_firewall_protection', value, } ).then( ( json ) => { if ( typeof json.applied === 'string' ) { setMode( json.applied ); } }, ( reason ) => setError( weSErrorMessage( reason ) ) ); } function setRemoveDataOnUninstall( value: boolean ) { setError( '' ); weSSentryPost< UpdateOptionResponse >( 'update_option', { option: 'we360cloud_sentry_remove_data_on_uninstall', value: value ? 'true' : 'false', } ).then( ( json ) => { if ( typeof json.applied === 'boolean' ) { setRemoveData( json.applied ); } }, ( reason ) => setError( weSErrorMessage( reason ) ) ); } /** * Add a country or a language to the rules in effect. * * The whole list is sent, as the option write has always taken it, but the * plugin only ever adds: the server refuses a list that drops an entry, * because removing one is done in the cloud. * * Returns the write so a caller updating both lists at once can wait on * them; callers that ignore the return value are unaffected. The promise * settles (never rejects) so a `finally` on it is enough. * @param newCountries * @param isLang */ function updateCountries( newCountries: string[], isLang: boolean = false ): Promise< void > { if ( ! loaded ) { return Promise.resolve(); } setError( '' ); return weSSentryPost< UpdateOptionResponse >( 'update_option', { option: isLang ? 'we360cloud_sentry_allowed_languages' : 'we360cloud_sentry_allowed_countries', value: JSON.stringify( newCountries ), } ).then( ( json ) => { if ( undefined !== json.applied ) { if ( isLang ) { setLanguages( newCountries ); } else { setCountries( newCountries ); } } }, ( reason ) => setError( weSErrorMessage( reason ) ) ); } const init = useCallback( () => { if ( false === loaded ) { weSSentryPost< FirewallResponse >( 'get_firewall', {}, ( json ) => weSIsRecord( json.payload ) ).then( ( json ) => { const payload = json.payload; if ( Array.isArray( payload.countries ) ) { setCountries( payload.countries ); } if ( Array.isArray( payload.languages ) ) { setLanguages( payload.languages ); } if ( [ 'off', 'monitor', 'protect' ].includes( payload.mode ) ) { setMode( payload.mode ); } setAllowedCount( parseInt( String( payload.allowed_ips_count ), 10 ) || 0 ); setBlockedCount( parseInt( String( payload.blocked_count ), 10 ) || 0 ); setRemoveData( true === payload.remove_data_on_uninstall ); const level = parseInt( String( payload.level ), 10 ); setProtectLevel( Number.isNaN( level ) ? 0 : level ); setLoaded( true ); }, ( reason ) => { // The cached rules keep enforcing; the tab says why it // could not read them rather than sit on its defaults. setError( weSErrorMessage( reason ) ); setLoaded( true ); } ); } }, [ loaded ] ); useEffect( () => { // Anything in here is fired on component mount. init(); return () => { // Anything in here is fired on component unmount. }; }, [ init ] ); return (
{ error }
) }