writeFile remeber choice per session

pull/3599/head
yann300 2 years ago
parent 2a8f3a7d13
commit 0e318280df
  1. 2
      apps/remix-ide/src/app/files/fileManager.ts
  2. 75
      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) { async setFileContent(path, content) {
if (this.currentRequest) { 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) const required = this.appManager.isRequired(this.currentRequest.from)
if (canCall && !required) { if (canCall && !required) {
// inform the user about modification after permission is granted and even if permission was saved before // 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 { export class PermissionHandlerPlugin extends Plugin {
permissions: any permissions: any
sessionPermissions: any
currentVersion: number currentVersion: number
fallbackMemory: boolean fallbackMemory: boolean
constructor() { constructor() {
super(profile) super(profile)
this.fallbackMemory = false this.fallbackMemory = false
this.permissions = this._getFromLocal() this.permissions = this._getFromLocal()
this.sessionPermissions = {}
this.currentVersion = 1 this.currentVersion = 1
// here we remove the old permissions saved before adding 'permissionVersion' // 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 // 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) { switchMode (from: Profile, to: Profile, method: string, set: boolean, sensitiveCall: boolean) {
set 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] = {} ? this.permissions[to.name][method][from.name] = {}
: delete this.permissions[to.name][method][from.name] : delete this.permissions[to.name][method][from.name]
}
} }
clear() { clear() {
localStorage.removeItem('plugins/permissions') localStorage.removeItem('plugins/permissions')
this.permissions = this._getFromLocal()
this.sessionPermissions = {}
} }
notAllowWarning(from: Profile, to: Profile, method: string) { notAllowWarning(from: Profile, to: Profile, method: string) {
@ -72,10 +82,16 @@ export class PermissionHandlerPlugin extends Plugin {
*/ */
async askPermission(from: Profile, to: Profile, method: string, message: string, sensitiveCall: boolean) { async askPermission(from: Profile, to: Profile, method: string, message: string, sensitiveCall: boolean) {
try { try {
this.permissions = this._getFromLocal() if (sensitiveCall) {
if (!this.permissions[to.name]) this.permissions[to.name] = {} if (!this.sessionPermissions[to.name]) this.sessionPermissions[to.name] = {}
if (!this.permissions[to.name][method]) this.permissions[to.name][method] = {} if (!this.sessionPermissions[to.name][method]) this.sessionPermissions[to.name][method] = {}
if (!this.permissions[to.name][method][from.name]) return this.openPermission(from, to, method, message, sensitiveCall) 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 } = this.permissions[to.name][method][from.name]
if (!allow) { if (!allow) {
@ -92,7 +108,12 @@ export class PermissionHandlerPlugin extends Plugin {
} }
async openPermission(from: Profile, to: Profile, method: string, message: string, sensitiveCall: boolean) { 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 = { const value: PermissionHandlerValue = {
from, from,
to, to,
@ -112,21 +133,39 @@ export class PermissionHandlerPlugin extends Plugin {
const result = await this.call('notification', 'modal', modal) const result = await this.call('notification', 'modal', modal)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (result) { if (result) {
if (this.permissions[to.name][method][from.name]) { if (sensitiveCall) {
this.permissions[to.name][method][from.name] = { if (this.sessionPermissions[to.name][method][from.name]) {
allow: true, this.sessionPermissions[to.name][method][from.name] = {
hash: from.hash allow: true,
} hash: from.hash
this.persistPermissions() }
}
} 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) resolve(true)
} else { } else {
if (this.permissions[to.name][method][from.name]) { if (sensitiveCall) {
this.permissions[to.name][method][from.name] = { if (this.sessionPermissions[to.name][method][from.name]) {
allow: false, this.sessionPermissions[to.name][method][from.name] = {
hash: from.hash allow: false,
hash: from.hash
}
}
} else {
if (this.permissions[to.name][method][from.name]) {
this.permissions[to.name][method][from.name] = {
allow: false,
hash: from.hash
}
this.persistPermissions()
} }
this.persistPermissions()
} }
reject(this.notAllowWarning(from, to, method)) reject(this.notAllowWarning(from, to, method))
} }

@ -4,7 +4,7 @@
"permissionHandler.permissionHandlerMessage": "\"{from}\" {rememberText} would like to access to \"{method}\" of \"{to}\"`", "permissionHandler.permissionHandlerMessage": "\"{from}\" {rememberText} would like to access to \"{method}\" of \"{to}\"`",
"permissionHandler.description": "Description", "permissionHandler.description": "Description",
"permissionHandler.noDescriptionProvided": "No description Provided", "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, the value will be kept only for the current session.",
"permissionHandler.rememberThisChoice": "Remember this choice", "permissionHandler.rememberThisChoice": "Remember this choice",
"permissionHandler.resetAllPermissions": "Reset all Permissions", "permissionHandler.resetAllPermissions": "Reset all Permissions",
"permissionHandler.permissionNeededFor": "Permission needed for {to}", "permissionHandler.permissionNeededFor": "Permission needed for {to}",

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

Loading…
Cancel
Save