Merge pull request #3599 from ethereum/save_file_remember_choice

Save file remember choice
pull/5370/head
yann300 2 years ago committed by GitHub
commit 54e50d0739
  1. 2
      apps/remix-ide/src/app/files/fileManager.ts
  2. 79
      apps/remix-ide/src/app/plugins/permission-handler-plugin.tsx
  3. 2
      apps/remix-ide/src/app/tabs/locales/en/permissionHandler.json
  4. 11
      libs/remix-ui/permission-handler/src/lib/permission-dialog.tsx

@ -562,7 +562,7 @@ class FileManager extends Plugin {
async setFileContent(path, content) {
if (this.currentRequest) {
const canCall = await this.askUserPermission('writeFile', '')
const canCall = await this.askUserPermission(`writeFile`, `modifying ${path} ...`)
const required = this.appManager.isRequired(this.currentRequest.from)
if (canCall && !required) {
// inform the user about modification after permission is granted and even if permission was saved before

@ -13,12 +13,14 @@ const profile = {
}
export class PermissionHandlerPlugin extends Plugin {
permissions: any
sessionPermissions: any
currentVersion: number
fallbackMemory: boolean
constructor() {
super(profile)
this.fallbackMemory = false
this.permissions = this._getFromLocal()
this.sessionPermissions = {}
this.currentVersion = 1
// here we remove the old permissions saved before adding 'permissionVersion'
// since with v1 the structure has been changed because of new engine ^0.2.0-alpha.6 changes
@ -44,14 +46,22 @@ export class PermissionHandlerPlugin extends Plugin {
}
}
switchMode (from: Profile, to: Profile, method: string, set: boolean) {
set
switchMode (from: Profile, to: Profile, method: string, set: boolean, sensitiveCall: boolean) {
if (sensitiveCall) {
set
? this.sessionPermissions[to.name][method][from.name] = {}
: delete this.sessionPermissions[to.name][method][from.name]
} else {
set
? this.permissions[to.name][method][from.name] = {}
: delete this.permissions[to.name][method][from.name]
}
}
clear() {
localStorage.removeItem('plugins/permissions')
this.permissions = this._getFromLocal()
this.sessionPermissions = {}
}
notAllowWarning(from: Profile, to: Profile, method: string) {
@ -72,12 +82,18 @@ export class PermissionHandlerPlugin extends Plugin {
*/
async askPermission(from: Profile, to: Profile, method: string, message: string, sensitiveCall: boolean) {
try {
this.permissions = this._getFromLocal()
if (!this.permissions[to.name]) this.permissions[to.name] = {}
if (!this.permissions[to.name][method]) this.permissions[to.name][method] = {}
if (!this.permissions[to.name][method][from.name]) return this.openPermission(from, to, method, message, sensitiveCall)
if (sensitiveCall) {
if (!this.sessionPermissions[to.name]) this.sessionPermissions[to.name] = {}
if (!this.sessionPermissions[to.name][method]) this.sessionPermissions[to.name][method] = {}
if (!this.sessionPermissions[to.name][method][from.name]) return this.openPermission(from, to, method, message, sensitiveCall)
} else {
this.permissions = this._getFromLocal()
if (!this.permissions[to.name]) this.permissions[to.name] = {}
if (!this.permissions[to.name][method]) this.permissions[to.name][method] = {}
if (!this.permissions[to.name][method][from.name]) return this.openPermission(from, to, method, message, sensitiveCall)
}
const { allow, hash } = this.permissions[to.name][method][from.name]
const { allow, hash } = sensitiveCall ? this.sessionPermissions[to.name][method][from.name] : this.permissions[to.name][method][from.name]
if (!allow) {
const warning = this.notAllowWarning(from, to, method)
this.call('notification', 'toast', warning)
@ -92,7 +108,12 @@ export class PermissionHandlerPlugin extends Plugin {
}
async openPermission(from: Profile, to: Profile, method: string, message: string, sensitiveCall: boolean) {
const remember = this.permissions[to.name][method][from.name]
let remember
if (sensitiveCall) {
remember = this.sessionPermissions[to.name][method][from.name]
} else {
remember = this.permissions[to.name][method][from.name]
}
const value: PermissionHandlerValue = {
from,
to,
@ -112,22 +133,40 @@ export class PermissionHandlerPlugin extends Plugin {
const result = await this.call('notification', 'modal', modal)
return new Promise((resolve, reject) => {
if (result) {
if (this.permissions[to.name][method][from.name]) {
this.permissions[to.name][method][from.name] = {
allow: true,
hash: from.hash
}
this.persistPermissions()
if (sensitiveCall) {
if (this.sessionPermissions[to.name][method][from.name]) {
this.sessionPermissions[to.name][method][from.name] = {
allow: true,
hash: from.hash
}
}
} else {
if (this.permissions[to.name][method][from.name]) {
this.permissions[to.name][method][from.name] = {
allow: true,
hash: from.hash
}
this.persistPermissions()
}
}
resolve(true)
} else {
if (this.permissions[to.name][method][from.name]) {
this.permissions[to.name][method][from.name] = {
allow: false,
hash: from.hash
if (sensitiveCall) {
if (this.sessionPermissions[to.name][method][from.name]) {
this.sessionPermissions[to.name][method][from.name] = {
allow: false,
hash: from.hash
}
}
this.persistPermissions()
}
} else {
if (this.permissions[to.name][method][from.name]) {
this.permissions[to.name][method][from.name] = {
allow: false,
hash: from.hash
}
this.persistPermissions()
}
}
reject(this.notAllowWarning(from, to, method))
}
})

@ -4,7 +4,7 @@
"permissionHandler.permissionHandlerMessage": "\"{from}\" {rememberText} would like to access to \"{method}\" of \"{to}\"`",
"permissionHandler.description": "Description",
"permissionHandler.noDescriptionProvided": "No description Provided",
"permissionHandler.makeSureYouTrustThisPlugin": "Make sure you trust this plugin before processing this call.",
"permissionHandler.makeSureYouTrustThisPlugin": "Make sure you trust this plugin before processing this call. If you choose to remember the choice for this specific call, the value will be kept only for the current session.",
"permissionHandler.rememberThisChoice": "Remember this choice",
"permissionHandler.resetAllPermissions": "Reset all Permissions",
"permissionHandler.permissionNeededFor": "Permission needed for {to}",

@ -10,12 +10,9 @@ const PermissionHandlerDialog = (props: PermissionHandlerProps) => {
const intl = useIntl()
const switchMode = (e: any) => {
props.plugin.switchMode(from, to, method, e.target.checked)
props.plugin.switchMode(from, to, method, e.target.checked, sensitiveCall)
}
const rememberSwitch = () => {
return <input type="checkbox" onChange={switchMode} className='form-check-input' id='remember' data-id={remember ? 'permissionHandlerRememberChecked' : 'permissionHandlerRememberUnchecked'}/>
}
const reset = () => {
props.plugin.clear()
setFeedback(intl.formatMessage({ id: 'permissionHandler.allPermissionsReset' }))
@ -57,9 +54,9 @@ const PermissionHandlerDialog = (props: PermissionHandlerProps) => {
{ sensitiveCall ? <p className='text-warning'><i className="fas fa-exclamation-triangle mr-2" aria-hidden="true"></i><FormattedMessage id='permissionHandler.makeSureYouTrustThisPlugin' /></p> : '' }
</article>
<article className='remember'>
{ !sensitiveCall && <div className='form-check'>
{rememberSwitch()}
<label htmlFor='remember' className="form-check-label" data-id="permissionHandlerRememberChoice"><FormattedMessage id='permissionHandler.rememberThisChoice' /></label>
{ <div className='form-check'>
<input type="checkbox" onChange={switchMode} className='form-check-input' id='rememberSwitchCheck' data-id={remember ? 'permissionHandlerRememberChecked' : 'permissionHandlerRememberUnchecked'}/>
<label htmlFor='rememberSwitchCheck' className="form-check-label" data-id="permissionHandlerRememberChoice"><FormattedMessage id='permissionHandler.rememberThisChoice' /></label>
</div>
}
<button className="btn-secondary btn-sm" onClick={reset}><FormattedMessage id='permissionHandler.resetAllPermissions' /></button>

Loading…
Cancel
Save