import * as React from 'react'; import {withRouter} from 'react-router'; import {WithRouterProps} from 'react-router/lib/withRouter'; import styled from '@emotion/styled'; import {ModalRenderProps} from 'app/actionCreators/modal'; import {Client} from 'app/api'; import Alert from 'app/components/alert'; import Button from 'app/components/button'; import U2fContainer from 'app/components/u2f/u2fContainer'; import {IconFlag} from 'app/icons'; import {t} from 'app/locale'; import ConfigStore from 'app/stores/configStore'; import space from 'app/styles/space'; import withApi from 'app/utils/withApi'; import Form from 'app/views/settings/components/forms/form'; import InputField from 'app/views/settings/components/forms/inputField'; import TextBlock from 'app/views/settings/components/text/textBlock'; type OnTapProps = NonNullable['onTap']>; type Props = WithRouterProps & Pick & { api: Client; closeModal: () => void; /** * User is a superuser without an active su session */ superuser?: boolean; /** * expects a function that returns a Promise */ retryRequest?: () => Promise; }; type State = { error: boolean; busy: boolean; }; class SudoModal extends React.Component { state: State = { error: false, busy: false, }; handleSuccess = () => { const {closeModal, superuser, location, router, retryRequest} = this.props; if (!retryRequest) { closeModal(); return; } if (superuser) { router.replace({pathname: location.pathname, state: {forceUpdate: new Date()}}); return; } this.setState({busy: true}, () => { retryRequest().then(() => { this.setState({busy: false}, closeModal); }); }); }; handleError = () => { this.setState({busy: false, error: true}); }; handleU2fTap = async (data: Parameters[0]) => { this.setState({busy: true}); const {api} = this.props; try { await api.requestPromise('/auth/', {method: 'PUT', data}); this.handleSuccess(); } catch (err) { this.setState({busy: false}); // u2fInterface relies on this throw err; } }; renderBodyContent() { const {superuser} = this.props; const {error} = this.state; const user = ConfigStore.get('user'); if (!user.hasPasswordAuth) { return ( {t('You will need to reauthenticate to continue.')} ); } return ( {superuser ? t( 'You are attempting to access a resource that requires superuser access, please re-authenticate as a superuser.' ) : t('Help us keep your account safe by confirming your identity.')} {error && ( }> {t('Incorrect password')} )}
); } render() { const {Header, Body} = this.props; return (
{t('Confirm Password to Continue')}
{this.renderBodyContent()}
); } } export default withRouter(withApi(SudoModal)); export {SudoModal}; const StyledTextBlock = styled(TextBlock)` margin-bottom: ${space(1)}; `; const StyledInputField = styled(InputField)` padding-left: 0; `; const StyledAlert = styled(Alert)` margin-bottom: 0; `;