forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add visual configurator for Parse Dashboard settings (parse-com…
- Loading branch information
Showing
5 changed files
with
297 additions
and
5 deletions.
There are no files selected for viewing
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
242 changes: 242 additions & 0 deletions
242
src/dashboard/Settings/DashboardSettings/DashboardSettings.react.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,242 @@ | ||
import DashboardView from 'dashboard/DashboardView.react'; | ||
import Field from 'components/Field/Field.react'; | ||
import Fieldset from 'components/Fieldset/Fieldset.react'; | ||
import FlowView from 'components/FlowView/FlowView.react'; | ||
import FormButton from 'components/FormButton/FormButton.react'; | ||
import Label from 'components/Label/Label.react'; | ||
import Button from 'components/Button/Button.react'; | ||
import React from 'react'; | ||
import styles from 'dashboard/Settings/DashboardSettings/DashboardSettings.scss'; | ||
import TextInput from 'components/TextInput/TextInput.react'; | ||
import Toggle from 'components/Toggle/Toggle.react'; | ||
import Icon from 'components/Icon/Icon.react'; | ||
import Dropdown from 'components/Dropdown/Dropdown.react'; | ||
import Option from 'components/Dropdown/Option.react'; | ||
import Toolbar from 'components/Toolbar/Toolbar.react'; | ||
import CodeSnippet from 'components/CodeSnippet/CodeSnippet.react'; | ||
import Notification from 'dashboard/Data/Browser/Notification.react'; | ||
import * as ColumnPreferences from 'lib/ColumnPreferences'; | ||
import bcrypt from 'bcryptjs'; | ||
import * as OTPAuth from 'otpauth'; | ||
import QRCode from 'qrcode'; | ||
|
||
export default class DashboardSettings extends DashboardView { | ||
constructor() { | ||
super(); | ||
this.section = 'App Settings'; | ||
this.subsection = 'Dashboard Configuration'; | ||
|
||
this.state = { | ||
createUserInput: false, | ||
username: '', | ||
password: '', | ||
encrypt: true, | ||
mfa: false, | ||
mfaDigits: 6, | ||
mfaPeriod: 30, | ||
mfaAlgorithm: 'SHA1', | ||
message: null, | ||
passwordInput: '', | ||
passwordHidden: true, | ||
columnData: { | ||
data: '', | ||
show: false, | ||
}, | ||
newUser: { | ||
data: '', | ||
show: false, | ||
mfa: '', | ||
}, | ||
}; | ||
} | ||
|
||
getColumns() { | ||
const data = ColumnPreferences.getAllPreferences(this.context.applicationId); | ||
this.setState({ | ||
columnData: { data: JSON.stringify(data, null, 2), show: true }, | ||
}); | ||
} | ||
|
||
copy(data, label) { | ||
navigator.clipboard.writeText(data); | ||
this.showNote(`${label} copied to clipboard`); | ||
} | ||
|
||
createUser() { | ||
if (!this.state.username) { | ||
this.showNote('Please enter a username'); | ||
return; | ||
} | ||
if (!this.state.password) { | ||
this.showNote('Please enter a password'); | ||
return; | ||
} | ||
|
||
let pass = this.state.password; | ||
if (this.state.encrypt) { | ||
const salt = bcrypt.genSaltSync(10); | ||
pass = bcrypt.hashSync(pass, salt); | ||
} | ||
|
||
const user = { | ||
username: this.state.username, | ||
pass, | ||
}; | ||
|
||
let mfa; | ||
if (this.state.mfa) { | ||
const secret = new OTPAuth.Secret(); | ||
const totp = new OTPAuth.TOTP({ | ||
issuer: this.context.name, | ||
label: user.username, | ||
algorithm: this.state.mfaAlgorithm || 'SHA1', | ||
digits: this.state.mfaDigits || 6, | ||
period: this.state.mfaPeriod || 30, | ||
secret, | ||
}); | ||
mfa = totp.toString(); | ||
user.mfa = secret.base32; | ||
if (totp.algorithm !== 'SHA1') { | ||
user.mfaAlgorithm = totp.algorithm; | ||
} | ||
if (totp.digits != 6) { | ||
user.mfaDigits = totp.digits; | ||
} | ||
if (totp.period != 30) { | ||
user.mfaPeriod = totp.period; | ||
} | ||
|
||
setTimeout(() => { | ||
const canvas = document.getElementById('canvas'); | ||
QRCode.toCanvas(canvas, mfa); | ||
}, 10); | ||
} | ||
|
||
this.setState({ | ||
newUser: { | ||
show: true, | ||
data: JSON.stringify(user, null, 2), | ||
mfa, | ||
}, | ||
}); | ||
} | ||
|
||
generatePassword() { | ||
let chars = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
let pwordLength = 20; | ||
let password = ''; | ||
|
||
const array = new Uint32Array(chars.length); | ||
window.crypto.getRandomValues(array); | ||
|
||
for (let i = 0; i < pwordLength; i++) { | ||
password += chars[array[i] % chars.length]; | ||
} | ||
this.setState({ password }); | ||
} | ||
|
||
showNote(message) { | ||
if (!message) { | ||
return; | ||
} | ||
|
||
clearTimeout(this.noteTimeout); | ||
|
||
this.setState({ message }); | ||
|
||
this.noteTimeout = setTimeout(() => { | ||
this.setState({ message: null }); | ||
}, 3500); | ||
} | ||
|
||
renderForm() { | ||
const createUserInput = ( | ||
<Fieldset legend="New User"> | ||
<Field label={<Label text="Username" />} input={<TextInput value={this.state.username} placeholder="Username" onChange={(username) => this.setState({ username })} />} /> | ||
<Field | ||
label={ | ||
<Label | ||
text={ | ||
<div className={styles.password}> | ||
<span>Password</span> | ||
<a onClick={() => this.setState({ passwordHidden: !this.state.passwordHidden })}> | ||
<Icon name={this.state.passwordHidden ? 'visibility' : 'visibility_off'} width={18} height={18} fill="rgba(0,0,0,0.4)" /> | ||
</a> | ||
</div> | ||
} | ||
description={<a onClick={() => this.generatePassword()}>Generate strong password</a>} | ||
/> | ||
} | ||
input={<TextInput hidden={this.state.passwordHidden} value={this.state.password} placeholder="Password" onChange={(password) => this.setState({ password })} />} | ||
/> | ||
<Field label={<Label text="Encrypt Password" />} input={<Toggle value={this.state.encrypt} type={Toggle.Types.YES_NO} onChange={(encrypt) => this.setState({ encrypt })} />} /> | ||
<Field label={<Label text="Enable MFA" />} input={<Toggle value={this.state.mfa} type={Toggle.Types.YES_NO} onChange={(mfa) => this.setState({ mfa })} />} /> | ||
{this.state.mfa && ( | ||
<Field | ||
label={<Label text="MFA Algorithm" />} | ||
input={ | ||
<Dropdown value={this.state.mfaAlgorithm} onChange={(mfaAlgorithm) => this.setState({ mfaAlgorithm })}> | ||
{['SHA1', 'SHA224', 'SHA256', 'SHA384', 'SHA512', 'SHA3-224', 'SHA3-256', 'SHA3-384', 'SHA3-512'].map((column) => ( | ||
<Option key={column} value={column}> | ||
{column} | ||
</Option> | ||
))} | ||
</Dropdown> | ||
} | ||
/> | ||
)} | ||
{this.state.mfa && <Field label={<Label text="MFA Digits" description="How many digits long should the MFA code be" />} input={<TextInput value={`${this.state.mfaDigits}`} placeholder="6" onChange={(mfaDigits) => this.setState({ mfaDigits })} />} />} | ||
{this.state.mfa && <Field label={<Label text="MFA Period" description="How many long should the MFA last for" />} input={<TextInput value={`${this.state.mfaPeriod}`} placeholder="30" onChange={(mfaPeriod) => this.setState({ mfaPeriod })} />} />} | ||
<Field input={<Button color="blue" value="Create" width="120px" onClick={() => this.createUser()} />} /> | ||
</Fieldset> | ||
); | ||
const columnPreferences = ( | ||
<div> | ||
<div className={styles.columnData}> | ||
<CodeSnippet source={this.state.columnData.data} language="json" /> | ||
</div> | ||
<div className={styles.footer}> | ||
<Button color="blue" value="Copy" width="120px" onClick={() => this.copy(this.state.columnData.data, 'Column Preferences')} /> | ||
<Button primary={true} value="Done" width="120px" onClick={() => this.setState({ columnData: { data: '', show: false } })} /> | ||
</div> | ||
</div> | ||
); | ||
const userData = ( | ||
<div className={styles.userData}> | ||
Add the following data to your Parse Dashboard configuration "users": | ||
{this.state.encrypt && <div>Make sure the dashboard option useEncryptedPasswords is set to true.</div>} | ||
<div className={styles.newUser}> | ||
<CodeSnippet source={this.state.newUser.data} language="json" /> | ||
</div> | ||
{this.state.mfa && ( | ||
<div className={styles.mfa}> | ||
<div>Share this MFA Data with your user:</div> | ||
<a onClick={() => this.copy(this.state.newUser.mfa, 'MFA Data')}>{this.state.newUser.mfa}</a> | ||
<canvas id="canvas" /> | ||
</div> | ||
)} | ||
<div className={styles.footer}> | ||
<Button color="blue" value="Copy" width="120px" onClick={() => this.copy(this.state.newUser.data, 'New User')} /> | ||
<Button primary={true} value="Done" width="120px" onClick={() => this.setState({ username: '', password: '', passwordHidden: true, mfaAlgorithm: 'SHA1', mfaDigits: 6, mfaPeriod: 30, encrypt: true, createUserInput: false, newUser: { data: '', show: false } })} /> | ||
</div> | ||
</div> | ||
); | ||
return ( | ||
<div className={styles.settings_page}> | ||
<Fieldset legend="Dashboard Configuration"> | ||
<Field label={<Label text="Export Column Preferences" />} input={<FormButton color="blue" value="Export" onClick={() => this.getColumns()} />} /> | ||
<Field label={<Label text="Create New User" />} input={<FormButton color="blue" value="Create" onClick={() => this.setState({ createUserInput: true })} />} /> | ||
</Fieldset> | ||
{this.state.columnData.show && columnPreferences} | ||
{this.state.createUserInput && createUserInput} | ||
{this.state.newUser.show && userData} | ||
<Toolbar section="Settings" subsection="Dashboard Configuration" /> | ||
<Notification note={this.state.message} isErrorNote={false} /> | ||
</div> | ||
); | ||
} | ||
|
||
renderContent() { | ||
return <FlowView initialFields={{}} initialChanges={{}} footerContents={() => {}} onSubmit={() => {}} renderForm={() => this.renderForm()} />; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/dashboard/Settings/DashboardSettings/DashboardSettings.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,28 @@ | ||
.columnData { | ||
max-height: 50vh; | ||
overflow-y: scroll; | ||
} | ||
.newUser { | ||
max-height: 100px; | ||
overflow-y: scroll; | ||
} | ||
.settings_page { | ||
padding: 120px 0 80px 0; | ||
} | ||
.footer { | ||
display: flex; | ||
padding: 10px; | ||
justify-content: end; | ||
gap: 10px; | ||
} | ||
.password { | ||
display: flex; | ||
gap: 4px; | ||
} | ||
.userData { | ||
padding: 10px; | ||
} | ||
.mfa { | ||
display: block; | ||
margin-top: 10px; | ||
} |
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