2022-07-11 03:09:09 -05:00
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
2023-01-02 00:23:55 -06:00
// Copyright (C) 2013 - 2023, The pgAdmin Development Team
2022-07-11 03:09:09 -05:00
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
2022-07-04 01:46:23 -05:00
import React , { useState , useRef , useEffect } from 'react' ;
import PropTypes from 'prop-types' ;
import gettext from 'sources/gettext' ;
import url _for from 'sources/url_for' ;
import { Box } from '@material-ui/core' ;
import CloseIcon from '@material-ui/icons/CloseRounded' ;
import DeleteForeverIcon from '@material-ui/icons/DeleteForever' ;
import CheckRoundedIcon from '@material-ui/icons/CheckRounded' ;
import HelpIcon from '@material-ui/icons/Help' ;
2022-08-05 03:35:53 -05:00
import { DefaultButton , PrimaryButton , PgIconButton } from '../components/Buttons' ;
import { useModalStyles } from '../helpers/ModalProvider' ;
import { FormFooterMessage , InputText , MESSAGE _TYPE } from '../components/FormComponents' ;
2022-07-04 01:46:23 -05:00
2023-05-22 00:38:29 -05:00
export default function MasterPasswordContent ( { closeModal , onResetPassowrd , onOK , onCancel , setHeight , isPWDPresent , data , isKeyring } ) {
2022-07-04 01:46:23 -05:00
const classes = useModalStyles ( ) ;
const containerRef = useRef ( ) ;
const firstEleRef = useRef ( ) ;
const okBtnRef = useRef ( ) ;
const [ formData , setFormData ] = useState ( {
password : ''
} ) ;
const onTextChange = ( e , id ) => {
let val = e ;
if ( e && e . target ) {
val = e . target . value ;
}
setFormData ( ( prev ) => ( { ... prev , [ id ] : val } ) ) ;
} ;
const onKeyDown = ( e ) => {
// If enter key is pressed then click on OK button
if ( e . key === 'Enter' ) {
okBtnRef . current ? . click ( ) ;
}
} ;
useEffect ( ( ) => {
setTimeout ( ( ) => {
firstEleRef . current && firstEleRef . current . focus ( ) ;
2022-08-22 05:28:17 -05:00
} , 350 ) ;
} , [ firstEleRef . current ] ) ;
2022-07-04 01:46:23 -05:00
useEffect ( ( ) => {
setHeight ? . ( containerRef . current ? . offsetHeight ) ;
} , [ containerRef . current ] ) ;
return (
< Box display = "flex" flexDirection = "column" className = { classes . container } ref = { containerRef } >
2023-05-22 00:38:29 -05:00
{ isKeyring ?
< Box flexGrow = "1" p = { 2 } >
< Box >
< span style = { { fontWeight : 'bold' } } >
{ gettext ( 'Please enter your master password.' ) }
< / span >
< br / >
< span style = { { fontWeight : 'bold' } } >
{ gettext ( 'This is required to migrate the existing saved Server password and SSH tunnel password to OS password manager, as pgAdmin 4 will now use the OS password manager in Desktop mode from version 7.2' ) }
< / span >
< / Box >
< Box marginTop = '12px' >
< InputText inputRef = { firstEleRef } type = "password" value = { formData [ 'password' ] } maxLength = { null }
onChange = { ( e ) => onTextChange ( e , 'password' ) } onKeyDown = { ( e ) => onKeyDown ( e ) } / >
< / Box >
< FormFooterMessage type = { MESSAGE _TYPE . ERROR } message = { data . errmsg } closable = { false } style = { {
position : 'unset' , padding : '12px 0px 0px'
} } / >
< / Box > :
< Box flexGrow = "1" p = { 2 } >
< Box >
< span style = { { fontWeight : 'bold' } } >
{ isPWDPresent ? gettext ( 'Please enter your master password.' ) : gettext ( 'Please set a master password for pgAdmin.' ) }
< / span >
< br / >
< span style = { { fontWeight : 'bold' } } >
{ isPWDPresent ? gettext ( 'This is required to unlock saved passwords and reconnect to the database server(s).' ) : gettext ( 'This will be used to secure and later unlock saved passwords and other credentials.' ) }
< / span >
< / Box >
< Box marginTop = '12px' >
< InputText inputRef = { firstEleRef } type = "password" value = { formData [ 'password' ] } maxLength = { null }
onChange = { ( e ) => onTextChange ( e , 'password' ) } onKeyDown = { ( e ) => onKeyDown ( e ) } / >
< / Box >
< FormFooterMessage type = { MESSAGE _TYPE . ERROR } message = { data . errmsg } closable = { false } style = { {
position : 'unset' , padding : '12px 0px 0px'
} } / >
2022-07-04 01:46:23 -05:00
< / Box >
2023-05-22 00:38:29 -05:00
}
2022-07-04 01:46:23 -05:00
< Box className = { classes . footer } >
< Box style = { { marginRight : 'auto' } } >
< PgIconButton data - test = "help-masterpassword" title = { gettext ( 'Help' ) } style = { { padding : '0.3rem' , paddingLeft : '0.7rem' } } startIcon = { < HelpIcon / > } onClick = { ( ) => {
let _url = url _for ( 'help.static' , {
'filename' : 'master_password.html' ,
} ) ;
window . open ( _url , 'pgadmin_help' ) ;
} } >
< / PgIconButton >
2023-05-22 00:38:29 -05:00
{ isPWDPresent && ! isKeyring &&
2022-08-10 04:35:20 -05:00
< DefaultButton data - test = "reset-masterpassword" style = { { marginLeft : '0.5rem' } } startIcon = { < DeleteForeverIcon / > }
2022-07-04 01:46:23 -05:00
onClick = { ( ) => { onResetPassowrd ? . ( ) ; } } >
{ gettext ( 'Reset Master Password' ) }
< / DefaultButton >
}
< / Box >
2023-05-22 00:38:29 -05:00
{
! isKeyring && < DefaultButton data - test = "close" startIcon = { < CloseIcon / > } onClick = { ( ) => {
onCancel ? . ( ) ;
closeModal ( ) ;
} } > { gettext ( 'Cancel' ) } < / DefaultButton >
}
2022-07-04 01:46:23 -05:00
< PrimaryButton ref = { okBtnRef } data - test = "save" className = { classes . margin } startIcon = { < CheckRoundedIcon / > }
disabled = { formData . password . length == 0 }
onClick = { ( ) => {
let postFormData = new FormData ( ) ;
postFormData . append ( 'password' , formData . password ) ;
postFormData . append ( 'submit_password' , true ) ;
onOK ? . ( postFormData ) ;
closeModal ( ) ;
} }
>
{ gettext ( 'OK' ) }
< / PrimaryButton >
< / Box >
< / Box > ) ;
}
MasterPasswordContent . propTypes = {
closeModal : PropTypes . func ,
onResetPassowrd : PropTypes . func ,
onOK : PropTypes . func ,
onCancel : PropTypes . func ,
setHeight : PropTypes . func ,
isPWDPresent : PropTypes . bool ,
data : PropTypes . object ,
2023-05-22 00:38:29 -05:00
isKeyring : PropTypes . bool ,
2022-07-04 01:46:23 -05:00
} ;