forked from silverstripe/silverstripe-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
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 cd2a595
Showing
10 changed files
with
196 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
108 changes: 108 additions & 0 deletions
108
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,108 @@ | ||
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 | ||
} = 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' | ||
); | ||
|
||
return <div className="panel panel--padded flexbox-area-grow"> | ||
<div | ||
className={classNames([ | ||
'sudo-mode-password-field', | ||
'alert', | ||
'alert-info', | ||
className, | ||
])} | ||
> | ||
<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, | ||
}; | ||
|
||
export default SudoModePasswordField; |
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
.sudo-mode-password-field { | ||
|
||
&__container { | ||
display: flex; | ||
} | ||
|
||
&__input-container { | ||
padding-right: 10px; | ||
width: 100%; | ||
max-width: 350px; | ||
} | ||
|
||
&__message { | ||
margin-top: 10px; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
/* global window */ | ||
import jQuery from 'jquery'; | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { schemaMerge } from 'lib/schemaFieldValues'; | ||
import { loadComponent } from 'lib/Injector'; | ||
|
||
jQuery.entwine('ss', ($) => { | ||
$('.js-injector-boot .SudoModePasswordField').entwine({ | ||
Component: null, | ||
ReactRoot: null, | ||
|
||
onmatch() { | ||
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.getAttributes(); | ||
const SudoModePasswordField = this.getComponent(); | ||
let root = this.getReactRoot(); | ||
if (!root) { | ||
root = createRoot(this[0]); | ||
} | ||
root.render(<SudoModePasswordField {...props} />); | ||
this.setReactRoot(root); | ||
}, | ||
|
||
getAttributes() { | ||
const state = this.data('state'); | ||
const schema = this.data('schema'); | ||
return schemaMerge(schema, state); | ||
}, | ||
}); | ||
}); |
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