-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db4002f
commit 10a7494
Showing
10 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
client/src/components/SudoModePasswordField/SudoModePasswordField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import classNames from 'classnames'; | ||
import Button from 'components/Button/Button'; | ||
import i18n from 'i18n'; | ||
import Config from 'lib/Config'; | ||
import backend from 'lib/Backend'; | ||
import PropTypes from 'prop-types'; | ||
import qs from 'qs'; | ||
import React, { createRef, useState } from 'react'; | ||
|
||
function SudoModePasswordField(props) { | ||
const { | ||
className, | ||
panelPadded, | ||
} = props; | ||
|
||
const passwordFieldRef = createRef(); | ||
const [responseMessage, setResponseMessage] = useState(''); | ||
|
||
function reloadPage() { | ||
// Add a ?reload=1 query parameter to the URL to force the browser to reload the page | ||
// window.location.reload() does not work as expected | ||
const query = qs.parse(window.location.search, { ignoreQueryPrefix: true }); | ||
const reload = query.reload ? parseInt(query.reload, 10) + 1 : 1; | ||
const hrefNoQuery = window.location.href.split('?')[0]; | ||
window.location.href = hrefNoQuery + qs.stringify({ ...query, reload }, { addQueryPrefix: true }); | ||
} | ||
|
||
async function handleClick() { | ||
const fetcher = backend.createEndpointFetcher({ | ||
url: Config.getSection('SilverStripe\\Admin\\SudoModeController').endpoints.activate, | ||
method: 'post', | ||
payloadFormat: 'urlencoded', | ||
responseFormat: 'json', | ||
}); | ||
const data = { | ||
Password: passwordFieldRef.current.value, | ||
}; | ||
const headers = { | ||
'X-SecurityID': Config.get('SecurityID'), | ||
}; | ||
const responseJson = await fetcher(data, headers); | ||
if (responseJson.result) { | ||
reloadPage(); | ||
} else { | ||
setResponseMessage(responseJson.message); | ||
} | ||
} | ||
|
||
function handleKeyDown(evt) { | ||
if (evt.key === 'Enter') { | ||
handleClick(); | ||
} | ||
} | ||
|
||
const text = i18n._t( | ||
'Admin.SUDO_MODE_PASSWORD_FIELD_TEXT', | ||
'This section is protected and is currently in read only mode. Please enter your password to make changes.' | ||
); | ||
const placeholder = i18n._t( | ||
'Admin.SUDO_MODE_PASSWORD_FIELD_PLACEHOLDER', | ||
'Your password' | ||
); | ||
const submit = i18n._t( | ||
'Admin.SUDO_MODE_PASSWORD_FIELD_SUBMIT', | ||
'Submit' | ||
); | ||
|
||
const classes = [ | ||
'sudo-mode-password-field', | ||
className, | ||
'flexbox-area-grow', | ||
'panel', | ||
]; | ||
if (panelPadded) { | ||
classes.push('panel--padded'); | ||
} | ||
return <div className={classNames(classes)}> | ||
<div className="alert alert-info"> | ||
<p>{text}</p> | ||
<div className="sudo-mode-password-field__container"> | ||
<div className="sudo-mode-password-field__input-container"> | ||
<input | ||
className="form-control no-change-track" | ||
type="password" | ||
ref={passwordFieldRef} | ||
placeholder={placeholder} | ||
onKeyDown={(evt) => handleKeyDown(evt)} | ||
/> | ||
</div> | ||
<div> | ||
<Button | ||
className="form-control" | ||
color="info" | ||
onClick={() => handleClick()} | ||
> | ||
{submit} | ||
</Button> | ||
</div> | ||
</div> | ||
{{ responseMessage } && | ||
<p className="sudo-mode-password-field__message">{responseMessage}</p> | ||
} | ||
</div> | ||
</div>; | ||
} | ||
|
||
SudoModePasswordField.propTypes = { | ||
className: PropTypes.string, | ||
panelPadded: PropTypes.bool, | ||
}; | ||
|
||
export default SudoModePasswordField; |
20 changes: 20 additions & 0 deletions
20
client/src/components/SudoModePasswordField/SudoModePasswordField.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.sudo-mode-password-field { | ||
|
||
// The react component has the `panel` class on it | ||
// this is to stop it from collapsing if the `panel--padded` class is not also present | ||
padding: 0.01px; | ||
|
||
&__container { | ||
display: flex; | ||
} | ||
|
||
&__input-container { | ||
padding-right: 10px; | ||
width: 100%; | ||
max-width: 350px; | ||
} | ||
|
||
&__message { | ||
margin-top: 10px; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
client/src/legacy/SudoModePasswordField/SudoModePasswordFieldEntwine.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* global window */ | ||
import jQuery from 'jquery'; | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { loadComponent } from 'lib/Injector'; | ||
|
||
jQuery.entwine('ss', ($) => { | ||
$('.js-injector-boot .SudoModePasswordField').entwine({ | ||
Component: null, | ||
ReactRoot: null, | ||
|
||
onmatch() { | ||
// onmatch will match both the field holder and the field | ||
// we only want to run this on the field holder | ||
if (this.is('.sudo-mode-password-field__input')) { | ||
return; | ||
} | ||
this._super(); | ||
const cmsContent = this.closest('.cms-content').attr('id'); | ||
const context = (cmsContent) | ||
? { context: cmsContent } | ||
: {}; | ||
const SudoModePasswordField = loadComponent('SudoModePasswordField', context); | ||
this.setComponent(SudoModePasswordField); | ||
this.refresh(); | ||
}, | ||
|
||
onunmatch() { | ||
this._super(); | ||
const root = this.getReactRoot(); | ||
if (root) { | ||
root.unmount(); | ||
this.setReactRoot(null); | ||
} | ||
}, | ||
|
||
refresh() { | ||
const props = this.getProps(); | ||
const SudoModePasswordField = this.getComponent(); | ||
let root = this.getReactRoot(); | ||
if (!root) { | ||
root = createRoot(this[0]); | ||
} | ||
root.render(<SudoModePasswordField {...props} />); | ||
this.setReactRoot(root); | ||
}, | ||
|
||
getProps() { | ||
const inputField = this.getInputField(); | ||
return { | ||
panelPadded: inputField.is('[data-panel-padded]'), | ||
}; | ||
}, | ||
|
||
getInputField() { | ||
return this.find('.sudo-mode-password-field__input'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters