Commit e5691579 authored by Johannes Zellner's avatar Johannes Zellner

Use xml2json and beautify it

parent a4e436ab
node_modules/
loolwsd_test.xml
......@@ -54,8 +54,8 @@
<h1>Settings</h1>
<p>To use Collabora with NextCloud, enter the NextCloud domain in the allowed domains input. Then enable the <a href="https://apps.nextcloud.com/apps/richdocuments" target="_blank">Collabora app</a> in NextCloud and configure it with <b>{{ domain }}</b></p>
<el-form :model="settingsData" @submit.native.prevent label-position="top" :rules="settingsRules" @validate="onSettingsValidate">
<el-form-item label="Comma separated list of allowed domains" prop="allowedHosts">
<el-input v-model="settingsData.allowedHosts" required autofocus :disabled="settingsData.busy"></el-input>
<el-form-item label="Allowed domain" prop="allowedHost">
<el-input v-model="settingsData.allowedHost" required autofocus :disabled="settingsData.busy"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" native-type="submit" @click="onSettings" :loading="settingsData.busy" :disabled="!settingsData.valid">Save</el-button>
......
......@@ -22,7 +22,7 @@ function refresh() {
superagent.get('/api/settings').end(function (error, result) {
if (error) return console.error(error);
app.settingsData.allowedHosts = result.body.allowedHosts;
app.settingsData.allowedHost = result.body.allowedHost;
});
}
......@@ -43,15 +43,11 @@ var app = new Vue({
settingsData: {
busy: false,
valid: true,
allowedHosts: ''
allowedHost: ''
},
settingsRules: {
allowedHosts: [
{ required: true, message: 'Cannot be empty', trigger: 'change' },
{ validator: function (rule, value, callback) {
if (!value.match(/^\s*(?:(?:\w+(?:-+\w+)*\.)+[a-z]+)\s*(?:,\s*(?:(?:\w+(?:-+\w+)*\.)+[a-z]+)\s*)*$/)) return callback(new Error('Has to be comma-separated list of domains'));
callback();
}, trigger: 'change' }
allowedHost: [
{ required: true, message: 'Cannot be empty', trigger: 'change' }
]
}
},
......@@ -88,7 +84,7 @@ var app = new Vue({
that.settingsData.busy = true;
superagent.post('/api/settings', { allowedHosts: this.settingsData.allowedHosts }).end(function (error) {
superagent.post('/api/settings', { allowedHost: this.settingsData.allowedHost }).end(function (error) {
that.settingsData.busy = false;
if (error) console.error(error);
......
......@@ -50,6 +50,11 @@
"safe-buffer": "5.1.1"
}
},
"bindings": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz",
"integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw=="
},
"body-parser": {
"version": "1.18.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.2.tgz",
......@@ -311,6 +316,11 @@
"path-is-absolute": "1.0.1"
}
},
"hoek": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/hoek/-/hoek-4.2.1.tgz",
"integrity": "sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA=="
},
"http-errors": {
"version": "1.6.3",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz",
......@@ -347,6 +357,31 @@
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz",
"integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4="
},
"isemail": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/isemail/-/isemail-3.1.3.tgz",
"integrity": "sha512-5xbsG5wYADIcB+mfLsd+nst1V/D+I7EU7LEZPo2GOIMu4JzfcRs5yQoypP4avA7QtUqgxYLKBYNv4IdzBmbhdw==",
"requires": {
"punycode": "2.1.1"
}
},
"joi": {
"version": "13.5.2",
"resolved": "https://registry.npmjs.org/joi/-/joi-13.5.2.tgz",
"integrity": "sha512-3HrFXLC57iU5CzYth3cJRdYEo4/Dr+tXmCQ+BHyiTTKnKxJ9ICkI/WJGPwUUXj3dWA4tO2hwZO5oCdBNhAYuRg==",
"requires": {
"hoek": "5.0.3",
"isemail": "3.1.3",
"topo": "3.0.0"
},
"dependencies": {
"hoek": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/hoek/-/hoek-5.0.3.tgz",
"integrity": "sha512-Bmr56pxML1c9kU+NS51SMFkiVQAb+9uFfXwyqR2tn4w2FPvmPt65eZ9aCcEfRXd9G74HkZnILC6p967pED4aiw=="
}
}
},
"ldap-filter": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/ldap-filter/-/ldap-filter-0.2.2.tgz",
......@@ -473,8 +508,7 @@
"nan": {
"version": "2.10.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.10.0.tgz",
"integrity": "sha512-bAdJv7fBLhWC+/Bls0Oza+mvTaNQtP+1RyhhhvD95pgUJz6XM5IzgmxOkItJ9tkoCiplvAnXI1tNmmUD/eScyA==",
"optional": true
"integrity": "sha512-bAdJv7fBLhWC+/Bls0Oza+mvTaNQtP+1RyhhhvD95pgUJz6XM5IzgmxOkItJ9tkoCiplvAnXI1tNmmUD/eScyA=="
},
"ncp": {
"version": "2.0.0",
......@@ -487,6 +521,15 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
"integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk="
},
"node-expat": {
"version": "2.3.16",
"resolved": "https://registry.npmjs.org/node-expat/-/node-expat-2.3.16.tgz",
"integrity": "sha512-e3HyQI0lk5CXyYQ4RsDYGiWdY5LJxNMlNCzo4/gwqY8lhYIeTf5VwGirGDa1EPrcZROmOR37wHuFVnoHmOWnOw==",
"requires": {
"bindings": "1.3.0",
"nan": "2.10.0"
}
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
......@@ -538,6 +581,11 @@
"ipaddr.js": "1.8.0"
}
},
"punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
},
"qs": {
"version": "6.5.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz",
......@@ -587,6 +635,11 @@
}
}
},
"repeat-string": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc="
},
"rimraf": {
"version": "2.4.5",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz",
......@@ -607,11 +660,6 @@
"integrity": "sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==",
"optional": true
},
"sax": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
},
"send": {
"version": "0.16.2",
"resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz",
......@@ -653,6 +701,21 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz",
"integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew=="
},
"topo": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/topo/-/topo-3.0.0.tgz",
"integrity": "sha512-Tlu1fGlR90iCdIPURqPiufqAlCZYzLjHYVVbcFWDMcX7+tK8hdZWAfsMrD/pBul9jqHHwFjNdf1WaxA9vTRRhw==",
"requires": {
"hoek": "5.0.3"
},
"dependencies": {
"hoek": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/hoek/-/hoek-5.0.3.tgz",
"integrity": "sha512-Bmr56pxML1c9kU+NS51SMFkiVQAb+9uFfXwyqR2tn4w2FPvmPt65eZ9aCcEfRXd9G74HkZnILC6p967pED4aiw=="
}
}
},
"type-is": {
"version": "1.6.16",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.16.tgz",
......@@ -718,19 +781,23 @@
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
},
"xml2js": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",
"integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==",
"xml-but-prettier": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/xml-but-prettier/-/xml-but-prettier-1.0.1.tgz",
"integrity": "sha1-9aMyZ+1CzNTjVcYlV6XjmwH7QPM=",
"requires": {
"sax": "1.2.4",
"xmlbuilder": "9.0.7"
"repeat-string": "1.6.1"
}
},
"xmlbuilder": {
"version": "9.0.7",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
"integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0="
"xml2json": {
"version": "0.11.2",
"resolved": "https://registry.npmjs.org/xml2json/-/xml2json-0.11.2.tgz",
"integrity": "sha512-ZJpHpPOL0T5lOvAHMnWm59iQOPqNtam5t2TMUllWZ1k5Wm8L5YyvQnkeaVnRKCvDwY5EumqXWyOjjMdQVz272A==",
"requires": {
"hoek": "4.2.1",
"joi": "13.5.2",
"node-expat": "2.3.16"
}
}
}
}
......@@ -14,7 +14,8 @@ var exec = require('child_process').exec,
ldap = require('ldapjs'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
xml2js = require('xml2js');
xmlButPrettier = require('xml-but-prettier'),
xml2json = require('xml2json');
const ldapClient = ldap.createClient({
url: process.env.LDAP_URL,
......@@ -22,7 +23,7 @@ const ldapClient = ldap.createClient({
reconnect: true /* undocumented option to automatically reconnect on connection failure : https://github.com/joyent/node-ldapjs/issues/318#issuecomment-165769581 */
});
const LOOL_CONFIG_FILE = process.env.CLOUDRON ? '/app/data/loolwsd.xml' : path.resolve(__dirname, 'loolwsd.xml');
const LOOL_CONFIG_FILE = process.env.CLOUDRON ? '/app/data/loolwsd.xml' : path.resolve(__dirname, 'loolwsd_test.xml');
function isAuthenticated(req, res, next) {
if (req.session && req.session.user) return next();
......@@ -94,12 +95,13 @@ function getSettings(req, res, next) {
fs.readFile(LOOL_CONFIG_FILE, function (error, result) {
if (error) return next(new HttpError(500, error));
var parser = new xml2js.Parser();
parser.parseString(result, function (error, result) {
if (error) return next(new HttpError(500, error));
var settings = xml2json.toJson(result, { object: true, reversible: true });
var wopi = settings.config.storage.wopi;
next(new HttpSuccess(200, { allowedHosts: result.config.storage[0].wopi[0].host[0]['_'] }));
});
// Only one wopi host is supported at all times
if (Array.isArray(wopi.host)) wopi.host = wopi.host[0];
next(new HttpSuccess(200, { allowedHost: wopi.host['$t'] }));
});
}
......@@ -107,26 +109,26 @@ function setSettings(req, res, next) {
fs.readFile(LOOL_CONFIG_FILE, function (error, result) {
if (error) return next(new HttpError(500, error));
var parser = new xml2js.Parser();
parser.parseString(result, function (error, result) {
if (error) return next(new HttpError(500, error));
var settings = xml2json.toJson(result, { object: true });
// We only allow certain properties to be changed
result.config.storage[0].wopi[0].host[0]['_'] = req.body.allowedHosts;
if (!settings.config.storage) settings.config.storage = {};
if (!settings.config.storage.wopi) settings.config.storage.wopi = { desc: 'Allow/deny wopi storage. Mutually exclusive with webdav.', allow: 'true' };
var builder = new xml2js.Builder();
var xml = builder.buildObject(result);
settings.config.storage.wopi.host = {
desc: 'Regex pattern of hostname to allow or deny.',
allow: 'true',
'$t': req.body.allowedHost.trim()
};
fs.writeFile(LOOL_CONFIG_FILE, xml, function (error) {
if (error) return next(new HttpError(500, error));
fs.writeFile(LOOL_CONFIG_FILE, xmlButPrettier(xml2json.toXml(JSON.stringify(settings)), { textNodesOnSameLine: true }), function (error) {
if (error) return next(new HttpError(500, error));
if (process.env.CLOUDRON) {
console.log('Restarting lool process');
exec('supervisorctl restart lool');
}
if (process.env.CLOUDRON) {
console.log('Restarting lool process');
exec('supervisorctl restart lool');
}
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
});
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment