Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add providers for opening files and getting server config #228

Merged
merged 10 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/config/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
"default": "",
"order": 2
},
"allowedConsumers": {
"title": "Whitelisted Consumers (encrypted)",
"description": "You can clear this to disallow access to your server configuration for all third-party packages.",
"type": "string",
"default": "",
"order": 3
},
"disallowedConsumers": {
"title": "Blacklisted Consumers (encrypted)",
"description": "You can clear this to allow all third-party packages to request access to your server configuration.",
"type": "string",
"default": "",
"order": 4
},
"tree": {
"type": "object",
"title": "Tree View",
Expand Down Expand Up @@ -81,7 +95,7 @@
"order": 9
}
},
"order": 3
"order": 5
},
"finder": {
"type": "object",
Expand Down Expand Up @@ -115,7 +129,7 @@
"order": 2
}
},
"order": 4
"order": 6
},
"notifications": {
"type": "object",
Expand All @@ -136,7 +150,7 @@
"order": 2
}
},
"order": 5
"order": 7
},
"dev": {
"type": "object",
Expand All @@ -150,6 +164,6 @@
"order": 1
}
},
"order": 6
"order": 8
}
}
142 changes: 141 additions & 1 deletion lib/ftp-remote-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import FindDialog from './dialogs/find-dialog.js';

import { $ } from 'atom-space-pen-views';
import { decrypt, encrypt, checkPasswordExists, checkPassword, setPassword, changePassword } from './helper/secure.js';
import { basename, trailingslashit } from './helper/format.js';
import { basename, trailingslashit, normalize } from './helper/format.js';
import { CompositeDisposable, Disposable, TextEditor } from 'atom';

const Path = require('path');
const getIconServices = require('./helper/icon.js');
const atom = global.atom;
const config = require('./config/config-schema.json');
Expand Down Expand Up @@ -147,6 +148,141 @@ class FtpRemoteEdit {
}
}

openRemoteFile() {
const self = this;
return (file) => {
const selected = self.treeView.list.find('.selected');
if (selected.length === 0) return;
let root = selected.view().getRoot();
let localPath = normalize(root.getLocalPath())
localPath = normalize(Path.join(localPath.slice(0, localPath.lastIndexOf(root.getPath())), file).replace(/\/+/g, Path.sep), Path.sep);
try {
let file = self.treeView.getElementByLocalPath(localPath, root, 'file');
file.open();
return true;
} catch (ex) {
if (self.debug) {
console.log(ex);
}
return false;
}
}
}

getCurrentServerName() {
const self = this;
return () => {
return new Promise((resolve, reject) => {
const selected = self.treeView.list.find('.selected');
if (selected.length === 0) reject('noservers')

let root = selected.view().getRoot();
resolve(root.name)
})
}
}

isInHashList(setting, msg) {
let hashes = this.getHashList(setting);
return hashes.indexOf(msg) > -1
}

getHashList(setting) {
let conf = atom.config.get(setting);
if (conf) {
try {
return JSON.parse(decrypt(this.info.password, conf));
} catch (ex) {
return []
}
} else {
return []
}
}

addToHashList(setting, msg) {
let hashes = this.getHashList(setting);
hashes.push(msg);
let str = JSON.stringify(hashes);
atom.config.set(setting, encrypt(this.info.password, str));
}

getCurrentServerConfig() {
const self = this;
return (reasonForRequest) => {
return new Promise((resolve, reject) => {
if (!reasonForRequest) {
reject('noreasongiven')
return
}

const selected = self.treeView.list.find('.selected');
if (selected.length === 0) {
reject('noservers')
return
}

let root = selected.view().getRoot();
let buttondismiss = false;

if (self.isInHashList('ftp-remote-edit.disallowedConsumers', reasonForRequest)) {
reject('userdeclined')
return
}
if (self.isInHashList('ftp-remote-edit.allowedConsumers', reasonForRequest)) {
resolve(root.config);
return
}

let caution = 'Decline this message if you did not initiate a request to share your server configuration with a pacakge!'
let notif = atom.notifications.addWarning('Server Configuration Requested', {
detail: reasonForRequest + '\n-------------------------------\n' + caution,
dismissable: true,
buttons: [
{
text: 'Always',
onDidClick: () => {
buttondismiss = true;
notif.dismiss();
self.addToHashList('ftp-remote-edit.allowedConsumers', reasonForRequest)
resolve(root.config);
}
},
{
text: 'Accept',
onDidClick: () => {
buttondismiss = true;
notif.dismiss();
resolve(root.config);
}
},
{
text: 'Decline',
onDidClick: () => {
buttondismiss = true;
notif.dismiss();
reject('userdeclined');
}
},
{
text: 'Never',
onDidClick: () => {
buttondismiss = true;
notif.dismiss();
self.addToHashList('ftp-remote-edit.disallowedConsumers', reasonForRequest)
reject('userdeclined');
}
},
]
})
let disposable = notif.onDidDismiss(() => {
if (!buttondismiss) reject('userdeclined');
disposable.dispose()
})
})
}
}

consumeElementIcons(service) {
getIconServices().setElementIcons(service)
return new Disposable(() => {
Expand Down Expand Up @@ -668,6 +804,10 @@ class FtpRemoteEdit {
}
}
}
}).catch(() => {
if (self.debug) {
console.log('Not a remote file.');
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/views/configuration-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ export default class ConfigurationView extends ScrollView {
const self = this;

if (self.configArray.length > 0) {
atom.config.set('ftp-remote-edit.config', encrypt(self.password, JSON.stringify(self.configArray)));
atom.config.set('ftp-remote-edit.config', encrypt(self.info.password, JSON.stringify(self.configArray)));
} else {
atom.config.set('ftp-remote-edit.config', '');
}
Expand Down
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@
}
}
},
"providedServices": {
"ftp-remote.openFile": {
"versions": {
"0.1.0": "openRemoteFile"
}
},
"ftp-remote.getCurrentServerConfig": {
"versions": {
"0.1.0": "getCurrentServerConfig"
}
},
"ftp-remote.getCurrentServerName": {
"versions": {
"0.1.0": "getCurrentServerName"
}
}
},
"uriHandler": {
"method": "handleURI",
"deferActivation": false
Expand Down